stm32串口 环形缓冲区 代码
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// printf("IT IN\r\n");
// printf("%d \r\n",HAL_GetTick());
//置零设定电流值 PID时间
if (huart->Instance == USART3)
{
// 将数据放入缓冲区
circular_buffer.buffer[circular_buffer.head] = rxBuffer[0];
circular_buffer.head = (circular_buffer.head + 1) % BUFFER_SIZE;
// 如果缓冲区满,则丢弃最早的数据
if (circular_buffer.head == circular_buffer.tail) {
circular_buffer.tail = (circular_buffer.tail + 1) % BUFFER_SIZE;
}
}
}
每接受一个字节的数据都触发一次中断
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
/* USER CODE END USART3_IRQn 0 */
HAL_UART_IRQHandler(&huart3);
/* USER CODE BEGIN USART3_IRQn 1 */
HAL_UART_Receive_IT(&huart3, (uint8_t *)&rxBuffer,1); //添加的一行代码
/* USER CODE END USART3_IRQn 1 */
}