|
功能:把键盘输入的数据存入显示缓存区 displaybuf ,然后显示缓存区的内容,
调试的结果不对,显示很乱~
不知道什么原因?
#include
#define uchar unsigned char
#define uint unsigned int
sbit ledk=P2^5; //锁存口
sbit d0=P1^0; //锁存口
sbit wela=P2^7; //使能口
sbit dula=P2^6; //使能口
uchar count;
uchar displaybufpoint; //当前显示位
uchar displaybuf []={0xff,0xff,0xff,0xff,0xff,0xff};//显示缓存区
void display_smg(uchar we,uchar du);
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for (y=110;y>0;y--);
}
//判断是否有键按下
uchar key_press()
{
uchar key;
P3=0xf0;
key=P3;
key=key&0xf0;
if(key==0xf0)
{
return 0;
}
else
{
return 1; //有键被按下
}
}
//键盘扫描
uchar key_scan()
{
uchar key;
if(key_press()) //是否有键按下
{
P3=0xfe;
key=P3;
switch (key)
{
case 0xee:return 0;break;
case 0xde:return 1;break;
case 0xbe:return 2;break;
case 0x7e:return 3;break;
}
P3=0xfd;
key=P3;
switch (key)
{
case 0xed:return 4;break;
case 0xdd:return 5;break;
case 0xbd:return 6;break;
case 0x7d:return 7;break;
}
P3=0xfb;
key=P3;
switch (key)
{
case 0xeb:return 8;break;
case 0xdb:return 9;break;
case 0xbb:return 10;break;
case 0x7b:return 11;break;
}
P3=0xf7;
key=P3;
switch (key)
{
case 0xe7:return 12;break;
case 0xd7:return 13;break;
case 0xb7:return 14;break;
case 0x77:return 15;break;
}
} //if end
}
//写显存
void write_dispalybuf(uchar x)
{
if (displaybufpoint==6) return; //6个数码管
displaybufpoint++;
displaybuf[displaybufpoint]=x;
}
void display()
{
uint i;
uchar du;
for(i=1;i<= displaybufpoint;i++)
{
du=displaybuf;
display_smg(i,du);
}
}
void display_smg(uchar we,uchar du)
{
uchar code tablewe []={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//位 0xfe 第1个数码
uchar code tabledu []={0x3f , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d ,
0x7d , 0x07 , 0x7f , 0x6f , 0x77 ,
0x7c , 0x39 , 0x5e , 0x79 , 0x71 };
wela=1;
P0=tablewe[we];
wela=0;
P0=0x00;
dula=1;
P0=tabledu[du];
dula=0;
delay(2);
P0=0xff;
}
void main()
{
uchar key;
ledk=0;
displaybufpoint=0;
while(1)
{
if( key_press() )
{
key=key_scan();
write_dispalybuf(key); //写显存
display();
}
}
}
|
|