#include <reg52.h>sbit KEY1 = P2^4;sbit KEY2 = P2^5;sbit KEY3 = P2^6;sbit KEY4 = P2^7;sbit ADDR0 = P1^0;sbit ADDR1 = P1^1;sbit ADDR2 = P1^2;sbit ADDR3 = P1^3;sbit ENLED = P1^4;unsigned char code LedChar[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8e}; //数码管真值表void delay(void); //延时函数声明void main(void){ bit keybuf = 1; //按键值暂存,临时保存按键的扫描值 bit backup = 1; //按键值备份,保存前一次的扫描值 unsigned char counter = 0; //计数器记录按键按下的次数 //选择最右边的数码管进行显示 P0 = LedChar[counter]; ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; ADDR3 = 1; ENLED = 0; //选中第一行按键以进行扫描 P2 = 0xF7; while(1) { keybuf = KEY4; //只取KEY4为例,把当前扫描值暂存 if (keybuf != backup) //当前值与前一次值不相等说明此时按键有动作 { delay(); //延时大约10ms if (keybuf == KEY4) //判断扫描值有没有发生改变,即按键抖动 { if (backup == 0) //如果前一次的值为0,则说明当前状态是由0变为1,即按键弹起 { counter++; //计数器+1 if (counter >= 10) { //只用1个数码管显示,所以记到10就清零重新开始 counter = 0; } P0 = LedChar[counter]; //计数值显示到数码管上 } backup = keybuf; //更新备份为当前值,以备进行下次比较 } } }}void delay(void){ unsigned int i = 1000; while (i--); //通过debug的KEIL软件延时方式计算得出大概是10ms}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>