你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 中断相关函数和类型

[复制链接]
STMCU小助手 发布时间:2022-4-1 15:19
01. 概述
. j- r6 b, `( M2 q* l中断相关代码主要分布在固件库的 stm32f4xx_exti.h 和 stm32f4xx_exti.c 文件中。
' r" F+ O% K/ i0 V1 S  f2 \, G# ~: z
02. 相关类型
  a! b1 W+ }! XEXTI模式枚举
+ C2 R6 d) G% h( u

8 a+ g6 o4 ~: H9 p$ K( ^5 F
  1. /** 6 c( g, C) {! J" E+ p
  2.   * @brief  EXTI mode enumeration  
    2 g) \" b+ q% S; ]- y
  3.   */
    " r. t1 m. q% }& A- H2 u

  4. ; \8 n$ p1 n  D( w+ _2 F
  5. typedef enum. N$ O: z' `, |& P, H
  6. {
    : M' v0 ?; `6 I$ p( M' y6 q3 ?
  7.   EXTI_Mode_Interrupt = 0x00,0 B6 l8 a4 g- j$ W) C5 p: A0 \4 P
  8.   EXTI_Mode_Event = 0x04
    , F4 R2 r& B4 F* N+ h1 v
  9. }EXTIMode_TypeDef;+ M9 O) ?* Z# a6 m/ n/ c+ A- f

  10. " f+ \- [5 x: a8 H4 F
  11. #define IS_EXTI_MODE(MODE) (((MODE) == EXTI_Mode_Interrupt) || ((MODE) == EXTI_Mode_Event))
复制代码
' q  ~; l9 r. g6 W' [
EXTI触发枚举
  1. /**
    ! P0 _' B$ J1 O; |/ N! J# V  U
  2.   * @brief  EXTI Trigger enumeration  
    0 g4 \" d' K) p& K1 w& C
  3.   */: g2 S6 b/ l5 K$ H

  4. 4 ^9 b  H  d- i
  5. typedef enum
    ( E8 \) e/ N" k: |+ Z
  6. {
    7 ^! o3 m) U* c+ V
  7.   EXTI_Trigger_Rising = 0x08, //上升沿
    ' F! A+ s/ O" p& Z8 r
  8.   EXTI_Trigger_Falling = 0x0C,  //下降沿
    & R, \; e. ^! b) j( k5 s
  9.   EXTI_Trigger_Rising_Falling = 0x10 //边沿触发4 L8 I, c% X9 L8 Q+ H$ p: `& J7 C
  10. }EXTITrigger_TypeDef;
    & P; m9 s5 z% s, M7 @; g

  11. : ~2 _1 o7 Y: i& N- j/ u' q+ t
  12. #define IS_EXTI_TRIGGER(TRIGGER) (((TRIGGER) == EXTI_Trigger_Rising) || \
    8 a& @) F+ @1 x6 \. I3 [! l
  13.                                   ((TRIGGER) == EXTI_Trigger_Falling) || \, |, q% T  d0 I& f
  14.                                   ((TRIGGER) == EXTI_Trigger_Rising_Falling))
复制代码

' W+ t1 F: D' ~0 h5 e9 eEXTI初始化结构体) T9 o) \; y' g8 x6 y/ H, ?

+ o8 N" |1 b: z$ U' B
  1. /**
    * j$ c+ m9 P0 F* d) C
  2.   * @brief  EXTI Init Structure definition  
    / N: q( f  F( Y3 l
  3.   */5 w0 g: Q5 R8 b1 g1 ~& W# B% a% w# a" Z/ d
  4. 0 r) ^/ L2 L( J" M5 w3 D
  5. typedef struct/ G  I. b3 W& H1 K
  6. {
    & t- R* S) K  u! B2 f! O0 P5 f
  7.   uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.2 Y, K8 p* k/ |  _" h8 L
  8.                                          This parameter can be any combination value of @ref EXTI_Lines */
    0 c0 u+ |. K5 m& H2 n4 {
  9. + n! V# `1 }( m" {$ ?3 _
  10.   EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.# J/ i0 o! M- ?9 I
  11.                                          This parameter can be a value of @ref EXTIMode_TypeDef */
    0 m8 j. y3 r7 P

  12. 4 L$ n) J# c+ N" [
  13.   EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.* x0 ], a- b9 t) Y% e4 i
  14.                                          This parameter can be a value of @ref EXTITrigger_TypeDef */
    " R# s6 u. V! n- [

  15. % n6 T$ a" c! d5 S8 G7 S
  16.   FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.# N- \% ^2 i  H
  17.                                          This parameter can be set either to ENABLE or DISABLE */ ! G, T' v1 Y- {' f$ c" A
  18. }EXTI_InitTypeDef;# N% v$ D/ b( K( w
复制代码
5 W4 f; @5 B& v9 V1 W
外部中断线; a, f. o  K9 H% s

5 c$ n/ l8 z+ }0 N1 b1 s6 J  q
  1. /** @defgroup EXTI_Lines 7 x& I! T9 l! l
  2.   * @{$ n: H- O/ A) v0 e. v' P# z8 v
  3.   *// V& h+ _2 D3 _
  4. 4 D, u- V- X" o) ~3 W& x  H( |& T
  5. #define EXTI_Line0       ((uint32_t)0x00001)     /*!< External interrupt line 0 */3 _! j+ T8 G( g; ^8 U- l
  6. #define EXTI_Line1       ((uint32_t)0x00002)     /*!< External interrupt line 1 */
    ) d. H( `5 P7 T2 \
  7. #define EXTI_Line2       ((uint32_t)0x00004)     /*!< External interrupt line 2 */
    . l- @4 Q# e$ j+ ^7 L( D8 ~. v$ q
  8. #define EXTI_Line3       ((uint32_t)0x00008)     /*!< External interrupt line 3 */& D3 y  Z4 V! u$ r, m7 i: O1 r
  9. #define EXTI_Line4       ((uint32_t)0x00010)     /*!< External interrupt line 4 */7 i1 Q% h( H( ?+ f6 w( w2 N
  10. #define EXTI_Line5       ((uint32_t)0x00020)     /*!< External interrupt line 5 */9 C% m$ E) K% k, {; W& d! C% c
  11. #define EXTI_Line6       ((uint32_t)0x00040)     /*!< External interrupt line 6 */
    - y3 `0 K6 i- `3 |
  12. #define EXTI_Line7       ((uint32_t)0x00080)     /*!< External interrupt line 7 */9 w; ^+ w. m$ W; v$ m/ N- h' k
  13. #define EXTI_Line8       ((uint32_t)0x00100)     /*!< External interrupt line 8 */
    / R% a- O* X$ b) `3 w" g
  14. #define EXTI_Line9       ((uint32_t)0x00200)     /*!< External interrupt line 9 */
    0 A* ~( H+ T8 j1 u# q, i' i
  15. #define EXTI_Line10      ((uint32_t)0x00400)     /*!< External interrupt line 10 */
    , F8 s6 ^- ^5 O" a0 O
  16. #define EXTI_Line11      ((uint32_t)0x00800)     /*!< External interrupt line 11 */
    3 K( w. ]+ X  L& B% g3 q
  17. #define EXTI_Line12      ((uint32_t)0x01000)     /*!< External interrupt line 12 */6 ]5 q( |" Y% \: \0 Q" |! Z: }1 [
  18. #define EXTI_Line13      ((uint32_t)0x02000)     /*!< External interrupt line 13 */
    ( D/ ~% w! n; N- n; h
  19. #define EXTI_Line14      ((uint32_t)0x04000)     /*!< External interrupt line 14 */
    " q' b, u1 z/ F4 w# r& F
  20. #define EXTI_Line15      ((uint32_t)0x08000)     /*!< External interrupt line 15 */! o! u# ]7 i+ _! \. z( W; ~
  21. #define EXTI_Line16      ((uint32_t)0x10000)     /*!< External interrupt line 16 Connected to the PVD Output */
    $ F1 R1 f2 Q# y- C
  22. #define EXTI_Line17      ((uint32_t)0x20000)     /*!< External interrupt line 17 Connected to the RTC Alarm event */- r6 f, R# W& _( v* s0 v
  23. #define EXTI_Line18      ((uint32_t)0x40000)     /*!< External interrupt line 18 Connected to the USB OTG FS Wakeup from suspend event */                                    
    - q: O$ C4 j( B6 t+ ]
  24. #define EXTI_Line19      ((uint32_t)0x80000)     /*!< External interrupt line 19 Connected to the Ethernet Wakeup event */
    % ]( o/ {" x- V1 I9 I1 ]$ F
  25. #define EXTI_Line20      ((uint32_t)0x00100000)  /*!< External interrupt line 20 Connected to the USB OTG HS (configured in FS) Wakeup event  */
    ( ~8 V( r' z+ [" I2 l
  26. #define EXTI_Line21      ((uint32_t)0x00200000)  /*!< External interrupt line 21 Connected to the RTC Tamper and Time Stamp events */                                               
    0 S& S  E( k; f+ a; W2 @
  27. #define EXTI_Line22      ((uint32_t)0x00400000)  /*!< External interrupt line 22 Connected to the RTC Wakeup event */! U( o7 J$ V6 v) }9 X
  28. #define EXTI_Line23      ((uint32_t)0x00800000)  /*!< External interrupt line 23 Connected to the LPTIM Wakeup event */$ i& m( @5 f# W1 g9 X# f
  29. 0 N8 Z7 `! w1 C& ^8 _+ O8 `2 \

  30. 1 H- \( M, s3 t0 d
  31. #define IS_EXTI_LINE(LINE) ((((LINE) & (uint32_t)0xFF800000) == 0x00) && ((LINE) != (uint16_t)0x00))
    ( I) |/ O, V4 d" u' Y- i

  32. & Q5 p/ l% U0 f+ U. d( d
  33. #define IS_GET_EXTI_LINE(LINE) (((LINE) == EXTI_Line0) || ((LINE) == EXTI_Line1) || \
    : I9 z: z9 N5 U8 J# d
  34.                                 ((LINE) == EXTI_Line2) || ((LINE) == EXTI_Line3) || \
    . X+ e( G6 D) t9 o# y
  35.                                 ((LINE) == EXTI_Line4) || ((LINE) == EXTI_Line5) || \
    ) u$ }) O% z* j9 }
  36.                                 ((LINE) == EXTI_Line6) || ((LINE) == EXTI_Line7) || \
    1 ^7 ?% w4 i" M8 R5 f
  37.                                 ((LINE) == EXTI_Line8) || ((LINE) == EXTI_Line9) || \! D* i( ?% P* d! r
  38.                                 ((LINE) == EXTI_Line10) || ((LINE) == EXTI_Line11) || \" J% a' T7 R9 e! l
  39.                                 ((LINE) == EXTI_Line12) || ((LINE) == EXTI_Line13) || \2 p4 H. N1 G8 W7 ?  G
  40.                                 ((LINE) == EXTI_Line14) || ((LINE) == EXTI_Line15) || \
    8 }: V6 i% y# F
  41.                                 ((LINE) == EXTI_Line16) || ((LINE) == EXTI_Line17) || \6 P7 Y+ j: u& k; V
  42.                                 ((LINE) == EXTI_Line18) || ((LINE) == EXTI_Line19) || \0 [6 \) Y. ]: C; P3 l  b5 ]; U; _% X
  43.                                 ((LINE) == EXTI_Line20) || ((LINE) == EXTI_Line21) ||\& r& s3 k+ T4 g# n4 b' V5 t! F
  44.                                 ((LINE) == EXTI_Line22) || ((LINE) == EXTI_Line23))
复制代码
1 Q7 c1 w/ i; A" B% K
03. 相关函数
+ a; Y* V$ g2 I: ~+ H/ e& n: f0 g5 u; K. F& w9 Q
  1. /* Exported macro ------------------------------------------------------------*/
    6 Q2 g/ c( V$ w; `0 q9 f/ G
  2. /* Exported functions --------------------------------------------------------*/
    7 V& L7 |3 A" c2 q  B1 W! T1 H

  3. 0 ]9 n- ]+ W: p) w8 h) w1 g
  4. /*  Function used to set the EXTI configuration to the default reset state *****/* U( n. L$ Z- C# Z  ?
  5. void EXTI_DeInit(void);
    & P# s8 m& A( f: s3 {  b# }' l! y

  6. 1 e1 f6 w" B' P' Y$ }
  7. /* Initialization and Configuration functions *********************************/
    $ L/ \5 ~' w' h! q# U
  8. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);$ }; T2 `" F$ Z& p
  9. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);
    0 P& \& A5 \+ @8 V2 Y
  10. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);
    1 Y; p3 f- l1 U; a0 f
  11. 2 i% F) I2 r5 t
  12. /* Interrupts and flags management functions **********************************/2 \! m1 y  f7 F5 k9 l' l
  13. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);
      y3 |: a+ c4 K
  14. void EXTI_ClearFlag(uint32_t EXTI_Line);$ ]% o7 [" R: B" i. s  G
  15. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);
    + k( ~/ C  B$ w4 ?7 ?2 d
  16. void EXTI_ClearITPendingBit(uint32_t EXTI_Line);  r7 x6 P2 }$ M) c4 k' a
复制代码
& ~/ V3 Y8 }4 ]; [4 `
04. 结构体封装* [7 }1 U& u" p7 D) s6 |! e
外部中断控制器结构体封装
  1. /**
    2 a* |% a" g. d
  2.   * @brief External Interrupt/Event Controller0 r. |: B2 {$ F. y& `( f
  3.   */9 N& Y7 M4 E3 G* Y) q5 I

  4. 0 h+ R  a+ m0 c  R6 v, P+ `5 P7 q
  5. typedef struct/ ]( Q; {: ?- H7 W0 S  v, ]
  6. {  X& N8 c7 [! {, }7 l0 b
  7.   __IO uint32_t IMR;    /*!< EXTI Interrupt mask register,            Address offset: 0x00 */5 t1 F6 b0 i+ U3 g1 f7 M3 w' w: J
  8.   __IO uint32_t EMR;    /*!< EXTI Event mask register,                Address offset: 0x04 */
    . E+ \* ^; ~: _( _
  9.   __IO uint32_t RTSR;   /*!< EXTI Rising trigger selection register,  Address offset: 0x08 */
    8 v  j) i' U4 L; L% D% @
  10.   __IO uint32_t FTSR;   /*!< EXTI Falling trigger selection register, Address offset: 0x0C */
    . y% {3 p# ?+ j
  11.   __IO uint32_t SWIER;  /*!< EXTI Software interrupt event register,  Address offset: 0x10 */7 L" c! f" k: ^
  12.   __IO uint32_t PR;     /*!< EXTI Pending register,                   Address offset: 0x14 */
    ) ^3 Z1 H9 m- k9 B6 Z9 D" T5 m7 W
  13. } EXTI_TypeDef;
复制代码
: ], v3 n; x% ]8 @. [  m
赞 收藏 评论0 发布时间:2022-4-1 15:19

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版