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

2)、开启中断模式 
3)、生成工程代
' A7 [* F- W% A, O6 ~6 q! N
0 r Q2 _ o+ D; L: b7 W6 [
" S: P% r3 N: L2 n0 Y2 \$ 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): k S x% B: z: O- [% V
- {' [4 N( `7 @' R' A( B) U5 B
- /* USER CODE BEGIN USART1_IRQn 0 */
7 b* F, M* s+ @% ]4 _+ m - recv_end_flag = 1;/ D# h+ p& L: M3 x7 ^
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);
* C/ A, S2 `: T; E+ } - /* USER CODE END USART1_IRQn 0 */6 G% a6 g! k$ t6 a* @0 R
- HAL_UART_IRQHandler(&huart1);" S K4 K; T) m; V' o; v; w, {
- /* 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>
复制代码 , ?: s8 g$ o" A- \5 Z2 g7 h' q) D6 M, M
在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>2 I* x: Y$ ]0 L/ W: Y5 _1 f# x! b6 G
- <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="-apple-system, " "=""><font style="font-size: 16px">MX_GPIO_Init();
" M6 `. X4 ]9 y$ a1 S - MX_DMA_Init();
! z" c( b/ \- u8 ` X - MX_ADC1_Init();6 ^1 i. K# O( B0 [" M
- MX_USART1_UART_Init();
% X% c& v/ G/ L+ Y- \+ X; ] - /* USER CODE BEGIN 2 */
5 F2 o$ A* t9 T/ T% x0 M2 L - __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);! O3 c9 W+ z" s5 S0 h0 _
- HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));
6 b ^* M7 p6 F4 x9 q - /* 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 */
. Y$ G1 f* i, N% D$ f# v# K9 P - /* USER CODE BEGIN WHILE */
' a) C2 J' g( b# J S0 m @ - while (1)( o0 f/ Y3 l/ 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);- p2 T) m. k/ y
- HAL_Delay(200);
6 f9 p6 ^' b! `, W7 S% w - 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);( J2 y) L3 s: t! |+ {+ u) H
- if(recv_end_flag ==1){
3 {& l) F% @# { - 0 \+ K6 M/ S0 U3 O* P3 S6 V
- }( {! f5 D3 d7 G+ x0 T0 v
- /* 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 */
# y- b2 V" j0 e1 o - }, y. W$ r1 P, E4 j5 b" C
- /* USER CODE END 3 */
1 H3 Q; F& f* ]# L$ M4 q. @ - }</font></font></font></p>
复制代码 2 Q- C% C& H7 @+ a
7 C6 V4 Y* o( @$ u |