电路板详细信息:NUCLEO64 - G071RBTX
我试图了解使用 USART2 实现 Modbus RTU 的接收超时中断。首先,要检查接收超时中断,我想检查 2 秒超时中断。所以在下面相应地配置了 RTOR 寄存器(如果配置错误请指导)
===
USART2 配置:
波特率=19200
默认所有其他位
===
我在代码生成 (HAL) 之后额外使用的步骤 下面的步骤是在 HAL 配置 USART2 之后编写的。
- 启用 RXNEIE 中断
- 启用 RTOIE 中断
- 启用接收超时(USART2->RTOEN 位已设置)
- 配置值为 3840002 的 RTOR 寄存器(查看 2 秒后的中断)
RTOR 的计算结果 = (2000000/ 0.520833)。我参考了
这里的计算
- static void MX_USART2_UART_Init(void)
- {
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 19200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
- //By default Receive interrupt is not enabled
- //Enable Receive interrupt and receive timeout of USART2
- USART2->CR1 |=(1<<26U) | (1<<5U);
- USART2->CR2 |=(1<<23U); //RECEIVER TIMEOUT ENABLE
- USART2->RTOR=3840002u;
- /* USER CODE END USART2_Init 2 */
- }
最后在下面的 USART2 IRQ 处理程序中是代码:
IRQ 处理程序中还有一件事,我检查了每一位。这是正确的方法吗?
- void USART2_IRQHandler(void)
- {
- /* USER CODE BEGIN USART2_IRQn 0 */
- if((USART2->ISR && 32u))
- {
- unsigned char test=USART2->RDR;
- //USART2->ICR |=(1<<11u); //CLEAR RTOF BIT
- }
- else if ((USART2->ISR && 2048u))
- {
- //ENTERED RTOF
- unsigned char tester;
- tester=0u;
- USART2->ICR |=(1<<11u); //CLEAR RTOF BIT
- }
- /* USER CODE END USART2_IRQn 0 */
- HAL_UART_IRQHandler(&huart2);
- /* USER CODE BEGIN USART2_IRQn 1 */
- /* USER CODE END USART2_IRQn 1 */
- }
大约 2 秒后不会发生中断。请指导我。