標(biāo)題: MSP430F149發(fā)送數(shù)據(jù)到PC [打印本頁(yè)]

作者: 北京北2016    時(shí)間: 2016-5-7 14:47
標(biāo)題: MSP430F149發(fā)送數(shù)據(jù)到PC
用MSP430F149單片機(jī)發(fā)送數(shù)據(jù)到PC程序

  1. /*****************************************************
  2. 程序功能:MCU不停向PC機(jī)發(fā)送數(shù)據(jù),在屏幕上顯示0~127對(duì)應(yīng)
  3.           的ASCII字符
  4. ------------------------------------------------------
  5. 通信格式:N.8.1, 2400
  6. ------------------------------------------------------
  7. 測(cè)試說明:打開串口調(diào)試助手,正確設(shè)置通信格式,觀察屏幕
  8. ******************************************************/

  9. #include  <msp430x14x.h>
  10. #define uchar unsigned char
  11. #define uint  unsigned int
  12. void Delays(void);
  13. void PutString(uchar *ptr);

  14. /********************主函數(shù)**********************/
  15. void main(void)
  16. {
  17.     uchar *tishi = " MCU sends 0~127 to PC and the\
  18.                     \n screen will display their corresponding\
  19.                     \n ASCII code as follows:";
  20.     uchar value = 0;
  21.   
  22.     WDTCTL = WDTPW + WDTHOLD;                 // 關(guān)狗
  23.     P3SEL |= 0x30;                            // P3.4,5選擇為UART收發(fā)端口
  24.     ME1 |= UTXE0 + URXE0;                     // 使能USART0收發(fā)
  25.     UCTL0 |= CHAR;                            // 8-bit character
  26.     UTCTL0 |= SSEL0;                          // UCLK = ACLK
  27.     UBR00 = 0x0D;                             // 32k/2400 - 13.65
  28.     UBR10 = 0x00;                             //
  29.     UMCTL0 = 0x6B;                            // Modulation
  30.     UCTL0 &= ~SWRST;                          // 初始化UART0狀態(tài)機(jī)
  31.     IE1 |= URXIE0;                            // 使能接收中斷
  32.     _EINT();
  33.     PutString(tishi);
  34.     while(1)
  35.     {     
  36.         while (!(IFG1 & UTXIFG0));
  37.         TXBUF0 = value++;
  38.         value &= 0x7f;                        // 保證value的數(shù)值小于128
  39.         while (!(IFG1 & UTXIFG0));
  40.         TXBUF0 = '\n';
  41.         Delays();
  42.     }
  43. }
  44. /*******************************************
  45. 函數(shù)名稱:PutSting
  46. 功    能:向PC機(jī)發(fā)送字符串
  47. 參    數(shù):無
  48. 返回值  :無
  49. ********************************************/
  50. void PutString(uchar *ptr)
  51. {
  52.       while(*ptr != '\0')
  53.       {
  54.             while (!(IFG1 & UTXIFG0));                // TX緩存空閑?
  55.             TXBUF0 = *ptr++;                       // 發(fā)送數(shù)據(jù)
  56.       }
  57.       while (!(IFG1 & UTXIFG0));
  58.       TXBUF0 = '\n';
  59. }
  60. /*******************************************
  61. 函數(shù)名稱:Delays
  62. 功    能:延時(shí)一會(huì)
  63. 參    數(shù):無
  64. 返回值  :無
  65. ********************************************/
  66. void Delays(void)
  67. {
  68.     uchar i=20;
  69.     uint j;

  70.     while(i--)
  71.     {
  72.             j=2000;
  73.             while(j--);
  74.     }
  75. }
復(fù)制代碼








歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1