STM32CubeMX—串口空闲中断+DMA接收 1)、串口一配置。 

2)、开启中断模式 
3)、生成工程代8 {% L, s7 z" {: J! J
+ E$ d0 A3 l7 v: S e2 p
' c2 L2 g0 y* ~# \: I6 r
在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)6 d4 D, V' D8 K8 F. n
- {3 J6 s/ [+ B3 [5 x; b6 k
- /* USER CODE BEGIN USART1_IRQn 0 */
6 k1 U2 W3 K# Q - recv_end_flag = 1;. Y0 L) l" p* s$ l
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);( s V& d" v" v6 |9 i
- /* USER CODE END USART1_IRQn 0 */
6 f j3 c+ z) _/ |8 ] - HAL_UART_IRQHandler(&huart1);
5 U6 {4 C& ]7 ~; G; d1 Q - /* 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>
复制代码 - k! ]: Y% @0 P: V9 t
在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>
/ k" M8 Y4 h' ^ s - <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">MX_GPIO_Init();
& ^6 C4 n& u+ K5 W2 L7 P6 R5 p8 ] - MX_DMA_Init();
9 w" P# e8 w" j ` - MX_ADC1_Init();4 i5 _8 J+ @! H/ ? ?0 G
- MX_USART1_UART_Init(); e/ N4 a! M1 N
- /* USER CODE BEGIN 2 */
0 K1 d5 t w1 s5 |1 S - __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);% @4 d- E2 `% k! Q! A
- HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));
: s# }) k* p9 B - /* 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 *// i% k z$ w" X. S3 [5 ?
- /* USER CODE BEGIN WHILE */
$ }5 u0 v ~, K/ y# F/ w+ j1 b - while (1)
/ `9 Z% K1 n4 b" _+ P - {</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);
3 p$ C# a# n! y - HAL_Delay(200);- w: w1 d# v7 S$ A( {% {/ k" |: x
- 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);
+ k% n- S R4 ~ ?( H - if(recv_end_flag ==1){$ h3 Y/ e( z, B6 O
v& b% Y5 T+ |) Q- }
5 u4 p9 u4 { b$ C - /* 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 */: H5 C6 D9 W+ N% Q2 B8 s. D
- }3 |* C$ Y5 g& W* m2 f0 m0 \; z
- /* USER CODE END 3 */
* X# E2 e* d% u; ?8 ? - }</font></font></font></p>
复制代码
9 t: q* f$ \8 P: E* [! D7 {
4 L+ j* v D8 d7 g |