新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2017-7-20
- 在线时间
- 5 小时
|
为了方便多次初始化,把函数中各参数设置成变量
KEY2,KEY1,KEY0的抢占优先级分别为3,2,1.
按下KEY2使LED0亮4秒
按下KEY1使LED1亮4秒
按下KEY0使蜂鸣器响4秒
实测效果:按顺序按下KEY2,KEY1,KEY0,LED0先亮,LED1再亮,蜂鸣器最后响,4秒后蜂鸣器停止响,再到LED1灭,最后LED0灭。成功实现2级嵌套并返回。
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include "sys.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "led.h"
#include "beep.h"
#include "key.h"
void EXTI_config(uint8_t PRI,uint8_t PorSource,uint8_t PinSource,uint8_t Line,uint8_t IRQChannel)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
GPIO_EXTILineConfig(PorSource,PinSource);
EXTI_InitStructure.EXTI_Line=Line;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PRI;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_config(GPIO_TypeDef * IO,uint16_t PIN )
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(IO,&GPIO_InitStructure);
}
int main(void)
{
delay_init();
KEY_Init();
LED_Init();
BEEP_Init();
GPIO_config(GPIOE,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
EXTI_config(3,GPIO_PortSourceGPIOE,GPIO_PinSource2,EXTI_Line2,EXTI2_IRQn);
EXTI_config(2,GPIO_PortSourceGPIOE,GPIO_PinSource3,EXTI_Line3,EXTI3_IRQn);
EXTI_config(1,GPIO_PortSourceGPIOE,GPIO_PinSource4,EXTI_Line4,EXTI4_IRQn);
while(1);
}
void EXTI2_IRQHandler(void)
{
u8 i=5;
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line2)==SET)
{
LED0=0;
while(i--)
delay_ms(1000);
EXTI_ClearITPendingBit(EXTI_Line2);
LED0=1;
}
}
void EXTI3_IRQHandler(void)
{
u8 i=5;
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line3)==SET)
{
LED1=0;
while(i--)
delay_ms(1000);
EXTI_ClearITPendingBit(EXTI_Line3);
LED1=1;
}
}
void EXTI4_IRQHandler(void)
{
u8 i=5;
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line4)==SET)
{
BEEP=1;
while(i--)
delay_ms(1000);
EXTI_ClearITPendingBit(EXTI_Line4);
BEEP=0;
}
}
|
-
|