|
外部中断实现led键控led灯实验
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #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;
- }
- }
复制代码
所有资料51hei提供下载:
外部中断.7z
(234.04 KB, 下载次数: 117)
|
-
6.png
(4.94 KB, 下载次数: 63)
-
5.png
(13.67 KB, 下载次数: 75)
-
4.png
(11.33 KB, 下载次数: 64)
评分
-
查看全部评分
|