按一下是有Push and Release 事件,要计算bounce?
要用Interrupt 或是Polling?
既然是单片机,就用interrupt表示,再写下流程图,根据流程图写功能程序
BYTE Key1=0,Key2=0;
IRQ Key1(){
Key1++;
}
IRQ Key2(){
Key2=1;
}
while(1){
if(Key1%2){
LED1=on;
}
else{
LED1=off;
if(Key1>=100) Key1=0; // avoid over 256
}
if(Key2){
LED2=on;
LED1=off;
Key2=0;
}
}
追问
大哥 有完整点的吗
追答
方案一:
将key1 与 key2 AND gate 到EXT INT pin, 利用中断如上代码逻辑
方案二:
BYTE KeyTrig=0;
ReadKey(){
bit ReadKey1=P0^1;
Sleep(n); // for de-bounce
if(ReadKey1!=P0^1){
Key1++;
KeyTrig|=1;
}
bit ReadKey2=P0^2;
Sleep(n); // for de-bounce
if(ReadKey2!=P0^2){
Key2++;
KeyTrig|=2;
}
}
while(1){
ReadKey();
if(KeyTrig>0){
if(KeyTrig&1){
if(Key1%2){ //Key1==odd
LED1=1;
}
else{ //
LED1=0;
if(Key1>=100) Key1=0;
}
KeyTrig=KeyTrig&0xfe;
}
if(KeyTrig&2){
if(Key2%2){ //Key2==odd
LED2=1;
LED1=0;
}
else{ //
LED2=0
LED1=1;
if(Key2>=100) Key2=0;
}
KeyTrig=KeyTrig&0xfd;
}
}
}
温馨提示:答案为网友推荐,仅供参考