diff --git a/book/04:中断系统/01.md b/book/04:中断系统/01.md index 9b46767..9ebb211 100644 --- a/book/04:中断系统/01.md +++ b/book/04:中断系统/01.md @@ -161,8 +161,6 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){ - - ## 2.2. HAL库的中断封装 1. 设置中断触发条件。如外部中断的边沿触发,定时器更新中断的时间间隔。 @@ -277,14 +275,65 @@ __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) > 仔细分析 HAL_GPIO_EXTI_IRQHandler 函数中还做了什么事情? -## 2.3. 外部中断处理流程 +## 2.3. 外部中断处理流程(p184) + +1. startup_stm32f103c8tx.s 中有缺省的中断处理函数ISR,且全部为 weak 修饰; +2. 如果开启了某个中断的ISR,会在 stm32f1xx_it.c 中生成对应的 ISR 去覆盖 startup_stm32f103c8tx.s 的缺省ISR,例如 EXTI15_10_IRQHandler +3. 一系列的函数调用后,最后会调用一个 weak 修饰的函数,如:HAL_GPIO_EXTI_Callback,这就是用户代码逻辑所在的位置,可以覆盖这个函数。 + +![image-20240917140406801](./img/image-20240917140406801.png) # 3. 外部中断的HAL库定义 ## 3.1. 外部中断的数据类型 +GPIO_InitTypeDef: + +```c +typedef struct +{ + uint32_t Pin; /*!< Specifies the GPIO pins to be configured. + This parameter can be any value of @ref GPIO_pins_define */ + + uint32_t Mode; /*!< Specifies the operating mode for the selected pins. + This parameter can be a value of @ref GPIO_mode_define */ + + uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins. + This parameter can be a value of @ref GPIO_pull_define */ + + uint32_t Speed; /*!< Specifies the speed for the selected pins. + This parameter can be a value of @ref GPIO_speed_define */ +} GPIO_InitTypeDef; +``` + +在main.c 的大约160 行,是外部中断的初始化过程,书上没有说,可以看看: + +```c +/*Configure GPIO pin : BTN_Pin */ + GPIO_InitStruct.Pin = BTN_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(BTN_GPIO_Port, &GPIO_InitStruct); + + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); +``` + +通过注释可以看出来,第一部分是初始化一个结构体;第二部分是设置中断的优先级(第二个参数是抢占优先级;第三个参数是子优先级),以及使能外部中断。 + ## 3.2. 外部中断的接口函数 + + +### 3.2.1. 外部中断通用处理函数 HAL_GPIO_EXTI_IRQHanddler + +![image-20240917140818389](./img/image-20240917140818389.png) + +### 3.2.2. 外部中断回调函数 HAL_GPIO_EXTI_Callback + +![image-20240917140901378](./img/image-20240917140901378.png) + # 4. 任务实现 -## 4.1. 中断方式检测按键 \ No newline at end of file +[实验1](../../实验指导书/实验1.md) \ No newline at end of file diff --git a/book/04:中断系统/img/image-20240917140406801.png b/book/04:中断系统/img/image-20240917140406801.png new file mode 100644 index 0000000..1c25e81 Binary files /dev/null and b/book/04:中断系统/img/image-20240917140406801.png differ diff --git a/book/04:中断系统/img/image-20240917140813636.png b/book/04:中断系统/img/image-20240917140813636.png new file mode 100644 index 0000000..b5a3f6d Binary files /dev/null and b/book/04:中断系统/img/image-20240917140813636.png differ diff --git a/book/04:中断系统/img/image-20240917140818389.png b/book/04:中断系统/img/image-20240917140818389.png new file mode 100644 index 0000000..b5a3f6d Binary files /dev/null and b/book/04:中断系统/img/image-20240917140818389.png differ diff --git a/book/04:中断系统/img/image-20240917140901378.png b/book/04:中断系统/img/image-20240917140901378.png new file mode 100644 index 0000000..1a90dc8 Binary files /dev/null and b/book/04:中断系统/img/image-20240917140901378.png differ