矩阵键盘 ,延时
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code led[]=
{~0x3F,~0x06,~0x5B,~0x4F,
~0x66,~0x6D,~0x7D,~0x07,
~0x7F,~0x6F,~0x77,~0x7C,
~0x39,~0x5E,~0x79,~0x71};
uchar key,temp,dis_buf;
void delay()
{ uint x,y;
for(x=10;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{ P1=0xf0;//?33??μ
if(P1!=0xf0)//ê?·?°′?ˉ?ü?ì
{temp=0;
P1=0xf0;
delay();
temp=P1;
temp=temp&0xf0;
temp=~(temp>>4|0xf0);
switch(temp)
{ case 1: key=1;break;
case 2: key=2;break;
case 4: key=3;break;
case 8: key=4;break;
default :key=16;
return;
}
P1=0x0f;
delay();
temp=P1;
temp=temp&0x0f;
temp=~(temp|0xf0);
switch(temp)
{ case 1: key=key;break;
case 2: key=key+4;break;
case 4: key=key+8;break;
case 8: key=key+12;break;
default :key=16;
return;
}
dis_buf = key & 0x0f;
}}
void keydown(void)
{ P1=0xf0;//?33??μ
if(P1!=0xf0)//ê?·?°′?ˉ?ü?ì
{ keyscan();}
}
void main()
{ P1=0xff;
P0=0xff;
delay();
while(1)
{keydown();
P0=led[ dis_buf%16]&0x7f;}
}
为什么程序中多加dis_buf = key & 0x0f;这句显示的数字就不会被清零?
#define uint unsigned int
#define uchar unsigned char
uchar code led[]=
{~0x3F,~0x06,~0x5B,~0x4F,
~0x66,~0x6D,~0x7D,~0x07,
~0x7F,~0x6F,~0x77,~0x7C,
~0x39,~0x5E,~0x79,~0x71};
uchar key,temp,dis_buf;
void delay()
{ uint x,y;
for(x=10;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{ P1=0xf0;//?33??μ
if(P1!=0xf0)//ê?·?°′?ˉ?ü?ì
{temp=0;
P1=0xf0;
delay();
temp=P1;
temp=temp&0xf0;
temp=~(temp>>4|0xf0);
switch(temp)
{ case 1: key=1;break;
case 2: key=2;break;
case 4: key=3;break;
case 8: key=4;break;
default :key=16;
return;
}
P1=0x0f;
delay();
temp=P1;
temp=temp&0x0f;
temp=~(temp|0xf0);
switch(temp)
{ case 1: key=key;break;
case 2: key=key+4;break;
case 4: key=key+8;break;
case 8: key=key+12;break;
default :key=16;
return;
}
dis_buf = key & 0x0f;
}}
void keydown(void)
{ P1=0xf0;//?33??μ
if(P1!=0xf0)//ê?·?°′?ˉ?ü?ì
{ keyscan();}
}
void main()
{ P1=0xff;
P0=0xff;
delay();
while(1)
{keydown();
P0=led[ dis_buf%16]&0x7f;}
}
为什么程序中多加dis_buf = key & 0x0f;这句显示的数字就不会被清零?
配套资料都有滴