STM32CubeMX—串口空闲中断+DMA接收 1)、串口一配置。
2)、开启中断模式
3)、生成工程代
在stm32g0xx_it.c中添 - <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">uint8_t recv_end_flag=0;</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">void USART1_IRQHandler(void)
- {
- /* USER CODE BEGIN USART1_IRQn 0 */
- recv_end_flag = 1;
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);
- /* USER CODE END USART1_IRQn 0 */
- HAL_UART_IRQHandler(&huart1);
- /* USER CODE BEGIN USART1_IRQn 1 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px"> /* USER CODE END USART1_IRQn 1 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">}</font></font></font></p>
复制代码
在main.c中加上 - <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">#include"stm32g0xx_it.h"</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">extern uint8_t recv_end_flag;</font></font></font></p>
- <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
- HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));
- /* USER CODE END 2 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px"> /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px"> HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
- HAL_Delay(200);
- HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px"> HAL_Delay(200);
- if(recv_end_flag ==1){
- }
- /* USER CODE END WHILE */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px"> /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }</font></font></font></p>
复制代码
|
|