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

2)、开启中断模式 
3)、生成工程代
1 d5 @+ a6 Z2 z' X % f; g. P: [3 s% H7 p( l
/ K! b8 L% Y& k A
在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)
% p# w: E1 k# i* E" J - {3 x4 U+ t0 E; e
- /* USER CODE BEGIN USART1_IRQn 0 */4 B6 }& H& \& L. c: a' t0 u# @
- recv_end_flag = 1;4 T/ x8 I6 X V
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);
- e& L- V- O+ Z3 @$ z - /* USER CODE END USART1_IRQn 0 */
; c5 a+ S' E$ b3 Q) \6 S - HAL_UART_IRQHandler(&huart1);! ?7 r! H: U* s8 P: k4 U; o! Y
- /* 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>
复制代码
, _ a5 h, F3 F0 c# n在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>' X4 d- E' @( B$ c, {
- <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">MX_GPIO_Init();/ [$ k. x% d+ b3 r( W2 c; o0 C% K
- MX_DMA_Init();% z! t7 L$ D& Z; a5 I
- MX_ADC1_Init();
6 B9 L% X7 l1 |: Q o0 } - MX_USART1_UART_Init();$ x, ^: V: q( t! N L! L
- /* USER CODE BEGIN 2 */. e2 x- L1 Z: O* Y4 g$ L( M7 }* l+ T
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
0 k# t0 G1 s# _' p - HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));& S/ m1 n9 i, {- ^1 ^& V `
- /* 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 */
+ P# p. [; t) a4 r - /* USER CODE BEGIN WHILE */3 Q- ?4 a `7 s: B" W
- while (1)
1 D5 } T- w" a& D: X - {</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);
1 I" G* ?( D k- g* Y, k. l( ` - HAL_Delay(200);1 n3 H8 e% d" i: b: b
- 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);) q* M1 i- z( n6 V$ `4 w6 c7 C
- if(recv_end_flag ==1){
7 P! G* a l( q& f
9 V2 B1 G, ^4 e6 `* U- m9 c, ^- }
& A& l' h$ D' o! R+ h/ i - /* 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 */
1 d( X( u E" J3 v, d - }/ D D. o7 V, \5 @1 E
- /* USER CODE END 3 */
, W H1 u/ @5 s8 h3 W! V& | - }</font></font></font></p>
复制代码 6 q4 s; P4 T) [7 G
! t% D% t, t* \, n2 r& J& M |