论坛元老
- 积分
- 4638
- 金钱
- 4638
- 注册时间
- 2017-7-6
- 在线时间
- 705 小时
|
希望新手们及早放弃用delay延时消抖等等,最好用定时器来实现按键消抖,延时等等~~~下面是我做的简单的按键多功能,程序如下:
uchar S_key = 2,D_key = 1,L_key = 3,N_key = 0;
bit b_key_led;
bit b_key_smg;
uchar key_drive(void)
{
static uchar key_state;
static uint key_time = 0;
uchar key_return;
key_return = N_key;
switch(key_state)
{
case 0:
{
if(!S1)
{
key_state = 1;
}
};break;
case 1:
{
if(!S1)
{
key_time = 0;
key_state = 2;
}
else
{
key_state = 0;
}
};break;
case 2:
{
if(S1)
{
key_return = D_key;
key_state = 0;
}
else if(++key_time >= 1500)
{
key_return = L_key;
key_state = 3;
}
};break;
case 3:
{
if(S1)
{
key_state = 0;
}
};break;
}
return key_return;
}
uchar key_read(void)
{
static uchar key_state_1,key_time_1 = 0;
uchar key_return,key_temp;
key_temp = key_drive();
switch(key_state_1)
{
case 0:
{
if(key_temp == D_key)
{
key_time_1 = 0;
key_state_1 = 1;
}
else
{
key_return = key_temp;
}
};break;
case 1:
{
if(key_temp == D_key)
{
key_return = S_key;
key_state_1 = 0;
}
else if(++key_time_1 >= 250)
{
key_return = D_key;
key_state_1 = 0;
}
};break;
}
return key_return;
}
void key_on(void)
{
uchar state_1;
state_1 = key_read();
if(state_1 == S_key) //双击
{
/*双击按键实现呼吸灯功能*/
state_1 = 0;
b_key_led = 1;
b_key_smg = 0;
seg = 0x00;
seg_l = 0x00;
}
if(state_1 == D_key) //单按
{
/*单击实现数码管走时*/
state_1 = 0;
b_key_smg = 1;
b_key_led = 0;
PW1D = 0x0000;
}
if(state_1 == L_key) //长按
{
/*长按,实现你要实现的功能*/
}
}
|
|