这次根据下载里面的技术文档进行串口,采用的C语言的库的编程形式,感觉到C语言库带来的方便,再也不用操作具体寄存器了。只需要知道的函数的作用就可以了。SMCLK = 8M 波特率9600 数据位8位,停止位1位,无检验。
void Init_UART()
{
// Configure UART
if ( STATUS_FAIL == EUSCI_A_UART_initAdvance(EUSCI_A0_BASE,
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
52, //波特率9600
1,
0x49,
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION ))
return;
EUSCI_A_UART_enable(EUSCI_A0_BASE);
EUSCI_A_UART_clearInterruptFlag(EUSCI_A0_BASE,
EUSCI_A_UART_RECEIVE_INTERRUPT);
// Enable USCI_A0 RX interrupt
EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE,
EUSCI_A_UART_RECEIVE_INTERRUPT); // Enable interrupt
// Enable globale interrupt
__enable_interrupt();
}
/*
* USCI_A0 Interrupt Service Routine that receives PC GUI's commands
*/
#pragma vector = USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
int i;
switch (__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
i = EUSCI_A_UART_receiveData(EUSCI_A0_BASE);
EUSCI_A_UART_transmitData(EUSCI_A0_BASE,i);
/*if (i == '5')
pingHost = 1;
else
mode = i;
__bic_SR_register_on_exit(LPM3_bits); // Exit active CPU*/
break;
case USCI_UART_UCTXIFG: break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
}
}
以上为中断函数和串口的初使化的函数。
函数代码
uart.rar
(493.7 KB)
(下载次数: 5, 2014-11-7 22:49 上传)