STM32CubeMX—串口空闲中断+DMA接收 1)、串口一配置。 2)、开启中断模式 3)、生成工程代
. @6 W9 ~, }2 [4 s# Y& | + J" Q& ^! h5 b; t/ ^6 @ O8 v
3 K4 [* u- R6 c3 B+ u. Q在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)$ W3 w1 i/ G" O3 o8 I9 G
- {
9 V; y; n2 b; R% y8 _ ^% g" H - /* USER CODE BEGIN USART1_IRQn 0 */+ z0 J. ]1 O* K) {$ V0 N1 d0 E
- recv_end_flag = 1;) M9 ]4 I" F) ]* T! d
- __HAL_UART_CLEAR_IDLEFLAG(&huart1);
0 u+ `3 k" s% |0 @) n - /* USER CODE END USART1_IRQn 0 */: t# Z2 U7 G; F2 C I
- HAL_UART_IRQHandler(&huart1);
8 g: _! m/ L# Z1 } - /* 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>
复制代码
# n! |8 \, V4 s在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();
0 c" S- ~+ h# k$ J) w$ V# K - MX_DMA_Init();
; i5 Y- y/ V. N - MX_ADC1_Init();' w' ~' |8 f2 f+ G7 D
- MX_USART1_UART_Init();* i$ s G5 {6 F0 |1 T* n
- /* USER CODE BEGIN 2 */
; ~3 Z, J) D; B: o - __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
* ~0 Q$ g' W% z; |! i! c9 E ?$ j - HAL_UART_Receive_DMA(&huart1,rx_buffer,sizeof(rx_buffer));7 b& X3 [2 u7 a# I/ L$ O
- /* 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 */
v; | S/ y' M9 i% U! ^9 S - /* USER CODE BEGIN WHILE */
% c7 n. N" [0 J9 I. P) |" i" i - while (1)
( e2 j- L- X) T, S - {</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);* N- \9 }! w8 z* L
- HAL_Delay(200);2 |( ?2 E; y& ?; y2 l, O/ o
- 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);
: j8 k$ l1 U$ S) z - if(recv_end_flag ==1){
# m. Y9 {+ L- x9 ^0 v - 8 V0 W: |: e4 f, K9 Z+ O
- }
, z1 y, `2 X7 p8 g" f* @ - /* 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 */
# V$ {+ Z) ?, j D l% f6 {% J# u - }$ v, j& n/ p. C( r) B
- /* USER CODE END 3 */3 J& _, }* b3 `
- }</font></font></font></p>
复制代码 ) a! f9 [' C7 {' V5 f4 {# F
|