中级会员
 
- 积分
- 277
- 金钱
- 277
- 注册时间
- 2017-3-17
- 在线时间
- 120 小时
|
5金钱
采用非中断按钮,长时间按按钮,如何取最后一次的信息。[mw_shl_code=c,true]u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&((WARING_KEY==0 && close_judge==0)||(WARING_KEY==1 && close_judge==1)||(INTRUDE_KEY==1 && Relay12==1)||(DOOR_KEY==0)))
{
delay_ms(10);
key_up=0;
if(WARING_KEY==0 && close_judge==0) return 3;
else if(WARING_KEY==1 && close_judge==1) return 4;
else if(INTRUDE_KEY==1 && Relay12==1) return 2;
else if(DOOR_KEY==0) return 1;
}
else if((WARING_KEY==0 && close_judge==1)||(WARING_KEY==1 && close_judge==0)||(INTRUDE_KEY==1 && Relay12==1)||(DOOR_KEY==1))
key_up=1;
return 0;
}
void Button_watch(void)
{
static u8 key;
key=KEY_Scan(1);
if(key)
{
switch(key)
{
case DOOR_PRES:
if(close_judge)
return;
state_door[0]=1;
state_door[1]=1;
state_door[2]=1;
return;
case WAR_INT:
if(close_judge || close_intrude || time_out_war)
return;
state_door[0]=1;
state_door[1]=3;
state_door[2]=1;
close_intrude=1;
return;
case WAR_STA:
state_door[0]=1;
state_door[1]=4;
state_door[2]=0;
close_judge=1;
OPEN_ALL_DOOR(2048);
return;
case WAR_END:
state_door[0]=1;
state_door[1]=4;
state_door[2]=1;
close_judge=0;
close_intrude=0;
time_out_war=0;
CLOSE_ALL_DOOR(2048);
return;
}
}
}[/mw_shl_code]
主程序
[mw_shl_code=applescript,true]while(1)
{
……
Button_watch();
……
}[/mw_shl_code]
怎么做到连续按下按钮,只触发一次
|
最佳答案
查看完整内容[请看2#楼]
先上百度,找一下单片机状态机编程的方式.了解一下事件与状态.
代码里,做一个类似秒表一样的记时器.按钮有按下时,清0记时器.松开时读取记时器.这样子就知道是按下了多久才松开的.什么长短,短按都可以完成了.如果要做长按三秒的功能.当记时器的记数值到了3秒时,执行相应按钮的功能代码即可.
|