希望可以对你有用
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "stm32f10x_exti.h"
- #include "key.h"
- #include "Delay.h"
- void exit_config(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8); //EXTI8EXTI_Line8中断线与PB8映射,设置PB8为中断源EXTI_Line8
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9); //设置PB9为中断源EXTI_Line9
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource10); //设置PB10为中断源EXTI_Line10
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11); //设置PB11为中断源EXTI_Line11
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource12); //EXTI12EXTI_Line12中断线2与PB12映射,设置PB12为中断源EXTI_Line12
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource13); //设置PB13为中断源EXTI_Line13
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14); //设置PB14为中断源EXTI_Line14
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource15); //设置PB15为中断源EXTI_Line15
-
- EXTI_InitStructure.EXTI_Line=0XFF00;//EXTI_Line8|EXTI_Line9|EXTI_Line10|EXTI_Line11|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15; //将中断映射到中断线EXTI_Line12~EXTI_Line15上
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断模式
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置为下降沿触发中断
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断使能,既开中断
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; //抢占优先级0位
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f; //响应优先级4位,优先级15,为最低优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; //抢占优先级0位
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f; //响应优先级4位,优先级15,为最低优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); //中断优先级分组初始化
- }
- void EXTI9_5_IRQHandler(void)
- {static u8 k1=0,k2=0; //LED亮和灭状态,为0是熄灭状态,为1是点亮状态
- Delay(20);
- if(KEY1==0) //读取K3按键状态,判断K1按键是否按下
- {
- if(k1==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_8);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_8);
- k1=!k1;
- }
- else if(KEY2==0) //读取K4按键状态,判断K4按键是否按下
- {
- if(k2==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_9);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_9);
- k2=!k2;
- }
- //while(KEY1!=1||KEY2!=1);//仿真可以取消
- //EXTI_ClearITPendingBit(EXTI_Line8);
- //EXTI_ClearITPendingBit(EXTI_Line9);
- }
- void EXTI15_10_IRQHandler(void)
- {
- static u8 k3=0,k4=0,k5=0,k6=0,k7=0,k8=0; //LED亮和灭状态,为0是熄灭状态,为1是点亮状态
- Delay(20);
- if(KEY3==0) //读取K3按键状态,判断K1按键是否按下
- {
- if(k3==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_10);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_10);
- k3=!k3;
- }
- else if(KEY4==0) //读取K4按键状态,判断K4按键是否按下
- {
- if(k4==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_11);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_11);
- k4=!k4;
- }
- else if(KEY5==0) //读取K5按键状态,判断K5按键是否按下
- {
- if(k5==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_12);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_12);
- k5=!k5;
- }
- else if(KEY6==0) //读取K6按键状态,判断K6按键是否按下
- {
- if(k6==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_13);
- k6=!k6;
- }
- else if(KEY7==0) //读取K7按键状态,判断K7按键是否按下
- {
- if(k7==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_14);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_14);
- k7=!k7;
- }
- else if(KEY8==0) //读取K7按键状态,判断K7按键是否按下
- {
- if(k8==0)
- GPIO_ResetBits(GPIOC,GPIO_Pin_15);
- else
- GPIO_SetBits(GPIOC,GPIO_Pin_15);
- k8=!k8;
- }
- //while(KEY3!=1||KEY4!=1||KEY5!=1||KEY6!=1||KEY7!=1||KEY8!=1);//仿真可以取消
- //EXTI_ClearITPendingBit(EXTI_Line10);
- //EXTI_ClearITPendingBit(EXTI_Line11);
- //EXTI_ClearITPendingBit(EXTI_Line12);
- //EXTI_ClearITPendingBit(EXTI_Line13);
- //EXTI_ClearITPendingBit(EXTI_Line14);
- //EXTI_ClearITPendingBit(EXTI_Line15);
- }
复制代码
所有资料51hei提供下载:
中断方式的按键控制 8按键整理.7z
(223.04 KB, 下载次数: 67)
|