EXTI:STM32支持所有IO端口触发外部中断,但是每个端口只能有一个PIn(如PA1和PB1..PN1只允许一个)
注意:EXTI不需要开启时钟
1.开启GPIO时钟,AFIO时钟: 前者配置IO口模式,后者完成EXTI引脚功能到普通引脚的映射.
/**
* @brief Enables or disables the High Speed APB (APB2) peripheral clock.
* @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
* RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
* RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
* RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
* RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
* RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
* RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
........
}
开启 GPIO AFIO 时钟
- 将EXTI引脚映射至GPIO引脚--------- 在 gpio函数库中
EXTI 引脚映射 : void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
/**
* @brief Selects the GPIO pin used as EXTI Line.
* @param GPIO_PortSource: selects the GPIO port to be used as source for EXTI lines.
* This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
* @param GPIO_PinSource: specifies the EXTI line to be configured.
* This parameter can be GPIO_PinSourcex where x can be (0..15).
* @retval None
*/
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
{
.................
}
将 EXTI引脚 映射到 GPIOx端口的某个引脚
2.EXTI函数库讲解:在固件库—— exti 函数库中
- 初始化函数: void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
/**
* @brief Initializes the EXTI peripheral according to the specified
* parameters in the EXTI_InitStruct.
* @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
* that contains the configuration information for the EXTI peripheral.
* @retval None
*/
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
{
.............
}
EXTI_InitStruct.EXTI_Line : 选择外部中断引脚,必须与映射引脚号相同
EXTI_InitStruct.EXTI_LineCmd : 是否使能引脚状态
EXTI_InitStruct.EXTI_Mode : 选择中断的模式
EXTI_InitStruct.EXTI_Trigger : 选择中断触发的方式
- 初始化配置函数: void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
/**
* @brief Fills each EXTI_InitStruct member with its reset value.
* @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
* be initialized.
* @retval None
*/
void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
{
.....
}
将EXTI初始化配置 :
- 清除初始化函数: void EXTI_DeInit(void)
/**
* @brief Deinitializes the EXTI peripheral registers to their default reset values.
* @param None
* @retval None
*/
void EXTI_DeInit(void)
{
........
}
清除初始化
- 软件触发中断: void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
/**
* @brief Generates a Software interrupt.
* @param EXTI_Line: specifies the EXTI lines to be enabled or disabled.
* This parameter can be any combination of EXTI_Linex where x can be (0..19).
* @retval None
*/
void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
{
/* Check the parameters */
assert_param(IS_EXTI_LINE(EXTI_Line));
EXTI->SWIER |= EXTI_Line;
}
软件触发一次中断
- 标志位函数1: FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
/**
* @brief Checks whether the specified EXTI line flag is set or not.
* @param EXTI_Line: specifies the EXTI line flag to check.
* This parameter can be:
* @arg EXTI_Linex: External interrupt line x where x(0..19)
* @retval The new state of EXTI_Line (SET or RESET).
*/
FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
{
.........
}
查看 所选通道的挂起标志位是否被 置1
此函数一般用作中断外
- 标志位函数2: void EXTI_ClearFlag(uint32_t EXTI_Line)
/**
* @brief Clears the EXTI's line pending flags.
* @param EXTI_Line: specifies the EXTI lines flags to clear.
* This parameter can be any combination of EXTI_Linex where x can be (0..19).
* @retval None
*/
void EXTI_ClearFlag(uint32_t EXTI_Line)
{
......
}
清除 挂起 标志位
此函数一般用作中断外
- 标志位函数3: ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
/**
* @brief Checks whether the specified EXTI line is asserted or not.
* @param EXTI_Line: specifies the EXTI line to check.
* This parameter can be:
* @arg EXTI_Linex: External interrupt line x where x(0..19)
* @retval The new state of EXTI_Line (SET or RESET).
*/
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
{
........
}
读取中断标志位是否被 置1
此函数一般中断内调用
- 标志位函数4: void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
/**
* @brief Clears the EXTI's line pending bits.
* @param EXTI_Line: specifies the EXTI lines to clear.
* This parameter can be any combination of EXTI_Linex where x can be (0..19).
* @retval None
*/
void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
{
.......
}
清除中断标志位
此函数一般中断内调用
中断函数:STM32中中断函数是固定--------在 startup_stm32f10x_hd.s文件中(启动文件)
- EXTI的中断函数有:
EXTI0_IRQHandler
EXTI1_IRQHandler
EXTI2_IRQHandler
EXTI3_IRQHandler
EXTI4_IRQHandler
EXTI9_5_IRQHandler
EXTI15_10_IRQHandler