KE 驱动库中的UART 中断问题
0赞UART接收中断是最常用的功能之一,在KE的驱动库里(下载地址:http://www.nxp.com/webapp/sps/download/license.jsp?colCode=KEXX_DRIVERS_V1.2.1_DEVD&location=null&Parent_nodeId=1370723260812713573371&Parent_pageType=product )可以看到有 UART_Interrupt_demo的例程,跑了一下,发现串口可以打印出信息,但是在串口调试助手中输入数据时,却没有任何反应,这是咋回事?
仔细看了一下代码,才发现原因是该例程只是实现了串口中断发送的功能,并没有实现中断接收的功能。
要想使用串口接收功能,改动如下:
1)在main函数里打开接收中断功能使能
UART_EnableRxBuffFullInt(UART1); /* enable rx interrupt */
2)修改UART1 回调函数UART_HandleInt()
void UART_HandleInt(UART_Type *pUART)
{
uint8_t u8Port;
uint8_t *pRdBuff;
uint8_t *pWrBuff;
volatile uint8_t read_temp = 0;
u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
/* check overrun flag */
if(UART_CheckFlag(pUART,UART_FlagOR))
{
read_temp = UART_ReadDataReg(pUART);
}
/* check receiver */
else if(UART_IsRxBuffFull(pUART))
{
#if 0
/* there's data received in rx buffer */
pRdBuff = pUART_RxBuff[u8Port]; /* get user read buffer */
read_temp = UART_ReadDataReg(pUART);
pRdBuff[gu16UART_RxBuffPos[u8Port]++]= read_temp; /* save received data */
//pRdBuff[gu16UART_RxBuffPos[u8Port]++] = UART_ReadDataReg(pUART);
if(gu16UART_RxBuffPos[u8Port] == gu32UART_BuffSize[u8Port])
{
/* User read buffer is full, end receive */
UART_DisableRxBuffFullInt(pUART);
if (UART_RxDoneCallback[u8Port])
{
/* call back function to tell user that rx is done */
UART_RxDoneCallback[u8Port]();
}
}
#endif
// wenxue 2015-12-25
read_temp = UART_ReadDataReg(pUART); /* read uart1 data */
UART_WriteDataReg(pUART, read_temp); /* send back uart1 data */
}
/* check transmitter */
else if(UART_IsTxBuffEmpty(pUART))
{
if(gu16UART_TxBuffPos[u8Port] != gu32UART_BuffSize[u8Port])
{
/* user tx buffer not empty */
pWrBuff = pUART_TxBuff[u8Port];
UART_WriteDataReg(pUART, pWrBuff[gu16UART_TxBuffPos[u8Port]++]);
}
else
{
UART_DisableTxBuffEmptyInt(pUART);
if (UART_TxDoneCallback[u8Port])
{
/* call back function to tell user that tx is done */
UART_TxDoneCallback[u8Port]();
}
}
}
else
{
/* default interrupt */
}
}经过以上两步修改之后,就可以实现串口接收中断的功能了,在串口调试中输入任何字符,都会回显相同的字符。
补充说明:
UART_HandleInt() 函数原来的功能是将串口接收到的数据存放到pUART_RxBuff[u8Port] 缓冲区中,当大小达到gu32UART_BuffSize[u8Port]时,关闭串口接收中断,然后判断是否需要调用UART_RxDoneCallback()函数。我在这里是将其功能变简单了,这样更方便理解。
当然不改动UART_HandleInt() 函数也可以,那么需要在main函数里调用UART_ReceiveInt()函数,完成相关变量的初始化工作。
说到调用UART_ReceiveInt()函数,就得提到另外一点,UART_SendInt()函数和UART_ReceiveInt()函数里都会对全局变量gu32UART_BuffSize 进行初始化,当在程序同时调用这两个函数时,使用时需要注意。
