4X4矩阵键盘控制LED ----AT89C51
#include
#define uchar unsigned char
#define uint unsigned int
/*键盘按键特征码*/
/*这里的特征码与具体的硬件连接有关*/
uchar code keycodetable[]={0x11,0x12,0x14,0x18,
0x21,0x22,0x24,0x28,
0x41,0x42,0x44,0x48,
0x81,0x82,0x84,0x88};
void delayms(uint x)
{ uchar i;
while(x--) for(i=0;i<120;i++);
}
uchar key_scan()
{
uchar scode,kcode,i,k;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
delayms(2); /*延时,消除按键抖动*/
if((P1&0xf0)!=0xf0)
{
scode=0xfe;
for(k=0;k<4;k++)
{
P1=scode;
if((P1&0xf0)!=0xf0)
{
kcode=~P1; /*读取键值*/
for(i=0;i<16;i++) /*查找键值表*/
if(kcode==keycodetable[i])
return(i);
}
else
scode=_crol_(scode,1); /*左移,低位补1,由scode的高位补到低位*/
}
}
}
return (-1);
}
void main()
{
uchar i,p2_led,p3_led;
uchar keyno=-1;
while(1)
{
keyno=key_scan();
if(keyno!=-1)
{
p2_led=0xff;
p3_led=0xff;
for(i=0;i { if(i<8) p3_led>>=1; else p2_led>>=1; } P3=p3_led; P2=p2_led; } } }
4X4矩阵键盘控制LEDAT89C5 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)