#define is_button_pressed() (SW==0)
#define is_button_pressed() (SW==1)
#define COMFIRM_TIME_SET 100
#define DELAY_SET 10000
volatile uint8_t key_counter=0;
volatile uint8_t filter_acc=0;
volatile uint16_t delay_acc=0;
void key(uint8_t *state)
{
switch((*state))
{
case 0:
//防抖
if(is_button_pressed()) filter_acc++;
else filter_acc=0;
//按钮按下
if(filter_acc>COMFIRM_TIME_SET)
{
(*state)=1;
key_counter++;
filter_acc=0;
}
break;
case 1:
//防抖
if(is_button_released()) filter_acc++;
else filter_acc=0;
//按钮释放
if(filter_acc>COMFIRM_TIME_SET)
{
(*state)=0;
key_counter++;
filter_acc=0;
}
break;
}
}
void play(uint8_t *state)
{
switch((*state))
{
case 0:
P10=0;P11=1;
break;
case 1:
P10=1;P11=1;
break;
case 2:
P10=0;P11=1;
if(++delay_acc>DELAY_SET)
{
delay_acc=0;
(*state)=3;
}
break;
case 3:
P10=1;P11=0;
if(++delay_acc>DELAY_SET)
{
delay_acc=0;
(*state)=2;
}
break;
}
}
你这个play函数卡循环,导致卡循环,给你个程序模板,你看看吧。
|