#include "stm32f10x.h" static EXTI_InitTypeDef EXTI_InitStructure; static GPIO_InitTypeDef GPIO_InitStructure; static NVIC_InitTypeDef NVIC_InitStructure; #define PB5OUT(n) *(volatile uint32_t *)(0x42000000+((uint32_t)&GPIOB->ODR-0x40000000)*32+n*4) void led(void){ //led-->PB5 //使能端口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //初始化GPIO引脚 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;//高速响应 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_5); } //中断初始化函数 void exti4_init(void) { //key0-->PE4 //使能(打开)端口E的硬件时钟,就是对端口E供电 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //使能系统配置时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //配置引脚4的工作模式 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_Init(GPIOE, &GPIO_InitStructure); //将引脚连接到外部中断,即将PE4和EXTI4连接在一起 GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4); //外部中断的配置 EXTI_InitStructure.EXTI_Line = EXTI_Line4; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising ;//检测按键按下 EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能 EXTI_Init(&EXTI_InitStructure); //配置NVIC大总管允许外部中断引脚申请中断请求 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //中断请求通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; //抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; //响应优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void delay(void){ uint32_t i=0x2000000; while(i--); } //中断服务函数 void EXTI4_IRQHandler(void){ //判断是否有中断请求 if(EXTI_GetITStatus(EXTI_Line4) == SET) { //灯亮 PB5OUT(5)=0; delay(); PB5OUT(5)=1; /* 清空标志位。告诉cpu,已经完成当前中断处理,可以响应新的中断请求 如果不清空标志位,cpu则认为当前中断处理还没有完成,继续执行中断服务函数,直到标志位清空为止 */ EXTI_ClearITPendingBit(EXTI_Line4); } } int main(){ led(); exti4_init(); while(1){ } }
发现自己进步了,哈哈哈哈哈~开心
微信扫码关注
更新实时通知