网站做一下301跳转,广东省建设工程交易中心,服务好的丹阳网站建设,网站标题设计在线跟着正点原子学习的HAL库写串口接收程序的时候一直有困惑#xff0c;使用HAL_UART_Receive_IT开启接收中断后#xff0c;为啥处理函数要写在HAL_UART_RxCpltCallback里#xff0c;中断发生的时候是怎么到这个回调函数里去的#xff1f;
void MX_USART1_UART_Init(void)
{h…跟着正点原子学习的HAL库写串口接收程序的时候一直有困惑使用HAL_UART_Receive_IT开启接收中断后为啥处理函数要写在HAL_UART_RxCpltCallback里中断发生的时候是怎么到这个回调函数里去的
void MX_USART1_UART_Init(void)
{huart1.Instance USART1;huart1.Init.BaudRate 115200;huart1.Init.WordLength UART_WORDLENGTH_8B;huart1.Init.StopBits UART_STOPBITS_1;huart1.Init.Parity UART_PARITY_NONE;huart1.Init.Mode UART_MODE_TX_RX;huart1.Init.HwFlowCtl UART_HWCONTROL_NONE;huart1.Init.OverSampling UART_OVERSAMPLING_16;if (HAL_UART_Init(huart1) ! HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 */uint8_t byte;HAL_UART_Receive_IT(huart1, byte, 1); /* 这里开启接收中断*//* USER CODE END USART1_Init 2 */
}接下来我们代码里面一步步看HAL_UART_Receive_IT检查了下串口是不是在等待状态是的话就执行UART_Start_Receive_IT
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{/* Check that a Rx process is not already ongoing */if (huart-RxState HAL_UART_STATE_READY){if ((pData NULL) || (Size 0U)){return HAL_ERROR;}/* 设置接收类型为标准类型 */huart-ReceptionType HAL_UART_RECEPTION_STANDARD;return (UART_Start_Receive_IT(huart, pData, Size));}else{return HAL_BUSY;}
}UART_Start_Receive_IT主要设置了中断寄存器。
HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{huart-pRxBuffPtr pData;huart-RxXferSize Size;huart-RxXferCount Size;huart-ErrorCode HAL_UART_ERROR_NONE;huart-RxState HAL_UART_STATE_BUSY_RX;if (huart-Init.Parity ! UART_PARITY_NONE){/* 启用奇偶校验错误中断 */__HAL_UART_ENABLE_IT(huart, UART_IT_PE);}/* 启用UART错误中断:(帧错误、噪声错误、溢出错误 */__HAL_UART_ENABLE_IT(huart, UART_IT_ERR);/* 启用 UART 数据寄存器非空中断 */__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);return HAL_OK;
}其中这个非空中断就是触发HAL_UART_RxCpltCallback回调函数的伏笔。
开启中断后串口收到数据就会触发外部中断代码在启动时的那个汇编文件里。 然后就跳转到了stm32f4xx_it.c里如果文件里找不到出现这个函数看看是不是Cube配置的时候没打开中断。
__weak void USART1_IRQHandler(void)
{HAL_UART_IRQHandler(huart1);
}HAL_UART_IRQHandler有将近200行主要做了如下事情 1.读取中断标志和控制寄存器 2.没有错误中断的情况下是否设置了非空中断如果是调用UART_Receive_IT 3.如果有错误中断则处理错误中断 4.处理空闲线路检测(上面设置接收模式为标准) 5.处理发送中断在接收完直接处理发送流程
然后UART_Receive_IT又调用了HAL_UART_RxCpltCallback。所以在这个回调函数里写操作过程。 那能不能在USART1_IRQHandler里写呢也是可以的但是回调函数结构上更清晰以前STM32最早的标准库就写在USART1_IRQHandler里。 文章转载自: http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.nkllb.cn.gov.cn.nkllb.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.qftzk.cn.gov.cn.qftzk.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn