[LaunchPad] 串口测试

  1 #include  "msp430g2553.h"
  2 #include  "stdio.h"
  3 
  4 
  5 unsigned char RxBuf[256]="haha";
  6 unsigned char RxFlag;
  7 unsigned char RxPoint;
  8 
  9 
 10 int putchar(int ch)
 11 {
 12   while(!(IFG2&UCA0TXIFG));
 13    UCA0TXBUF=ch;
 14    return ch;
 15 }
 16 
 17 
 18  void sendChar(unsigned char c)
 19  {
 20    while(!(IFG2&UCA0TXIFG));
 21    UCA0TXBUF=c;
 22  }
 23 
 24  void sendStr(unsigned char *s)
 25  {
 26        while(*s!='\0')
 27        {
 28                sendChar(*s);
 29                s++;
 30        }
 31  }
 32  
 33  void main(void)
 34  
 35  {
 36  
 37  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
 38  
 39   BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
 40   DCOCTL = CALDCO_1MHZ;
 41   BCSCTL2 &= ~(DIVS_3);
 42  
 43  
 44   P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
 45   P1SEL2 = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
 46  
 47   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
 48   UCA0BR0 = 104;                            // 1MHz 9600
 49   UCA0BR1 = 0;                              // 1MHz 9600
 50   UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
 51   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
 52  
 53   IE2 |= UCA0RXIE;   // Enable USCI_A0 RX interrupt
 54   
 55   
 56   RxFlag=0; 
 57   RxPoint=0;
 58   _EINT();
 59   
 60  
 61  printf("\n%s\n","========串口测试=========");
 62 
 63  //__bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, interrupts enabled
 64  
 65  
 66   while(1)
 67   {
 68   __delay_cycles(1000000);
 69  if(RxFlag == 0)
 70   printf("%s\n",RxBuf); 
 71   }
 72   
 73   
 74  }
 75  
 76  //  Echo back RXed character, confirm TX buffer is ready first
 77  
 78  #pragma vector=USCIAB0RX_VECTOR
 79  __interrupt void USCI0RX_ISR(void)
 80  {
 81 
 82 
 83   while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
 84  
 85   if(RxFlag == 0)
 86   {
 87     RxFlag = 1;
 88     RxBuf[RxPoint] = UCA0RXBUF;
 89      RxPoint++;
 90   }
 91   else
 92   {
 93      if(UCA0RXBUF=='*')
 94     {
 95       putchar('$');
 96       RxBuf[RxPoint]='\0';
 97       RxFlag = 0;
 98       RxPoint = 0;
 99     }
100     else
101     {
102         RxBuf[RxPoint] = UCA0RXBUF;
103         RxPoint++;
104     }
105   }
106  
107  }


HW模式,RxFlag=0,表示接收完毕,=1为正在接收,实现printf函数

posted @ 2012-08-24 09:25  sky1991  阅读(574)  评论(0编辑  收藏  举报