我的设想是在按键按下时按键计时的标志量清零,然后一秒加一,按键抬起时记录时间数据判断,超过两秒即为长按,执行语句,但是代码写出来按按键没有用,下面是代码段这个是按键读取的单片机代码,按下按键flag_key置1,松手变成2
- unsigned char read_key()
- {
- unsigned char line,row,key_value=0;
- switch (key_sate)
- {
- case 1:
- {
- P33=0;
- P32=0;
- P42=1;
- P44=1;
- if(P42==0||P44==0)
- {
- Delay_ms(5);
- if(P42==0||P44==0)
- {
- key_sate=2;
- flag_key=1;
- }
- }
- }
- break;
- case 2:
- {
- if(P44==0)
- line=1;
- if(P42==0)
- line=2;
- P33=1;
- P32=1;
- P42=0;
- P44=0;
- if(P33==0)
- row=4;
- if(P32==0)
- row=3;
- key_value=row*10+line;
- key_sate=3;
- }
- break;
- case 3:
- {
- if(P32==1&&P33==1&&P42==0&&P44==0)
- {
- key_sate=1;
- flag_key=2;
- }
- }
- break;
- }
- return key_value;
- }
复制代码 然后是定时器计时的代码,中间有几句是读取555频率的- void timer1_ms() interrupt 3
- {
- time_1ms++;
- if(flag_key==1)
- {
- key_time=0;
- key_time_1s=0;
- }
- if(time_1ms>=1000)
- {
- frequency_ne555=count_ne555;
- count_ne555=0;
- time_1s++;
- key_time_1s++;
- time_1ms=0;
- }
- if(flag_key==2)
- {
- key_time=key_time_1s;
- }
- }
复制代码 最后就是判断长按的,然后执行的代码段- void mod_re_time()
- {
- unsigned char i,time_h_1,time_m_1,count_1;
- time_h_1=time_h;
- time_m_1=time_m;
- count_1=count;
- if(key==31)
- {
- mod_re++;
- if(mod_re>=4)
- mod_re=1;
- }
- if(key==32)
- {
- if(key_time>=2)
- {
- key=0;
- count=0;
- key_time=0;
- sum_tem=0;
- sum_hum=0;
- ave_tem=0;
- ave_hum=0;
- max_tem=0;
- max_hum=0;
- key_time_1s=0;
- }
- }
复制代码
|