c语言按一下按键使led亮10s,求一单片机C语言程序:两个按键控制两个LED KEY1按一下LED1亮 再按一下LED1灭 KEY2按下LED2亮 LED1灭...

按一下是有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;

}

}

}

温馨提示:答案为网友推荐,仅供参考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值