打印

按键短按灯亮六秒后灭,长按三秒灯亮三秒后灭

[复制链接]
173|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
TI, ni, vi, ic, ST
今天闲着无聊,一个新手请教我一个问题:短按 SW1 后,LED1亮6秒然后灭。长按SW1三秒后松开,LED1亮3秒后灭。闲着无聊,就写了一下。我的思路大概是这样的:外部中断+定时器
1.定时器3
配置好后定时器3,默认不使能定时器。
void TIM3_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,DISABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
2.SW1引脚配置成外部中断
void EXTIX_Init(void)
{
NVIC_InitTypeDefNVIC_InitStructure;
EXTI_InitTypeDefEXTI_InitStructure;
KEY_Init(); //这里按键配置就不说了
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;//LINE0
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//ʹÄÜLINE0
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
3.中断服务函数处理
(1)定时器中断服务函数
u16 ucTimer = 0;
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
ucTimer++;
if(WK_UP)
{
if(ucTimer ==300)//3S
{
handle_Led(ON,3);
ucTimer = 0;
TIM_Cmd(TIM3,DISABLE);
}
}
else
{
handle_Led(ON,6);
ucTimer = 0;
TIM_Cmd(TIM3,DISABLE);
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
(2)外部中断函数
void EXTI0_IRQHandler(void)
{
delay_ms(10);
if(WK_UP)
{
TIM_Cmd(TIM3,ENABLE);
}
EXTI_ClearITPendingBit(EXTI_Line0);
}

4.LED函数
void Led_ON(void)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_9); // 1- ON
}

void Led_OFF(void)
{
GPIO_SetBits(GPIOF, GPIO_Pin_9); // 0- OFF
}
void handle_Led(u8 ucSwitch, u8 usCnt)
{
if (ucSwitch == ON)
{int i;
for(i = 0; i < usCnt; i++)
Led_ON();
delay_ms(1000);}
Led_OFF(); }
else
{Led_OFF(); }
5.主函数
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LED_Init();
EXTIX_Init()
TIM3_Int_Init(100-1,8400-1); //10ms
while(1)
{LED1 = !LED1;
delay_ms(1000);}
简单程序,练练手。不知道其他高手有没有其他有效的办法,请分享一下。

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

376

主题

377

帖子

0

粉丝