MSP430F149学习之路——UART

代码一:

 1 #include 
 2 void int_clk()
 3 {
 4   BCSCTL1 &= ~XT2OFF;
 5   BCSCTL2 |= SELM_2 + SELS; 
 6   do
 7   {
 8     IFG1 &= ~OFIFG;
 9     for(int i=0;i<100;i++)
10       _NOP();
11   }while((IFG1 & OFIFG)!=0);
12   IFG1 &= ~OFIFG;
13 }
14 void int_usart()
15 {
16   U0CTL |= SWRST;    //复位串口
17   U0CTL |= CHAR;     //8位数据
18   U0TCTL |= SSEL1;   //选择SMCLK
19   //波特率19200
20   U0BR1 = 0X01;
21   U0BR0 = 0XA0;
22   UMCTL0 = 0XC0;
23    
24   ME1 |= UTXE0 + URXE0;   //使能接收和发送
25   U0CTL &= ~SWRST;        //清楚串口复位信号
26   
27   IE1 |= URXIE0;          //使能接收中断
28   P3SEL |= BIT4 + BIT5;
29   P3DIR |= BIT4;
30 }
31 void sent_byte(unsigned char data)
32 {
33   while((IFG1 & UTXIFG0)==0);
34   U0TXBUF = data;
35 }
36 #pragma vector=UART0RX_VECTOR
37 __interrupt void UART0(void)
38 {
39   unsigned char data = 0;
40   data = U0RXBUF;
41   sent_byte(data);
42 }
43 int main()
44 {
45   WDTCTL = WDTPW + WDTHOLD;
46   int_clk();
47   int_usart();
48   _EINT();
49   return 0;
50 }

代码二:

 1 #include 
 2 void int_clk()
 3 {
 4   BCSCTL1 &= XT2OFF;
 5   BCSCTL2 |= SELS + SELM_2;
 6   do
 7   {
 8     IFG1 &= ~OFIFG;
 9     for(int i=0;i<100;i++)
10       _NOP();
11   }while((IFG1 & OFIFG)!=0);
12   IFG1 &= ~OFIFG;
13 }
14 void int_usart()
15 {
16   U0CTL |= SWRST;
17   U0CTL |= CHAR;
18   U0TCTL = SSEL1;
19    //9600
20   U0BR1 = 0X03;
21   U0BR0 = 0X41;
22   U0MCTL = 0X00;
23  
24   ME1 |= UTXE0 + URXE0;
25   U0CTL &= ~SWRST;
26   IE1 |= URXIE0;
27   
28   P3SEL |= BIT4 + BIT5;
29   P3DIR |= BIT4;
30 }
31 void send_byte(unsigned char data)
32 {
33   while((IFG1 & UTXIFG0)==0);
34   U0TXBUF = data;
35 }
36 //串口0发送字符串函数
37 /*void Print_Str(char *s)
38 {
39   while(*s != '\0')
40   {
41     send_byte(*s++);
42   }
43 }
44 */
45 #pragma vector=UART0RX_VECTOR
46 __interrupt void UART0_RX_ISR(void)
47 {
48   unsigned char data;
49   data = U0RXBUF;
50   P6OUT = data;
51   send_byte(data);
52 }
53 void main()
54 {
55   WDTCTL = WDTPW + WDTHOLD;
56   P6DIR = 0XFF;
57   P6OUT = 0XFF;
58   int_clk();
59   int_usart();
60   _EINT();
61 //  Print_Str("hello!\n");
62 }

 

转载于:https://www.cnblogs.com/boyiliushui/p/4638932.html

你可能感兴趣的:(MSP430F149学习之路——UART)