51驱动1602液晶+按键 protues仿真
程序:
#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按一下,开端设置液晶左下方00,然后可按键加减(00-20);
p1.2再按一下,开端设置液晶第二个00,然后可按键加减(00-38);
p1.2再按一下,开端设置液晶第三个参数0,然后可按键加减(0-8);
p1.2再按一下,最终一位编程1,表明设置完结;
可长期按加减键,接连加减;
以上操作可重复。
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/259216.html