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

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

[复制链接]
STMCU小助手 发布时间:2022-4-1 15:19
01. 概述5 x8 c8 d% D9 @. Y7 R/ W" O2 w, x. K! m
中断相关代码主要分布在固件库的 stm32f4xx_exti.h 和 stm32f4xx_exti.c 文件中。
/ x$ x4 L+ |" D1 D) ~" l* ^
. `. t4 U1 X% J' g8 B02. 相关类型
# L) Q2 M4 ?, X6 ^$ v% |+ A, {0 C6 u9 \EXTI模式枚举
1 D8 Y1 I8 I6 ~/ K: K  x
* m6 O- J0 S3 ?) Q& Y+ \/ b, v  u
  1. /** % G7 {3 w0 ~. W) m
  2.   * @brief  EXTI mode enumeration  2 I  W) X0 r/ Z+ ?% n# B
  3.   */* F- e, k/ u( J5 y9 D0 l) {4 t
  4. & w1 L) i* q5 I3 R  ^% I" @
  5. typedef enum- ]8 [# i/ M% {4 P2 P3 Y
  6. {
    3 x: w* c: R& ^9 h# i
  7.   EXTI_Mode_Interrupt = 0x00,
    8 r& J0 |: n" r* S
  8.   EXTI_Mode_Event = 0x04( L2 S& L/ m, I, r. M$ ]
  9. }EXTIMode_TypeDef;
    ! e% E$ `' T. D9 e, Z8 d- D

  10. 4 V! F+ q% s6 k
  11. #define IS_EXTI_MODE(MODE) (((MODE) == EXTI_Mode_Interrupt) || ((MODE) == EXTI_Mode_Event))
复制代码

, L$ c8 B2 S2 r; d/ eEXTI触发枚举
  1. /**
    9 J% U, X7 F5 y1 g0 u
  2.   * @brief  EXTI Trigger enumeration  
    - i# `! W) w1 L* ?& p
  3.   */
    6 p* p* I6 f3 b" ?) J5 x# _9 n, {

  4. + v: `% e0 ^- [* s7 M; ]
  5. typedef enum
    $ Y# L/ D5 [& T! d) ]# M0 u" g
  6. {
    - K. f" C8 D- l% v
  7.   EXTI_Trigger_Rising = 0x08, //上升沿
    3 H3 X* L- T$ p4 e) @" v4 D
  8.   EXTI_Trigger_Falling = 0x0C,  //下降沿, Z: b! J4 W, k, i: c
  9.   EXTI_Trigger_Rising_Falling = 0x10 //边沿触发  d8 U# Z! p8 y7 }4 L6 y
  10. }EXTITrigger_TypeDef;8 X: F# \% l5 N4 \

  11. 3 z- S) [$ L- h. z& \6 r+ b
  12. #define IS_EXTI_TRIGGER(TRIGGER) (((TRIGGER) == EXTI_Trigger_Rising) || \
    ' c% W! a$ k$ C' H4 [* H
  13.                                   ((TRIGGER) == EXTI_Trigger_Falling) || \8 R7 U$ `  u# w3 h! u
  14.                                   ((TRIGGER) == EXTI_Trigger_Rising_Falling))
复制代码
! j( p+ i  A  j& \7 A
EXTI初始化结构体
0 |. o- k" h  C5 E8 w# }& _. s, V  o/ A- ]7 I* c
  1. /**   o1 f7 i! m5 Q7 n
  2.   * @brief  EXTI Init Structure definition  
    $ M; f7 Q+ ?& Y& g( k6 {
  3.   */
    1 ]7 b8 i: E+ b! N7 c

  4. 5 I6 i: |) X( E" s( m5 o0 w
  5. typedef struct$ I- g$ g; Z3 ~2 F  D6 x
  6. {
    ! Z3 e$ Q4 b9 K" h: \6 Z
  7.   uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.; [9 \: U. `* h
  8.                                          This parameter can be any combination value of @ref EXTI_Lines */
    # L' T1 g$ S' R8 ]

  9. ' R, h8 e! b4 b+ K: I
  10.   EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.
    % I  M. A$ W+ a7 {1 A# K
  11.                                          This parameter can be a value of @ref EXTIMode_TypeDef */
    8 k9 ~" v2 [/ F
  12. : B% M8 ~# d- S0 T- F; Z& Q
  13.   EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.
    6 ]8 }( r+ \* r% v
  14.                                          This parameter can be a value of @ref EXTITrigger_TypeDef */
    " n7 t) l) A+ J( E3 g+ D; P* J# F

  15. ' }5 g; m/ E5 q
  16.   FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.
    # W* F2 ^, |% L+ J  C
  17.                                          This parameter can be set either to ENABLE or DISABLE */
    * m. }. t7 G. t7 [0 p
  18. }EXTI_InitTypeDef;8 V8 Z3 v" {9 d0 f2 ?3 [
复制代码

! t# O& O: X3 q5 i3 V  V4 ^外部中断线
' c& z; k. M1 ?1 P
  Q$ q9 |9 G- Z9 D
  1. /** @defgroup EXTI_Lines
    % u: R. l! n& a" }5 M
  2.   * @{9 ^! i4 i. a' Q4 Q" a# G
  3.   */
    " L7 d% a# H# f. y! S& q9 a

  4. + \) e6 y8 `9 }- F4 m! L
  5. #define EXTI_Line0       ((uint32_t)0x00001)     /*!< External interrupt line 0 */, w4 }/ A1 b* m( V) Q
  6. #define EXTI_Line1       ((uint32_t)0x00002)     /*!< External interrupt line 1 */
    ' ]/ Z; s2 \: A/ j' y
  7. #define EXTI_Line2       ((uint32_t)0x00004)     /*!< External interrupt line 2 */2 X1 ]' Q0 [" Z  ?- ~! x
  8. #define EXTI_Line3       ((uint32_t)0x00008)     /*!< External interrupt line 3 */
    ' C$ U0 t: T; |: d! ]. u
  9. #define EXTI_Line4       ((uint32_t)0x00010)     /*!< External interrupt line 4 */) d  E2 k9 K) T; Q: c2 o
  10. #define EXTI_Line5       ((uint32_t)0x00020)     /*!< External interrupt line 5 */7 |  k+ y% a/ t) Y
  11. #define EXTI_Line6       ((uint32_t)0x00040)     /*!< External interrupt line 6 */
    ) V4 p( n  B* E  O
  12. #define EXTI_Line7       ((uint32_t)0x00080)     /*!< External interrupt line 7 */
    ( w" S2 b" a/ {
  13. #define EXTI_Line8       ((uint32_t)0x00100)     /*!< External interrupt line 8 */8 q- E$ g3 J' _1 h; F7 g
  14. #define EXTI_Line9       ((uint32_t)0x00200)     /*!< External interrupt line 9 */7 D4 v- U6 R! {3 P5 b# B# P9 e
  15. #define EXTI_Line10      ((uint32_t)0x00400)     /*!< External interrupt line 10 */3 b6 Q! G8 p" J
  16. #define EXTI_Line11      ((uint32_t)0x00800)     /*!< External interrupt line 11 */0 X. ?3 w- ?/ U0 S2 E0 i
  17. #define EXTI_Line12      ((uint32_t)0x01000)     /*!< External interrupt line 12 */
    * j- M! _3 X! z5 p  m
  18. #define EXTI_Line13      ((uint32_t)0x02000)     /*!< External interrupt line 13 */
    " O" D5 B* M+ O/ x5 a
  19. #define EXTI_Line14      ((uint32_t)0x04000)     /*!< External interrupt line 14 */
    . [: |# a& {% |5 u' C  W1 M
  20. #define EXTI_Line15      ((uint32_t)0x08000)     /*!< External interrupt line 15 */+ U6 A6 ]8 O  X: ^: }. ]% O' a1 H
  21. #define EXTI_Line16      ((uint32_t)0x10000)     /*!< External interrupt line 16 Connected to the PVD Output */% P& I& ^/ w, d: B" z! c5 c1 b
  22. #define EXTI_Line17      ((uint32_t)0x20000)     /*!< External interrupt line 17 Connected to the RTC Alarm event */
    ) N8 R  C6 C" k8 U6 i
  23. #define EXTI_Line18      ((uint32_t)0x40000)     /*!< External interrupt line 18 Connected to the USB OTG FS Wakeup from suspend event */                                    
    " n& P5 a2 W0 r$ T/ M5 f
  24. #define EXTI_Line19      ((uint32_t)0x80000)     /*!< External interrupt line 19 Connected to the Ethernet Wakeup event */% B0 O3 C5 d0 x2 b. D% z
  25. #define EXTI_Line20      ((uint32_t)0x00100000)  /*!< External interrupt line 20 Connected to the USB OTG HS (configured in FS) Wakeup event  */* e% \2 k* k) m7 ?9 t2 u
  26. #define EXTI_Line21      ((uint32_t)0x00200000)  /*!< External interrupt line 21 Connected to the RTC Tamper and Time Stamp events */                                               
    * p  U# g5 e- C1 C4 B# J5 A
  27. #define EXTI_Line22      ((uint32_t)0x00400000)  /*!< External interrupt line 22 Connected to the RTC Wakeup event */  W5 I: i& G* ]$ C$ Q
  28. #define EXTI_Line23      ((uint32_t)0x00800000)  /*!< External interrupt line 23 Connected to the LPTIM Wakeup event */
    ! C8 D/ \: K$ g# r' q: Y! X' j
  29. ) E& L9 y: o% A, p& T" p
  30. ; p& l2 I  |; `5 M. |
  31. #define IS_EXTI_LINE(LINE) ((((LINE) & (uint32_t)0xFF800000) == 0x00) && ((LINE) != (uint16_t)0x00))
    0 y, C' m) y2 q; l/ v" B: d

  32. ; P+ l# M0 V- T
  33. #define IS_GET_EXTI_LINE(LINE) (((LINE) == EXTI_Line0) || ((LINE) == EXTI_Line1) || \/ J. o* ]# k" C& n8 w) T
  34.                                 ((LINE) == EXTI_Line2) || ((LINE) == EXTI_Line3) || \' W' @7 k7 q/ d' W) ]& P9 a5 ?- l. Q
  35.                                 ((LINE) == EXTI_Line4) || ((LINE) == EXTI_Line5) || \
    7 ?8 `) E5 _1 V4 |7 R
  36.                                 ((LINE) == EXTI_Line6) || ((LINE) == EXTI_Line7) || \
    & _+ a5 a5 H: q0 _8 {: Q9 Z
  37.                                 ((LINE) == EXTI_Line8) || ((LINE) == EXTI_Line9) || \
    - o! H" }9 X1 \1 r
  38.                                 ((LINE) == EXTI_Line10) || ((LINE) == EXTI_Line11) || \) K9 w6 u- W$ [. Q. c6 R$ v
  39.                                 ((LINE) == EXTI_Line12) || ((LINE) == EXTI_Line13) || \
    7 D1 }  ~9 J/ b" o+ k5 |# ?7 i. l
  40.                                 ((LINE) == EXTI_Line14) || ((LINE) == EXTI_Line15) || \
      ^, s# p3 \% B
  41.                                 ((LINE) == EXTI_Line16) || ((LINE) == EXTI_Line17) || \
    7 N! q& [& f8 M* M8 E3 ]5 {- S: _
  42.                                 ((LINE) == EXTI_Line18) || ((LINE) == EXTI_Line19) || \
    ) @1 A7 s% u6 L$ Z. i7 z4 h
  43.                                 ((LINE) == EXTI_Line20) || ((LINE) == EXTI_Line21) ||\
    0 K' d1 O7 k+ [( o' E
  44.                                 ((LINE) == EXTI_Line22) || ((LINE) == EXTI_Line23))
复制代码

( K3 @+ L$ ~9 @' |: \0 V+ d& o5 Q$ |03. 相关函数
5 F, o$ s% {: t- i6 a3 a% p
$ |# D. C4 E5 Y; ^; W" B. R
  1. /* Exported macro ------------------------------------------------------------*/2 E9 L2 B# Y. \7 W# o* m
  2. /* Exported functions --------------------------------------------------------*/3 m9 E: v9 {% H2 W

  3. & G" d. @6 a- V6 k& Y7 R) h; Y
  4. /*  Function used to set the EXTI configuration to the default reset state *****/( ]; z9 H! t# ~+ J" C* ~! S6 _3 h
  5. void EXTI_DeInit(void);4 K5 k& g4 m: @% f) s" L! @

  6. 6 c  o. F! \7 W& }8 ]8 D
  7. /* Initialization and Configuration functions *********************************/3 `5 ?/ Y. g2 P6 }7 m
  8. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);. W& [8 T& Z2 C7 e6 _, a
  9. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);4 G3 S0 h/ Z5 j, ?
  10. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);5 J, z' s4 }4 I, Z

  11. 0 k( C$ G2 n* A# \( e* V
  12. /* Interrupts and flags management functions **********************************/" R! C" }) D& i0 X+ m5 C
  13. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);
    / s: e" h% p6 X3 G3 }
  14. void EXTI_ClearFlag(uint32_t EXTI_Line);
    / k* b4 Q6 L4 G: ^
  15. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);1 P  t# t1 g! M9 \) A
  16. void EXTI_ClearITPendingBit(uint32_t EXTI_Line);
    : l8 I/ x$ _1 {% y
复制代码

  a  `1 R3 L2 L, G$ `; K04. 结构体封装9 ^# `( p5 @5 P# f( w( l
外部中断控制器结构体封装
  1. /**
    & a$ M6 K* S- n+ [
  2.   * @brief External Interrupt/Event Controller% ?0 V+ v( K  c: X! W
  3.   */
    6 }/ P( L# ?' Y7 F: x

  4. : C" g( [! N0 U- H" Y# ~, p
  5. typedef struct
    4 h' H% l6 f* F, y) K
  6. {9 i3 K6 t& o" ~1 {
  7.   __IO uint32_t IMR;    /*!< EXTI Interrupt mask register,            Address offset: 0x00 */
    % ~4 Q1 V' z: [; l7 t
  8.   __IO uint32_t EMR;    /*!< EXTI Event mask register,                Address offset: 0x04 */
    7 B$ s9 C9 l# s8 q7 w! u- h  J/ w
  9.   __IO uint32_t RTSR;   /*!< EXTI Rising trigger selection register,  Address offset: 0x08 */
    : v0 O( b9 e7 t0 k1 R1 Q. ?
  10.   __IO uint32_t FTSR;   /*!< EXTI Falling trigger selection register, Address offset: 0x0C */6 h+ m$ h& a3 _$ O0 j" g8 i& c
  11.   __IO uint32_t SWIER;  /*!< EXTI Software interrupt event register,  Address offset: 0x10 */& f& c: E& y6 |
  12.   __IO uint32_t PR;     /*!< EXTI Pending register,                   Address offset: 0x14 */
    5 C6 w; [$ h4 k6 a7 P; k1 Z
  13. } EXTI_TypeDef;
复制代码

* J  ]" K- f6 ~
收藏 评论0 发布时间:2022-4-1 15:19

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版