大家来一起找BUG啊!!!!!!!!!!!!!!!!!!!!!!!!!
小白做了好久,做出来了TIM超时中断,但是有很多BUG。BUG1:第一次接收只能接收一个字节。
BUG2:中断中必须有一个printf输出。
代码:先配置好usart1串口和TIM2(中断时间100ms)定时器。
main.h
#define RXBUFFERSIZE 1024
main.c
while(1){
unsigned char IT_Rcv[512];
HAL_UART_Receive_IT(&huart1,IT_Rcv,RXBUFFERSIZE);
printf("Rcv:%s
",IT_Rcv);
HAL_Delay(1000);
}
stm32f4xx_it.c
void TIM2_IRQHandler(void)
{
if(__HAL_TIM_GET_ITSTATUS(&htim2,TIM_IT_UPDATE) != RESET) //是更新中断
{
//添加结束符
*(huart1.pRxBuffPtr+(RXBUFFERSIZE-huart1.RxXferCount)) = 0;
// 重置数据接受缓冲区
CLEAR_BIT(huart1.Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart1.Instance->CR3, USART_CR3_EIE);
huart1.RxState = HAL_UART_STATE_READY;
TIM_GET_CLEAR_IT(&htim2,TIM_IT_UPDATE); //清除TIM2更新中断标志
HAL_TIM_Base_Stop_IT(&htim2); //关闭TIM2
printf("&
");
}
}
void USART1_IRQHandler(void)
{
unsigned char res;
if( huart1.RxState == HAL_UART_STATE_BUSY_RX )
{
res = (uint8_t)(huart1.Instance->DR & (uint8_t)0x00FFU);
if( (RXBUFFERSIZE-huart1.RxXferCount)<RXBUFFERSIZE ) //还可以接收数据
{
__HAL_TIM_SetCounter(&htim2,0); //计数器清空
if( (RXBUFFERSIZE-huart1.RxXferCount)==0 ){
HAL_TIM_Base_Start_IT(&htim2); //使能定时器7
*huart1.pRxBuffPtr++ = res; //记录接收到的值
}else{
*huart1.pRxBuffPtr++ = res; //记录接收到的值
*(huart1.pRxBuffPtr+(RXBUFFERSIZE-huart1.RxXferCount)) = 0; //添加结束符
// 重置数据接受缓冲区
CLEAR_BIT(huart1.Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
CLEAR_BIT(huart1.Instance->CR3, USART_CR3_EIE);
huart1.RxState = HAL_UART_STATE_READY;
printf(&quot; Error
&quot;);
}
}
}
} |