//P1.7--P1.4(行)通過上拉電阻拉+5V,P1.3--P1.0(列)
//計數(shù)修正值:計95(i)次,每次9.988秒(方式1,T0,TH0=0xd9,TL0=0x09),0x42鍵為鍵盤鎖
#i nclude <reg51.h>
unsigned char key;h;j;a;x;i;k;m;n;o;p;b;
unsigned char code dtab[]={'0','1','2','3','4','5','6','7','8','9'};
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sfr Lcd=0xB0;
void delay(unsigned char x){
while(x-->0){
for (h=0;h<125;h++){;};}}
//write_instruction
void enable(void){
RS=0;RW=0;E=0;
delay(2);
E=1;
Lcd=a;
E=0;
}
//writelcddata
void read(void){
RW=0;E=0;RS=1;
delay(2);
E=1;
Lcd=a;
E=0;
}
//initial
void lcd(void){
a=0x01;enable();a=0x38;enable();
a=0x38;enable();a=0x0e;enable();
a=0x06;enable();}
//清屏
void cls(void){a=0x01;delay(2);enable();}
unsigned char kbscan(void) //確定哪個按鍵并得鍵值
{ unsigned char sccode,recode;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{delay(10);
if((P1&0xf0)!=0xf0)
{sccode=0xfe;
while((sccode&0x10)!=0)//0xfe左移四次,第五次時11101111停止掃描
{P1=sccode;
if ((P1&0xf0)!=0xf0)
{recode=(P1&0xf0)|0x0f;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}//while
}//if
}//if
return(0);
}
void option(void){ //按鍵處理
if (key==0x42) // 鍵盤鎖
{b=1;return;}
if (key==0x44)//地址向右
{ if (b==1)
{a+=1;
enable();}}
if (key==0x48)//地址向左
{ if (b==1)
{a-=1;
enable();}}
if (key==0x81)
{if (a==0x84) //+
{p+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[p];E=0;}
else if (a==0x85)
{o+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[o];E=0;}
else if (a==0x87)
{n+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[n];E=0;}
else if (a==0x88)
{m+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[m];E=0;}
else if (a==0x8a)
{k+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[k];E=0;}
else if (a==0x8b)
{j+=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[j];E=0;}}
if (key==0x82)
{if (a==0x84) //-
{p-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[p];E=0;}
else if (a==0x85)
{o-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[o];E=0;}
else if (a==0x87)
{n-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[n];E=0;}
else if (a==0x88)
{m-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[m];E=0;}
else if (a==0x8a)
{k-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[k];E=0;}
else if (a==0x8b)
{j-=1;RW=0;E=0;RS=1;delay(2);E=1;Lcd=dtab[j];E=0;}
}
if (key==0x84)
{ if (b==1)
{ET0=1;}}
}
//T0初始化
void timer0() interrupt 1
{i+=1;
if (i==95){
{i=0;j+=1;}
if (j==10)
{k+=1;j=0;}
if (k==6)
{k=0;m+=1;}
if (m==10)
{n+=1;m=0;}
if (n==6)
{n=0;o+=1;}
if (o==10)
{p+=1;o=0;}
if (p==2&&o==4)
{p=0;o=0;}
{a=0x8b;enable();a=dtab[j];read();
a=0x8a;enable();a=dtab[k];read();
a=0x89;enable();a=0x3a;read();
a=0x88;enable();a=dtab[m];read();
a=0x87;enable();a=dtab[n];read();
a=0x86;enable();a=0x3a;read();
a=0x85;enable();a=dtab[o];read();
a=0x84;enable();a=dtab[p];read();
}
{TH0=0xd9;TL0=0x09;}
}
else
{TH0=0xd9;TL0=0x09;}
}
void inishow(void){
a=0x84;enable();a=0x30;read();
a=0x85;enable();a=0x30;read();
a=0x86;enable();a=0x3a;read();
a=0x87;enable();a=0x30;read();
a=0x88;enable();a=0x30;read();
a=0x89;enable();a=0x3a;read();
a=0x8a;enable();a=0x30;read();
a=0x8b;enable();a=0x30;read();}
void main(void)
{lcd();cls();
TMOD=0x01;TH0=0xd9;TL0=0x09;EA=1;TR0=1;
inishow();
a=0x80;enable();
j=0;
for(;;){
key=kbscan();
delay(60000);
option();
}
}