使用stm32 cubx 生成工程后 发现USART1_IRQHandler 中断不能进入。8 T" Y& k. r1 e. k+ ^8 k
2 A# y6 \8 _+ }7 B3 X9 n+ X6 T下面这段代码进不去:
( Z# v3 {- l( p# X# i8 T
9 J7 e, X; ^- `5 y3 t0 I- void USART1_IRQHandler(void)
- r% k' k3 V) r4 G! T; A - {4 G/ j: K$ g6 S/ I% ]6 b1 W, W, n
- /* USER CODE BEGIN USART1_IRQn 0 */
+ N8 c8 Y ~ T2 {; w - # b: ^& I9 {. o. B! w; d9 l4 ~ m; X
- /* USER CODE END USART1_IRQn 0 */
' d9 H, ]0 D6 \" d+ J - HAL_UART_IRQHandler(&huart1);
3 i0 H+ { L2 [1 e/ T' x x/ Z - /* USER CODE BEGIN USART1_IRQn 1 */- L1 g' x) h1 y; j) ~* q
- HAL_UART_Receive_IT(&huart1, recive_buf, 10);0 S8 h/ T4 w5 P
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);2 l! Q/ ]& x6 W) f$ F
- /* USER CODE END USART1_IRQn 1 */
. v5 Z- K8 Q$ j6 i/ ^0 z/ d0 q - }
复制代码
# k. J5 ?7 ] d( @: ?! j于是添加红色部分,能够进入中断,完成数据接收. Z( l# e O1 L A# h
7 i) y( n& a5 ^6 L! m9 K- void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
D& B' [. F$ W9 u8 S5 R1 ] - {- f, n! R1 g6 H6 Z
- GPIO_InitTypeDef GPIO_InitStruct = {0};
, W" H. {$ R0 S - if(uartHandle->Instance==USART1)6 S+ c& s0 H& ]0 Z
- {2 g2 F/ N* H' o' {/ }
- /* USER CODE BEGIN USART1_MspInit 0 */
* W( t; o% W& Q% Z0 F; e) X
g& D! `& I) x0 ^5 E, n0 D0 a- /* USER CODE END USART1_MspInit 0 */
4 e$ [8 b$ D2 g* i - /* USART1 clock enable */6 c5 r2 h6 q. {
- __HAL_RCC_USART1_CLK_ENABLE();
- n; s- V3 a: c
8 ? T6 Y4 `* [" e) ^* X- __HAL_RCC_GPIOA_CLK_ENABLE();8 X5 d0 A: y# c0 V i
- /**USART1 GPIO Configuration * o# D" [. M# F( @6 q
- PA9 ------> USART1_TX
( m) P- L8 D: K" K" b( ^& H - PA10 ------> USART1_RX
3 ~' e! ^8 f T+ Q0 X! ~ - */ o6 x3 m' L+ p0 v9 X7 [4 y
- GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
* |6 J. c b" k9 r - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;+ [. P7 M7 M- _/ Q
- GPIO_InitStruct.Pull = GPIO_NOPULL;
/ c$ x# U& q4 j# d - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;* a+ G9 X& @4 e1 v
- GPIO_InitStruct.Alternate = GPIO_AF4_USART1; n! ]) a/ q# V& a
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);3 S- Q) I+ j& {( x
- ! U" e& b- `2 \5 f4 g, {0 F6 v
- /* USART1 interrupt Init */
; ?: y& a; v& Z! ]7 w" o' ?5 o - HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);" Y' {7 h$ Z9 q: I
- HAL_NVIC_EnableIRQ(USART1_IRQn);4 H* {# \/ D( }8 q6 |
- // SET_BIT(USART1->ICR, USART_ICR_TCCF); /* ȥԽTCע̍ΪԉҪ־ */5 L! [4 r' m! {
- // SET_BIT(USART1->RQR, USART_RQR_RXFRQ); /* ȥԽRXNEޓ˕Ҫ־ */2 v1 W' d, {; ^7 Z9 N/ f3 g
- // SET_BIT(USART1->CR1, USART_CR1_RXNEIE); /* ʹŜPE. RXޓ˜א */9 M5 A1 B9 }' x/ `
- /* USER CODE BEGIN USART1_MspInit 1 */) r0 s6 O. E5 a! |7 F3 N
- I Y; {0 d/ w8 W J" }* Y, H- /* USER CODE END USART1_MspInit 1 */9 A L6 j1 Z1 i5 `" w1 B" L2 g; b
- }
/ \2 X/ C: N3 {# R - else if(uartHandle->Instance==USART2)# A2 ~4 M6 h2 X2 p4 O$ t
- {
. n& F7 x7 f0 M6 b - /* USER CODE BEGIN USART2_MspInit 0 */
9 z, m( F5 |9 t' m/ j& s+ ?% G: |
- b) F- b% h+ ]$ w- v7 x- /* USER CODE END USART2_MspInit 0 */5 F- x& w2 X- N) |' F' P
- /* USART2 clock enable */5 }" ?& T+ x `7 G' z6 ^0 n- Z
- __HAL_RCC_USART2_CLK_ENABLE();
1 ], V2 H+ y" l: k5 c, v5 R
, y+ q2 F3 T/ y" |- __HAL_RCC_GPIOA_CLK_ENABLE();4 ]- n9 ~& J# m, P7 d v* C% W. c
- /**USART2 GPIO Configuration ; U9 S' n9 i: V m* }2 R! o
- PA2 ------> USART2_TX
6 {% J. s& k) r4 l& N - PA3 ------> USART2_RX
8 l: G9 p) X' _4 H7 R - */
7 _' P6 J/ ~" M" q! ?; d1 l- }+ \ - GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
% z9 U ^9 Y. c4 M& X2 V- ? - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;( o: z% q' i% N! M/ s. u/ M' G/ Y
- GPIO_InitStruct.Pull = GPIO_NOPULL;
7 b3 F6 ?6 ?" {& a/ a i: i- u - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;5 N ^. h) \0 d- T/ T
- GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
) \% `3 ]2 A) S/ P0 ]' V - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
i" |* _+ n) `) U5 ]; r - 4 a0 e, H+ }' T9 i0 V) [- Z. b6 H8 E
- /* USART2 interrupt Init */4 j& g- o: C0 j3 }% e2 X( G5 r
- HAL_NVIC_SetPriority(USART2_IRQn, 3, 0);" [+ _' p: x3 j/ r8 n% `
- HAL_NVIC_EnableIRQ(USART2_IRQn);
6 t) @) T9 ?* J5 q - /* USER CODE BEGIN USART2_MspInit 1 */
3 {; y( _% p; S" f% |! n& n - * W) t4 N- \* T0 D
- SET_BIT(USART2->ICR, USART_ICR_TCCF); /* 清除TC发送完成标志*/
4 ^1 H/ P5 C% {. J& r1 r - SET_BIT(USART2->RQR, USART_RQR_RXFRQ); /*־清除RXNE接收标志*/
6 ~% D3 S7 V. [' z5 l+ ~ - SET_BIT(USART2->CR1, USART_CR1_RXNEIE); /* 使能PERX接收中断*/
3 A' Y; w2 N" ?, J: Z3 r6 O
, R5 ]9 i* A( J. b- /* USER CODE END USART2_MspInit 1 */2 R- C3 g8 N* N. T3 v# q# v
- }
9 ?! X6 m z5 \/ @+ I) W2 Z - }
复制代码
# {+ |" z0 m$ E2 L, v2 M
+ ` ^: W) ~& Q1 K) B5 b5 v6 Y' | X. O
4 z9 _- y2 a* k* w |