6.UART的相關寄存器:
UART的相關寄存器的每位功能參考TI公司提供的數(shù)據(jù)手冊進行查看?。?!
7.簡單例程:
//將將接收到數(shù)據(jù)再發(fā)送回去;可以通過串口調試助手,觀察單片機發(fā)送的數(shù)據(jù)
#includevoid main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD;// Stop WDT
P3SEL |= 0x30;// P3.4,5 = USART0 TXD/RXD
//使用低功耗模式
BCSCTL1 |= XTS;// ACLK= LFXT1= HF XTAL
do
{
IFG1 &= ~OFIFG;// Clear OSCFault flag
for (i = 0xFF; i > 0; i--);// Time for flag to set
}
while ((IFG1 & OFIFG));// OSCFault flag still set?
BCSCTL2 |= SELM_3;// MCLK = LFXT1 (safe)
//配置UART的功能
UCTL0 |= CHAR;// 8-bit character
UTCTL0 |= SSEL0;// UCLK= ACLK
UBR00 = 0x45;// 8MHz 115200
UBR10 = 0x00;// 8MHz 115200
UMCTL0 = 0x00;// 8MHz 115200 modulation
UCTL0 &= ~SWRST;// Initialize USART state machine
//發(fā)送和接受使能及其中斷使能
ME1 |= UTXE0 + URXE0;// Enable USART0 TXD/RXD
IE1 |= URXIE0;// Enable USART0 RX interrupt
_BIS_SR(LPM0_bits + GIE);// Enter LPM0 w/ interrupt
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
while (!(IFG1 & UTXIFG0));// USART0 TX buffer ready?
TXBUF0 = RXBUF0;// RXBUF0 to TXBUF0
}
評論