初始化
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 //發(fā)送數(shù)據(jù)
2 //發(fā)送字符
3 void uart_send_ch(unsigned char ch)
4 {
5
6 while(!(IFG2& UCA0TXIFG)); //查詢發(fā)送是否結(jié)束
7 UCA0TXBUF = ch;
8 IFG2&=~UCA0TXIFG; //清除發(fā)送一標志位
9 }
10
11 //發(fā)送字符串
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()發(fā)送一個數(shù)組合緊接著發(fā)送回車會出現(xiàn)亂碼,大約九個字符出現(xiàn),在中間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);
}