微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 外部按键中断

外部按键中断

时间:11-27 来源:互联网 点击:
#include "stm32f10x.h"

unsigned int TimingDelay;

void Delay(unsigned int x)

{

TimingDelay=x;

while(TimingDelay--);

}

void EXTI9_5_IRQHandler(void) //中断处理函数

{

unsigned int tt;

tt=6553500;

if(EXTI_GetITStatus(EXTI_Line5)!=RESET)

{

GPIO_SetBits(GPIOC, GPIO_Pin_7);

while(tt--);

GPIO_ResetBits(GPIOC, GPIO_Pin_7);

while(tt--);

EXTI_ClearFlag(EXTI_Line5);//清除中断标志

EXTI_ClearITPendingBit(EXTI_Line5);//清除EXTI线路挂起位

}

}

int main(void)

{

//配置IO口

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);//IO口使能设置

GPIO_InitTypeDef GPIO_InitStructure; //定义结构体

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置为上拉输入

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

//配置外部中断控制器

EXTI_InitTypeDef EXTI_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能时钟

EXTI_InitStructure.EXTI_Line = EXTI_Line5; //使用IO口5作为中断口

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置EXTI线路为中断请求

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource5);//将PE5挂接到EXTI9_5_IRQn中断源上

//配置中断控制器

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //选择中断组0

NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //指定中断源,0-4管脚属于EXTI0_IRQn~EXTI4_IRQn,5-9管脚属于EXTI9_5_IRQn,10-15管脚属于EXTI15_10_IRQn

//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//无先占优先级,从优先级为1

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

while(1);

}

//以下是报错函数

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

while (1)

{

}

}

#endif

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top