exti.c文件:
#include "exti.h"
#include "stm32f10x.h"
void exti_init(void)
{
//定义结构体
GPIO_InitTypeDef shake_init;
EXTI_InitTypeDef exti_init;
NVIC_InitTypeDef nvic_init;
//1.配置GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);
GPIO_EXTILineConfig( GPIO_PortSourceGPIOA , GPIO_PinSource2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
shake_init.GPIO_Mode=GPIO_Mode_IPD;
shake_init.GPIO_Pin=GPIO_Pin_2;
shake_init.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &shake_init);
//配置EXTI外部中断
exti_init.EXTI_Line = EXTI_Line2;
exti_init.EXTI_LineCmd = ENABLE;
exti_init.EXTI_Mode = EXTI_Mode_Interrupt;
exti_init.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&exti_init);
//配置NVIC中断控制器
nvic_init.NVIC_IRQChannel = EXTI2_IRQn;
nvic_init.NVIC_IRQChannelCmd = ENABLE;
nvic_init.NVIC_IRQChannelPreemptionPriority = 1;
nvic_init.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&nvic_init);
}
main.c文件:
#include "relay.h"
#include "shake.h"
#include "exti.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=12000;
while(i--);
}
}
int main()
{
Relay_Init();
Shake_Init();
exti_init();
GPIO_SetBits(GPIOA , GPIO_Pin_3); //关闭继电器
while(1)
{
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) !=RESET) //判断是否发生中断
{
GPIO_ResetBits(GPIOA , GPIO_Pin_3); //打开继电器
delay(1000);
GPIO_SetBits(GPIOA , GPIO_Pin_3); //关闭继电器
EXTI_ClearITPendingBit(EXTI_Line2); //清除中断
}
}