简单的单片机矩阵键盘程序
时间:01-18 14:54 阅读:876次
*温馨提示:点击图片可以放大观看高清大图
简介:在这里和大家分享一个简单的单片机矩阵键盘程序,感兴趣的朋友可以看看。
#include <reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY1 = P2^4;
sbit KEY2 = P2^5;
sbit KEY3 = P2^6;
sbit KEY4 = P2^7;
unsigned char code LedChar[] = { //数码管显示字符转换表
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
void delay();
void main()
{
bit keybuf = 1; //按键值暂存,临时保存按键的扫描值
bit backup = 1; //按键值备份,保存前一次的扫描值
unsigned char cnt = 0; //按键计数,记录按键按下的次数
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
P2 = 0xf7; //P2.3置0,即KeyOut1输出低电平
P0 = LedChar[cnt]; //显示按键次数初值
while (1)
{
keybuf = KEY4; //把当前扫描值暂存
if (keybuf != backup) //当前值与前次值不相等说明此时按键有动作
{
delay(); //延时大约10ms
if (keybuf == KEY4) //判断扫描值有没有发生改变,即按键抖动
{
if (backup == 0) //如果前次值为0,则说明当前是弹起动作
{
cnt++; //按键次数+1
if (cnt >= 10)
{ //只用1个数码管显示,所以加到10就清零重新开始
cnt = 0;
}
P0 = LedChar[cnt]; //计数值显示到数码管上
}
backup = keybuf; //更新备份为当前值,以备进行下次比较
}
}
}
}
/* 软件延时函数,延时约10ms */
void delay()
{
unsigned int i = 1000;
while (i--);
}