#include<reg52.h>
#include<math.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define COUNT 10
uchar buffer[COUNT];
uchar point;
void UART_init();
void COM_send(void);
uchar CLU_checkdata(void);
unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0X40,0X00};
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^5;
sbit lcdrs=P3^7;
uchar tab_key[50];
uchar code tab[]="0123456789abcdef";
uchar n=0,temp,key;
void UART_init()
{
SCON=0x58;
TMOD=0x21;
TH1 =0xfd;
TR1 =1;
ET1 =0;
ES =1 ;
EA =1;
}
com_interrupt(void) interrupt 4 using 3
{
uchar RECEIVR_buffer;
if(RI)
{
RI=0;
RECEIVR_buffer=SBUF;
if(point==0)
{
if(RECEIVR_buffer=='S')
point++;
else
point=0;
}
else if(point>0&&point<10)
buffer[point++]=RECEIVR_buffer;
else point =0;
}
if(TI)
{
TI=0;
}
}
void COM_sent(void)
{
for(point=0;point<=10,TI=1;point++)
{
SBUF=buffer[point];
TI=0 ;
}
}
uchar CLU_checkdata(void)
{
uchar checkdata=0;
for(point=0;point<9,TI=1;point++ )
{
checkdata=checkdata|buffer[point];
}
return(checkdata);
}
void lcd_disp();
void lcd_init();
void display(unsigned char);
void write_com(uchar);
void write_data(uchar);
void delay(unit);
void key_scan();
void key_manage1();
void key_manage2();
void key_manage3();
void key_manage4();
void key_manage5();
void key_manage6();
void key_manage7();
void key_manage8();
void key_manage9();
void key_manage10();
void key_manage11();
void key_manage12();
void key_manage13();
void key_manage14();
void key_manage15();
void key_manage16();
void main()
{ uchar checkdata;
lcd_init();
write_com(1);
while(1)
{
key_scan();
lcd_disp();
if(n>15)
write_com(0x80+0x40);
{
P3=0x10;
delay(10);
P3=0xe18;
delay(10);
P3=0x04;
delay(10);
P3=0x08;
delay(10);
P3=0x0c;
delay(10);
P3=0x1c;
delay(10);
P3=0x14;
delay(10);
}
}
do
{
UART_init();
if(point==10)
checkdata=CLU_checkdata;
if(checkdata==buffer[9])
COM_send();
point=0;
}
while(1);
}
void lcd_disp()
{
uchar a,i=0;
write_com(0x80);
i=0;
while(i<n)
{
a=tab_key[i];
write_data(tab[a]);
i++;
if(i==15)
{
write_com(0x80+0x40);
}
}
}
void lcd_init()
{
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
// write_com(0x80+0x40);
}
void write_com(uchar com)
{
lcdrs=0;
P2=com;
delay(3);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat)
{
lcdrs=1;
P2=dat;
delay(3);
lcden=1;
delay(5);
lcden=0;
}
void key_scan()
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee: key_manage1();display(0); break;
case 0xde: key_manage2();display(1); break;
case 0xbe: key_manage3();display(2); break;
case 0x7e: key_manage4();display(3); break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed: key_manage5();display(4); break;
case 0xdd: key_manage6();display(5); break;
case 0xbd: key_manage7();display(6); break;
case 0x7d: key_manage8();display(7); break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb: key_manage9();display(8); break;
case 0xdb: key_manage10();display(9); break;
case 0xbb: key_manage11();display(10); break;
case 0x7b: key_manage12();display(11); break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7: key_manage13();display(12); break;
case 0xd7: key_manage14();display(13); break;
case 0xb7: key_manage15();display(14); break;
case 0x77: key_manage16();display(15); break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
void display(unsigned char S)
{
P0 = table[S];
delay(200);
P2 = 0x7f;
}
void delay(uint k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=50;j>0;j--);
}
void key_manage1()
{
tab_key[n]=0;
n++;
}
void key_manage2()
{
tab_key[n]=1;
n++;
}
void key_manage3()
{
tab_key[n]=2;
n++;
}
void key_manage4()
{
tab_key[n]=3;
n++;
}
void key_manage5()
{
tab_key[n]=4;
n++;
}
void key_manage6()
{
tab_key[n]=5;
n++;
}
void key_manage7()
{
tab_key[n]=6;
n++;
}
void key_manage8()
{
tab_key[n]=7;
n++;
}
void key_manage9()
{
tab_key[n]=8;
n++;
}
void key_manage10()
{
tab_key[n]=9;
n++;
}
void key_manage11()
{
tab_key[n]=10;
n++;
}
void key_manage12()
{
tab_key[n]=11;
n++;
}
void key_manage13()
{
tab_key[n]=12;
n++;
}
void key_manage14()
{
tab_key[n]=13;
n++;
}
void key_manage15()
{
tab_key[n]=14;
n++;
}
void key_manage16()
{
tab_key[n]=15;
n++;
} |