百米計時器LCD1602顯示
#include
#define uchar unsigned char
#define uint unsigned int
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit k5=P3^4;
sbit k6=P3^5;
sbit k7=P3^6;
sbit k8=P3^7;
sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;
uchar count;
uint tim;
void usdelay()
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rw=0;
rs=0;
en=0;
P0=com;
delayms(2);
en=1;
delayms(2);
en=0;
delayms(2);
}
void write_dat(uchar dat)
{
rw=0;
rs=1;
en=0;
P0=dat;
delayms(2);
en=1;
delayms(2);
en=0;
delayms(2);
}
void write_zfc(uchar *p)
{
while(*p!=0)
write_dat(*p++);
}
void init_1602()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+1);
write_zfc("sec : .");
write_com(0x80+12);
write_dat(S);
write_com(0x80+0x40+1);
write_zfc("the last:");
}
void display()
{
uchar wan,qian,bai,shi,ge;
wan=tim/10000;
qian=(tim-wan*10000)/1000;
bai=(tim-wan*10000-qian*1000)/100;
shi=(tim-wan*10000-qian*1000-bai*100)/10;
ge=tim;
write_com(0x80+6);
write_dat(0x30+bai);
write_dat(0x30+shi);
write_com(0x80+9);
write_dat(0x30+ge);
}
void display111()
{
uchar wan,qian,bai,shi,ge;
wan=tim/10000;
qian=(tim-wan*10000)/1000;
bai=(tim-wan*10000-qian*1000)/100;
shi=(tim-wan*10000-qian*1000-bai*100)/10;
ge=tim;
write_com(0x80+0x40+10);
write_dat(0x30+bai);
write_dat(0x30+shi);
write_com(0x80+0x40+12);
write_dat(.);
write_com(0x80+0x40+13);
write_dat(0x30+ge);
}
void main()
{
init_1602();
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
EA=1;
ET0=1;
TR0=1;
display();
while(1)
{
if(k3==0)
display111();
if(k7==0)
{
delayms(50);
TR0=~TR0;
}
if(count==2)
{
count=0;
tim++;
}
display();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
count++;
}
評論