#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
uchar a;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_cmd(uchar cmd)
{
lcden=0;
lcdrs=0;
lcdrw=0;
P0=cmd;
_nop_();
lcden=1;
delay(1);
lcden=0;
_nop_();
}
void write_dat(uchar dat)
{
lcden=0;
lcdrs=1;
P0=dat;
_nop_();
lcden=1;
delay(1);
lcden=0;
_nop_();
}
void lcdint()
{
lcden=0;
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);
}
void delayms(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=112;y>0;y--);
}
char keyscan()
{
uchar key_l,key_h,key;
P1=0xf0;
key_l=P1;
key_l=key_l&0xf0;
if(key_l!=0xf0)
{
delayms(50);
if(key_l!=0xf0) //假设检测0
{
key_l=P1&0xf0; //读取高4位 11100000
key_l=key_l|0x0f; //低4位写1 11101111
P1=key_l; //针第列
key_h=P1; //11101110
key_h=key_h&0x0f;//屏蔽高四位 00001110
key_l=key_l&0xf0; //掉低4位 11100000
key=key_h+key_l; //11101110
}
}
switch(key)
{
case 0xee: a=1 ;break;
case 0xde: a=2 ;break;
case 0xbe: a=3 ;break;
case 0x7e: a=4 ;break;
case 0xed: a=5 ;break;
case 0xdd: a=6 ;break;
case 0xbd: a=7 ;break;
case 0x7d: a=8 ;break;
case 0xeb: a=9 ;break;
case 0xdb: a=10;break;
case 0xbb: a=18;break;
case 0x7b: a=12;break;
case 0xe7: a=13;break;
case 0xd7: a=14;break;
case 0xb7: a=15;break;
case 0x77: a=16;break;
}
return a;
}
void main()
{
lcdint();
while(1)
{
keyscan();
if(a!=17)
{
if(a>9)
{
write_cmd(0x80);
write_dat(0x30+(a/10));
write_cmd(0x14);
write_dat(0x30+(a%10));
}
else
{
write_cmd(0x80);
write_dat(0x30+(a/10));
write_cmd(0x14);
write_dat(0x30+(a%10));
}
}
}
}
|