27
693
1950
金牌会员
打工人
社区*** 发表于 2023-12-28 16:00 【分享】新型的按键扫描程序 + 累计循环次数。不占用定时器 https://www.stcaimcu.com/forum.php?mod=vie ...
使用道具 举报 送花
25
159
304
中级会员
4
242
1287
17
61
575
高级会员
12
319
2017
QW1*** 发表于 2024-1-31 16:33 鸿哥的帖子告诉我,放到定时器里面去,1ms中断一次,判断20次,消抖
13
1257
2971
10
70
1307
76
4833
8343
超级版主
DebugLab
void Key_Scan(void) { static bit Key_Flag; if(KEY==0) { if(Key_Flag==1) { Key_Flag=0; //在这里加按下要执行的 } } else { if(Key_Flag==0) { Key_Flag=1; //在这里加松开要执行的复制代码
#define Init_Up 25 //默认释放时间 #define Init_Down 25 //默认按下时间 #define Init_Long 100 //默认长按时间 #define Init_Disable 1 //默认死区时间 unsigned char Key_Decode(unsigned char temp) { unsigned char key; switch(temp) { case 0x01:key=0;break; case 0x02:key=1;break; case 0x04:key=2;break; case 0x08:key=3;break; case 0x10:key=4;break; case 0x20:key=5;break; case 0x40:key=6;break; case 0x80:key=7;break; default:key=0xff; } return key; } void Key_Scan(void) { unsigned char temp_x,temp_y; X_BUS=0x00; Y_BUS=0xff; Delay_x10us(100); temp_y=Y_BUS; if(temp_y!=0xff) { temp_y=Y_BUS; X_BUS=0xff; Y_BUS=temp_y; Delay_x10us(100); temp_x=X_BUS; temp_x=Key_Decode(~temp_x); temp_y=Key_Decode(~temp_y); if(temp_x<8&&temp_y<8) { Key=temp_x*8+temp_y; Key_Flag=1; } else { Key_Flag=0; Key=0xff; } } else { Key_Flag=0; Key=0xff; } } void Clear_Time(bit flag) { TL1=0x00; TH1=0x70; TR1=flag; } void main(void) { Init(); //初始化设置定时器1为不自动重载模式,以20ms为单位,定20ms Temp=IapReadByte(Sector0); if(Temp>=5&&Temp<=50) Time_Up=Temp; else { IapEraseSector(Sector0); IapProgramByte(Sector0,Init_Up); Time_Up=Init_Up; } Temp=IapReadByte(Sector1); if(Temp>=5&&Temp<=50) Time_Down=Temp; else { IapEraseSector(Sector1); IapProgramByte(Sector1,Init_Down); Time_Down=Init_Down; } Temp=IapReadByte(Sector2); if(Temp>=50&&Temp<=250) Time_Long=Temp; else { IapEraseSector(Sector2); IapProgramByte(Sector2,Init_Long); Time_Long=Init_Long; } Temp=IapReadByte(Sector3); if(Temp>=1&&Temp<=10) Time_Disable=Temp; else { IapEraseSector(Sector3); IapProgramByte(Sector3,Init_Disable); Time_Disable=Init_Disable; } Key_Temp=255; while(1) { Key_Scan(); if(Key_State!=Key_Flag) //按键状态改变 { Key_State=Key_Flag; Clear_Time(1); if(Key_State) //按下瞬间 { Count_Down=0; Key_Single=1; Key_Long=1; if(Key_Temp==Key) Key_Double=1; //和上次一样 else //和上次不一样 { Key_Double=0; Key_Temp=Key; } } else //松开瞬间 { Count_UP=0; Key_Long=0; if(Key_Single&&Count_Down>Time_Disable&&Count_Down<Time_Down); else { Clear_Time(0); Key_Single=0; Key_Double=0; Key_Long=0; Key_Temp=0xff; } } } if(Key_Flag) //按下 { if(Count_UP>=Time_Long&&Key_Long) //长按 { Send_Key(Key_Temp+128); Clear_Time(0); Key_Single=0; Key_Double=0; Key_Long=0; Key_Temp=0xff; } } else //松开 { if(Count_UP>=Time_Up&&Key_Single) //单击 { Send_Key(Key_Temp); Clear_Time(0); Key_Single=0; Key_Double=0; Key_Long=0; Key_Temp=0xff; } if(Count_UP>=Time_Disable&&Count_UP<=Time_Up&&Count_Down>=Time_Disable&&Count_Down<=Time_Down&&Key_Double&&Key_Single) //双击 { Send_Key(Key_Temp+64); Clear_Time(0); Key_Single=0; Key_Double=0; Key_Long=0; Key_Temp=0xff; } } } } void Timer1_Isr(void) interrupt 3 { Count_UP++; Count_Down++; if(Count_UP>250||Count_Down>250) { Count_UP=0; Count_Down=0; } }复制代码
21cns*** 发表于 2024-1-31 19:53 有必要在1ms这么短的中断中判断按键码?会不会时间片太短了?我觉得10-20ms应该比较合理吧,纯技术探讨 ...
818
1490
myli*** 发表于 2023-12-28 11:15 amobbs里有几个关于按键的帖子挺不错的可以去参考下
本版积分规则 发表回复 回帖后跳转到最后一页
|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )
GMT+8, 2025-5-2 00:21 , Processed in 0.122642 second(s), 125 queries .
Powered by Discuz! X3.5
© 2001-2025 Discuz! Team.