当前位置:首页 > 单片机 > 单片机
[导读]初始化1 void Uart_Init(void)2 {3 BCSCTL1 = CALBC1_1MHZ; // Set DCO4 DCOCTL = CALDCO_1MHZ;5 P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD6 P1SEL2 = BIT1 + BIT2 ;

初始化



1 void Uart_Init(void)

2 {

3 BCSCTL1 = CALBC1_1MHZ; // Set DCO

4 DCOCTL = CALDCO_1MHZ;

5 P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD

6 P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD

7 UCA0CTL1 |= UCSSEL_2; // SMCLK

8 UCA0BR0 = 104; // 1MHz 9600

9 UCA0BR1 = 0; // 1MHz 9600

10 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1

11 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**

12 IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt

13 }


1 // Echo back RXed character, confirm TX buffer is ready first

2 #pragma vector=USCIAB0RX_VECTOR

3 __interrupt void USCI0RX_ISR(void)

4 {

5 while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?

6 UCA0TXBUF = UCA0RXBUF; // TX -> RXed character

7 }




1 //发送数据

2 //发送字符

3 void uart_send_ch(unsigned char ch)

4 {

5

6 while(!(IFG2& UCA0TXIFG)); //查询发送是否结束

7 UCA0TXBUF = ch;

8 IFG2&=~UCA0TXIFG; //清除发送一标志位

9 }

10

11 //发送字符串

12 void uart_send_str(char *str)

13 {

14 for( ; *str ; )

15 {

16 uart_send_ch((unsigned char)*str);

17 str++;

18 }

19 }


在用uart_send_str()发送一个数组合紧接着发送回车会出现乱码,大约九个字符出现,在中间delay一下解决了


char a[4];


uart_send_str(a);


__delay_cycles(5);


uart_send_huiche();



void uart_send_huiche(void)

{

uart_send_ch(0x0d);

uart_send_ch(0x0a);

}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭