STM32CubeMX—串口空闲中断+DMA接收 1)、串口一配置。 2)、开启中断模式 3)、生成工程代
( C: f, i' O5 o9 p- w 6 R2 }, c5 p; A( |# Y" K
: _/ v6 O. ~3 c. i/ v
在stm32g0xx_it.c中添 - <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">uint8_t recv_end_flag=0;</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">void USART1_IRQHandler(void)! [) u, \. t. t% B, F* S9 X
- {
0 S- J: W$ F+ N: v2 }! a. h# V - /* USER CODE BEGIN USART1_IRQn 0 */
! n# I% A' I' b - recv_end_flag = 1;
! @' @8 k% C% z; Y! L) q& O9 q - __HAL_UART_CLEAR_IDLEFLAG(&huart1);
. @1 y$ n* O Y, t' c- K - /* USER CODE END USART1_IRQn 0 */
: K7 W# }) v# f. M. L1 \ - HAL_UART_IRQHandler(&huart1);
" A$ b8 P8 s$ ^! P5 s/ @, h - /* USER CODE BEGIN USART1_IRQn 1 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3"> /* USER CODE END USART1_IRQn 1 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">}</font></font></font></p>
复制代码 % t0 ~! \) K, v- @5 J% E; p' g
在main.c中加上 - <p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">#include"stm32g0xx_it.h"</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">extern uint8_t recv_end_flag;</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3"> </font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3">MX_GPIO_Init();& l9 o( N0 u; T! L" x5 ?
- MX_DMA_Init();7 Y2 z+ S9 v) m# W6 \
- MX_ADC1_Init();- ?% o3 e8 J2 x% j% O6 @7 G
- MX_USART1_UART_Init();
* W: N; ^7 K+ R4 z4 V! G. P5 Z - /* USER CODE BEGIN 2 */$ ^! h$ a, v- e1 Z# m3 [
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);! J* h+ k. b0 {- a: c
- HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));
9 i+ H9 \9 f; `2 q - /* USER CODE END 2 */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3"> /* Infinite loop */% t n e4 [" O
- /* USER CODE BEGIN WHILE */; S6 ]( `. ^, U3 V
- while (1)
; e) m( H3 N+ F5 r ?2 { - {</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3"> HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);" y' C$ }( j$ n# C
- HAL_Delay(200);. X# H3 u7 A, r; \4 q
- 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="Tahoma"><font size="3"> HAL_Delay(200); U" v, _! F/ x* [9 ^0 U& T Y3 Y
- if(recv_end_flag ==1){* Q" A7 a! z1 l1 ]( c4 W
+ f" r# z' L/ d7 x' K) J, c- }1 X) `; S% a! f6 E- o
- /* USER CODE END WHILE */</font></font></font></p><p style="line-height: 26px;"><font color="rgb(77, 77, 77)"><font face="Tahoma"><font size="3"> /* USER CODE BEGIN 3 */3 o. e. R/ L9 x9 ^8 r" i! ]# @- S
- }/ T6 L5 \7 V" s# Y0 _1 y
- /* USER CODE END 3 */4 D+ U G5 Q! D! W, l9 {
- }</font></font></font></p>
复制代码
/ ~6 z& v0 W9 ~# F, H |