51驅(qū)動(dòng)1602液晶+按鍵 protues仿真
程序:
本文引用地址:http://m.butianyuan.cn/article/201611/321330.htm#include
#define uchar unsigned char
#define uint unsigned int
unsigned char Trg;
unsigned char Cont;
sbit lcdrs=P0^4;
sbit lcden=P0^6;
uchar code table1[]="20121029SAT12:00";
uchar code table2[]="0123456789";
void delay(uint z)
{
uint x,y;
for(x=0;x for(y=0;y<110;y++)
;
}
void KeyRead( void )
{
unsigned char ReadData = P1^0xff; // 1
Trg = ReadData & (ReadData ^ Cont); // 2
Cont = ReadData; // 3
}
void write_com(uchar com)
{
lcdrs=0;
P2=com;
delay(2);
lcden=1;
lcden=0;
}
void write_data(uchar dat)
{
lcdrs=1;
P2=dat;
delay(2);
lcden=1;
lcden=0;
}
void lcd1602_inti()
{
write_com(0x38);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
delay(5);
write_com(0x01);//清屏
delay(5);
}
void main()
{
uchar i,x,y,z,w,p=0,m,a,s,d;
lcd1602_inti();
write_com(0x01);
while(1)
{ KeyRead();
write_com(0x80+0x00);
for(i=0;i<4;i++)
{
write_data(table1[i]);
delay(5);
}
write_com(0x80+0x40);
write_data(table2[x]);
delay(10);
write_data(table2[y]);
delay(10);
write_com(0x80+0x43);
write_data(table2[z]);
delay(10);
write_data(table2[w]);
delay(10);
write_com(0x80+0x46);
write_data(table2[d]);
delay(10);
write_com(0x80+0x48);
write_data(table2[m]);
delay(10);
if(Trg==0x01)
{
switch(p%4)
{ case 1: { a++;
if(a==21)
a=0;
} break;
case 2:{s++;
if(s==39)
s=0;
} break;
case 3: {d++;
if(d==9)
d=0;
} break;
}
x=a/10;y=a;
z=s/10;w=s;
}
if(Trg==0x02)
{
switch(p%4)
{ case 1: { a--;
if(a==0)
a=20;
} break;
case 2:{s--;
if(s==0)
s=38;
} break;
case 3: {d--;
if(d==0)
d=8;
} break;
}
x=a/10;y=a;
z=s/10;w=s;
}
if(Trg==0x04)
{p++;
if(p==9)
{p=0;}
if(p%4==0)
{m=1;}else m=0;
}
delay(50);
if((Trg==0x00)&&(Cont==0x01))
{ switch(p%4)
{ case 1: {a++;delay(60);
if(a==21)
a=0;
} break;
case 2: {s++;delay(60);
if(s==39)
s=0;
} break;
case 3: {d++; delay(60);
if(d==9)
d=0;
}break;
}
x=a/10;y=a;
z=s/10;w=s;
}
if((Trg==0x00)&&(Cont==0x02))
{
{ switch(p%4)
{ case 1: {a--;delay(60);
if(a==0)
a=20;
} break;
case 2: {s--;delay(60);
if(s==0)
s=38;
} break;
case 3: {d--; delay(60);
if(d==0)
d=8;
}break;
}
x=a/10;y=a;
z=s/10;w=s;
}
}
}
}
按鍵部分功能:
P1.2:功能鍵
p1.1:減
p1.0:加
p1.2按一下,開始設(shè)置液晶左下方00,然后可按鍵加減(00-20);
p1.2再按一下,開始設(shè)置液晶第二個(gè)00,然后可按鍵加減(00-38);
p1.2再按一下,開始設(shè)置液晶第三個(gè)參數(shù)0,然后可按鍵加減(0-8);
p1.2再按一下,最后一位編程1,表示設(shè)置完成;
可長(zhǎng)時(shí)間按加減鍵,連續(xù)加減;
以上操作可重復(fù)。
評(píng)論