#include<reg52.h>
#define bike_cm 66.04
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit rw=P2^1;
sbit ep=P2^2;
uchar code table[] ="rate";
uchar code table1[]="km/h";
uchar hou,min,sec,tt,num,q;
uint rate,su;
float mile,bike_fm;
/* 延时程序 */
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*写入指令数据到LcD */
void write_com(uchar com)
{
rs = 0;
ep = 0;
P0=com;
delay(5);
ep = 1;
delay(5);
ep = 0;
}
/* 写入字符显示数据到LcD */
void write_data(uchar date)
{
rs =1;
P0=date;
delay(5);
ep = 1;
delay(5);
ep = 0;
}
//把那些数字转化成能显示出来的格式
void lcd_sfm(uchar add,uchar aa)
{
uchar shi,ge;
shi=aa/10;
ge =aa%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void lcd_sfm1(uint i)
{
uchar qian,bai,shi,ge;
qian=i/1000;
bai=i/100-qian*10;
shi=i/10-qian*100-bai*10;
ge =i%10;
write_com(0x80+0x45);
write_data(0x30+qian);
write_data(0x30+bai);
write_com(0x80+0x48);
write_data(0x30+shi);
write_data(0x30+ge);
}
void lcd_sfm2(uint i)
{
uchar wang,qian,bai,shi,ge;
wang=i/10000;
qian=i/1000-wang*10;
bai=i/100-wang*100-qian*10;
shi=i/10-wang*1000-qian*100-bai*10;
ge =i%10;
write_com(0x80+0x09);
write_data(0x30+wang);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_com(0x80+0x0e);
write_data(0x30+ge);
}
/* LcD初始化设定 */
void init()
{
bike_fm=bike_cm/10 ;
q=0;
su=0;
tt=0;
rw = 0;
write_com(0x38); // 显示模式设置
write_com(0x0c); // 显示开关及光标设置
write_com(0x06); // 显示光标设置
write_com(0x01); // 显示清屏设置
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EX0=1;
IT0=1;
EA=1;
}
void main()
{
init();
write_com(0x80+0x02); //write_com1(0x02);
write_data(':');
write_com(0x80+0x05);
write_data(':');
write_com(0x80+0x0f);
write_data('m');
write_com(0x80+0x0d);
write_data('.');
write_com(0x80+0x47);
write_data('.');
write_com(0x80+0x40);
for(num=0;num<4;num++)
write_data(table[num]);
write_com(0x80+0x4B );
for(num=0;num<4;num++)
write_data(table1[num]);
while(1)
{
lcd_sfm(0,hou);
lcd_sfm(3,min);
lcd_sfm(6,sec);
lcd_sfm1(rate);
lcd_sfm2(mile);
}
}
//传感器中断
void time0() interrupt 0
{
rate=72*bike_cm/su;
if(q==2)
mile+=bike_fm;
su=0;
if(q==0)
q+=1;
}
//时钟中断
void isrtimer0() interrupt 1
{
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
tt++;
su++;
if(q==1)
{su=0;
q+=1;}
if(tt==20)
{
tt=0;
sec++;
if(sec==60)
{
min++;
sec=0;
if(min==60)
{
hou++;
min=0;
if(hou==10)
hou=0;
}
}
}
}