使用MSP430F5529单片机串口时,发现数据可以正常发送,但中断始终无法进入。
查询资料了解到:串口初始化时,模块初始化的过程顺序与SWRST的值有关。
SWRST为串口复位软件控制位。一次正确的模块初始化过程应该是:先在SWRST = 1时设置串口,设置完串口后再设置SWRST = 0,最后如需中断,再设置中断使能。
操作寄存器修改SWRST:
UCA0CTL1 |= UCSWRST; /*复位USCI_Ax*/
......
UCA0CTL1 &= ~UCSWRST; /*清除复位位,使能UART*/
使用库函数修改SWRST:
void USCI_A_UART_disable (uint16_t baseAddress)
{
//Set the UCSWRST bit to disable the USCI Module
HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
}
void USCI_A_UART_enable (uint16_t baseAddress)
{
//Reset the UCSWRST bit to enable the USCI Module
HWREG8(baseAddress + OFS_UCAxCTL1) &= ~(UCSWRST);
}
将串口中断使能放在串口初始化之前,会导致无法进入串口中断。此前使用其他单片机,无SWRST位,不曾遇到该问题。