外部中断实现led键控led灯实验
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
外部中断.7z
(234.04 KB, 售价: 1 E币)
部分源码
- #include"stm32f10x.h"
- #define Led_Red_On GPIO_ResetBits(GPIOC,GPIO_Pin_1)
- #define Led_Red_Off GPIO_SetBits(GPIOC,GPIO_Pin_1)
- #define Led_Red_Toggle GPIOC -> ODR ^= GPIO_Pin_1
- #define Led_Green_On GPIO_ResetBits(GPIOC,GPIO_Pin_2)
- #define Led_Green_Off GPIO_SetBits(GPIOC,GPIO_Pin_2)
- #define Led_Green_Toggle GPIOC -> ODR ^= GPIO_Pin_2
- #define Led_Blue_On GPIO_ResetBits(GPIOC,GPIO_Pin_3)
- #define Led_Blue_Off GPIO_SetBits(GPIOC,GPIO_Pin_3)
- #define Led_Blue_Toggle GPIOC -> ODR ^= GPIO_Pin_3
- void Blue_Flash() //蓝灯闪烁
- {
- int i;
- void delay(unsigned int t);
- for(i=3;i>0;i--)
- {
- Led_Blue_Toggle;delay(100000);
- }
- }
- void Led_Configration()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
-
- GPIO_InitStruct.GPIO_Pin = (GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);
-
- Led_Red_Off;
- Led_Green_Off;
- Led_Blue_Off;
- }
- void delay(unsigned int t)
- {
- while(t--);
- }
- void EXTI_Configration()
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ,ENABLE);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource13);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line13;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- void NVIC_Configration()
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- }
- int main()
-
- {
- Led_Configration();
- EXTI_Configration();
- NVIC_Configration();
-
- while(1) //控制红绿灯交替闪烁
- {
- Led_Red_Toggle;
- delay(100000);
- Led_Green_Toggle;
-
- }
- }
- void EXTI0_IRQHandler() //按键1进入EXTI0中断控制蓝灯闪烁
- {
-
- if(EXTI_GetITStatus(EXTI_Line0) != RESET)
- {
- EXTI_ClearITPendingBit(EXTI_Line0);
- Blue_Flash();
- }
- }
-
- void EXTI15_10_IRQHandler() //按下KEY2进入EXTI13通道使蓝灯状态取反
- {
-
- if(EXTI_GetITStatus(EXTI_Line13) != RESET)
- {
- EXTI_ClearITPendingBit(EXTI_Line13);
- Led_Blue_Toggle;
- }
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|