一、初始化该引脚时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB ,ENABLE);
二、初始化该引脚为外部中止
void GPIO_Config_Init(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PB8 dog_wake
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, GPIO_InitStructure);
}
三、初始化外部中止函数
void EXTI_Config_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line8);//
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8);//
EXTI_InitStructure.EXTI_Line=EXTI_Line8; //
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//
EXTI_Init(EXTI_InitStructure);
}
四、初始化中止向量表
void NVIC_Config_Init(void)
{
NVIC_InitStructure.NVIC_IRQChannel =EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//×ÓÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(NVIC_InitStructure);
}
五、编写外部中止函数
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line8) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line8); //
GPIO_SetBits(GPIOB,GPIO_Pin_9);
delay_ms(10);
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
}
}
- STM32单片机中文官网
- STM32单片机官方开发工具
- STM32单片机参阅规划