MSP430另一种UART实现

  1 #include "msp430g2553.h"

  2 

  3 typedef unsigned char uchar;

  4 typedef unsigned int  uint;

  5 

  6 #define LED     BIT0

  7 #define TXD     BIT1                            // TXD on P1.1

  8 #define RXD    BIT2                             // RXD on P1.2

  9 #define POUT    P1OUT

 10 

 11 #define BITTIME_1b       13*4    //1bit宽度

 12 #define BITTIME_1b5      13*6    //1.5bit宽度

 13 

 14 uchar   bitcnt;

 15 uint    uart_buf;

 16 int    Send_flag;

 17 

 18 uchar  *str=" Hello EEWorld!  \r";

 19 

 20 void FaultRoutine(void)

 21  {

 22    while(1);                                 // 异常挂起

 23  }

 24 

 25 void ConfigClocks(void)

 26  {

 27  if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)

 28    FaultRoutine();                            // If calibration data is erased

 29                                              // run FaultRoutine()

 30   BCSCTL1 = CALBC1_1MHZ;                     // Set range

 31   DCOCTL = CALDCO_1MHZ;                      // Set DCO step + modulation

 32   BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO

 33   IFG1 &= ~OFIFG;                           // Clear OSCFault flag

 34   BCSCTL2 = 0;                               // MCLK = DCO = SMCLK

 35  }

 36 

 37 void ConfigPins(void)

 38  {

 39   P1DIR |= TXD+LED;

 40   P1DIR &= ~RXD;                    // P1.3 input, other outputs

 41   P1OUT |= TXD;                                  // clear output pins

 42 

 43  }

 44 

 45 

 46 //发送一个字节

 47 void send_char(uchar tchar)

 48 {

 49   TACTL = TACLR + TASSEL_2 + ID_3;              //选择SMCLK时钟;清TAR

 50   CCR0 = BITTIME_1b5;                    //crr0定时间隔为1bit时间宽度

 51   CCTL0 |= CCIE;                        //打开CCR0中断

 52   bitcnt = 10;                           //待发送的位数

 53   uart_buf = 0x0100;                     //8+N+1

 54   uart_buf |= tchar;                  //stop bit and start bit;

 55   uart_buf <<=1;

 56   Send_flag = 0;

 57   TACTL |= MC_1;                        //Start TA, UP mode.

 58   _BIS_SR(GIE);

 59   while(!Send_flag);                   //wait until send complete

 60   Send_flag = 1;

 61 }

 62 

 63 //发送一个字符串

 64 void send_String(uchar *tstr)

 65 {

 66   while(*tstr)

 67     send_char(*tstr++);

 68 }

 69 

 70 

 71 void send_IRQ(void)

 72 {

 73   if(bitcnt>0)

 74   {

 75     if(uart_buf & 0x01)

 76       POUT |= TXD;

 77     else

 78       POUT &= ~TXD;

 79     uart_buf >>= 1;

 80     bitcnt--;

 81   }

 82   else

 83   {

 84     TACTL &= ~MC_3;                    //Close the TA when a Byte send over.   ??

 85     CCTL0 &= ~CCIE;                    //关闭CCR0中断

 86     Send_flag = 1;

 87   }

 88 

 89 }

 90 

 91 

 92 

 93 void main( void )

 94 {

 95   // Stop watchdog timer to prevent time out reset

 96   uint i;

 97 

 98 

 99 

100   WDTCTL = WDTPW + WDTHOLD;

101   ConfigClocks();

102   ConfigPins();

103 

104 

105   while(1)

106   {

107    send_String(str);

108 

109    for(i=50000;i>0;i--);

110    for(i=50000;i>0;i--);

111    for(i=50000;i>0;i--);

112    for(i=50000;i>0;i--);

113    POUT ^= LED; // 翻转LED

114   }

115 }

116 

117 // Timer A0 interrupt service routine

118 #pragma vector=TIMER0_A0_VECTOR

119 __interrupt void Timer_A (void)

120 {

121 

122 

123    send_IRQ();

124 }

你可能感兴趣的:(实现)