本人用串口1和串口4分别进行中断的接受,
在初始化之后,首先在main函数中
调用:
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //添加开启接收中断的语句:
HAL_UART_Receive_IT(&huart4, (uint8_t *)&bRxBuffer, 1);
aRxBuffer
bRxBuffer
为全局变量,
callbak函数为;
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- if (huart->Instance == USART1) //如果是串口1
- {
- if (USART1_Rx_Cnt >= 255) //溢出判断
- {
- USART1_Rx_Cnt = 0;
- memset(USART1_RxBuff, 0x00, sizeof(USART1_RxBuff));
- //可以做一些命令
- RS485PA8_TX;
- HAL_UART_Transmit(&huart1, (uint8_t *)&cAlarmStr, sizeof(cAlarmStr), 0xFFFF);
- RS485PA8_RX;
- }
- else
- {
- USART1_RxBuff[USART1_Rx_Cnt++] = aRxBuffer; //接收数据转存
- //判断结束符,LF=0x0a, 0x0d=CR,意为回车换行,后期可修改
- if ((USART1_RxBuff[USART1_Rx_Cnt - 1] == 0x0A) && (USART1_RxBuff[USART1_Rx_Cnt - 2] == 0x0D)) //判断结束位
- {
- //can do some action eg: send USART1_RxBuff using freertos's queue
- //UART1_CallBack(UART4_RxBuff, ctn);
- RS485PA8_TX;
- HAL_UART_Transmit(&huart1, (uint8_t *)&USART1_RxBuff, USART1_Rx_Cnt, 0xFFFF); //将收到的信息发送出去
- RS485PA8_RX;
- USART1_Rx_Cnt = 0;
- memset(USART1_RxBuff, 0, sizeof(USART1_RxBuff)); //清空数组
- }
- }
- HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //再开启接收中断
- }
- else if (huart->Instance == UART4) //如果是串口4
- {
- if (UART4_Rx_Cnt >= 255) //溢出判断
- {
- UART4_Rx_Cnt = 0;
- memset(UART4_RxBuff, 0x00, sizeof(UART4_RxBuff));
- //可以做一些命令
- RS485PC9_TX;
- HAL_UART_Transmit(&huart4, (uint8_t *)&cAlarmStr, sizeof(cAlarmStr), 0xFFFF);
- RS485PC9_RX;
- }
- else
- {
- UART4_RxBuff[UART4_Rx_Cnt++] = bRxBuffer; //接收数据转存
- //判断结束符,LF=0x0a, 0x0d=CR,意为回车换行,后期可修改
- if ((UART4_RxBuff[UART4_Rx_Cnt - 1] == 0x0A) && (UART4_RxBuff[UART4_Rx_Cnt - 2] == 0x0D)) //判断结束位
- {
- //can do some action eg: send UART4_RxBuff using freertos's queue
- //UART1_CallBack(UART4_RxBuff, ctn);
- RS485PC9_TX;
- HAL_UART_Transmit(&huart4, (uint8_t *)&UART4_RxBuff, UART4_Rx_Cnt, 0xFFFF); //将收到的信息发送出去
- RS485PC9_RX;
- UART4_Rx_Cnt = 0;
- memset(UART4_RxBuff, 0, sizeof(UART4_RxBuff)); //清空数组
- }
- }
- HAL_UART_Receive_IT(&huart4, (uint8_t *)&bRxBuffer, 1); //再开启接收中断
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
我在串口助手中发串口1,正常工作,但是硬件练到串口4中,就无法进中断,
请问这是为什么,
那位大侠指点下。谢谢
|
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
已经解决了,谢谢啊,是因硬件上面的问题
嗯嗯
你说的对
那要怎么改比较好??能够降低进中断的频率??
那就是用空闲中断比较好了??