#include <reg52.h>
#include <intrins.h>
unsigned char key;
// 7段显示代码
unsigned char code display[] =
{
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
};
unsigned char code key_code[] =
{
//键编码数组
0xee, 0xed, 0xeb, 0xe7,
0xde, 0xdd, 0xdb, 0xd7,
0xbe, 0xbd, 0xbb, 0xb7,
0x7e, 0x7d, 0x7b, 0x77
};
char code reserve[3]_at_ 0x3b; //保留0x3b开始的三个字节
sbit k1 = P3 ^ 2;
sbit k2 = P3 ^ 3;
sbit k3 = P3 ^ 4;
/**********************************************************
延时子程序
**********************************************************/
void delayms(unsigned int ms)
{
unsigned char t;
while (ms--)
{
for (t = 0; t < 114; t++)
;
}
}
/**********************************************************
x*0.14MS 延时
**********************************************************/
void delayus(unsigned char x)
{
unsigned char i;
while (x--)
{
for (i = 0; i < 14; i++)
{
;
}
}
}
/**********************************************************
键盘扫描子程序
**********************************************************/
unsigned char keyscan()
{
unsigned char scan1, scan2, keycode, j;
P3 = 0xf0;
scan1 = P3;
if (scan1 != 0xf0)
//判断是否按下
{
delayms(10); //ÑÓʱ10ms
scan1 = P3;
if (scan1 != 0xf0)
//二次判断是否按下
{
P3 = 0x0f;
delayms(10);
scan2 = P3;
keycode = scan1 | scan2; //组合成键扫描编码
for (j = 0; j < 16; j++)
{
if (keycode == key_code[j])
//查表得键值
{
key = j;
return (key); //返回有效有效键值
}
}
}
}
else
P3 = 0xff;
return (key = 16); //返回无效键值
}
/**********************************************************
主程序
**********************************************************/
void main(void)
{
unsigned char k, shift,a;
P0 = 0xff; //端口初始化
P1 = 0xff;
P3 = 0xff;
while (1)
{
shift = 0xfe; //位扫描初值
P1 = 0xff; //关闭显示
keyscan();
if (key < 16)
//有效键值
{
a=(key+1)*200; //对延时进行调整
}
for (k = 0; k < 16; k++)
{
P0 = display[k]; //送段码
P1 = shift; //送位码
shift = _crol_(shift, 1);
delayms(a);
}
}
}
/*********************************************************/
|