#include <reg51.h>
#include <intrins.h>
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
#define uchar unsigned char
#define uint unsigned int
uint key;
uchar code1[]={"0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,-"};
uchar code2[]={" Key number "};
uchar code3[]={" key code:- H "};
void delay_1ms(uint n)
{
uint i,j;
for(i=0;i<n;i++)
for(j=0;j<125;j++);
}
void write_cmd(uchar cmd)
{
rs=0;
rw=0;
en=1;
P0=cmd;
delay_1ms(20);
en=0;
}
void write_dat(uchar dat)
{
rs=1;
rw=0;
en=1;
P0=dat;
delay_1ms(20);
en=0;
}
void lcd_init()
{
en=1;
write_cmd(0x38);
delay_1ms(10);
write_cmd(0x0c);
delay_1ms(10);
write_cmd(0x06);
delay_1ms(10);
write_cmd(0x01);
delay_1ms(10);
en=0;
}
void keyscan()
{
uchar temp,sem1,sem2;
P1=0xf0;
temp=P1;
if(temp!=0xf0)
{
delay_1ms(15);
if(temp!=0xf0)
sem1=temp&0xf0;
P1=0x0f;
delay_1ms(1);
temp=P1;
sem2=temp&0x0f;
temp=sem1|sem2;
}
switch(temp)
{
case 0xee : key=0; break;
case 0xde : key=4; break;
case 0xbe : key=8; break;
case 0x7e : key=12; break;
case 0xed : key=1; break;
case 0xdd : key=5; break;
case 0xbd : key=9; break;
case 0x7d : key=13; break;
case 0xeb : key=2; break;
case 0xdb : key=6; break;
case 0xbb : key=10; break;
case 0x7b : key=14; break;
case 0xe7 : key=3; break;
case 0xd7 : key=7; break;
case 0xb7 : key=11; break;
case 0x77 : key=15; break;
default : key=16; break;
}
}
void main()
{
uint k;
lcd_init();
delay_1ms(10);
write_cmd(0x80);
for(k=0;k<16;k++)
{
write_dat(code2[k]);
delay_1ms(10);
}
write_cmd(0x80+0x40);
for(k=0;k<16;k++)
{
write_dat(code3[k]);
delay_1ms(10);
}
while(1)
{
keyscan();
delay_1ms(1);
write_cmd(0x80+0x4d);
delay_1ms(2);
write_dat(code1[key]);
delay_1ms(10);
}
} |