#include <reg52.h>
unsigned char second,count;
#define uchar unsigned char
#define uint unsigned int
uchar jishu,shi,ge,i,j,sb,yy,cc;
uint speed,t,t1,speed1,money,ceshi,flag,minute,bao;
uchar table1[16]=" Miles X:000.0 "; //1602显示数据
uchar table2[16]=" 2.0/M $:000.0 ";
uchar code table3[16]=" STOP ";
uchar table4[16]=" wait T=00.00 ";
uchar code table5[16]=" welcome baby ";
uchar code table6[16]=" I'm available ";
uchar code table7[10]="0123456789";
//uchar code table8[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar m;
uchar k1;
sbit s1=P2^0;//
sbit s2=P2^1;
sbit s3=P2^2;
sbit s4=P2^3;
sbit start=P3^7;
sbit rs=P3^3; //1602RS控制位
sbit rw=P3^4; //1602RW控制位
sbit e =P3^5; //1602E 控制位
bit flag0,flag1,flag2,flag3;
void delay_50us(uint t)
{
uchar j1;
for(;t>0;t--)
for(j1=19;j1>0;j1--);
}
/************************************************************************/
/************** 1602显示初试部分 *****************/
/************************************************************************/
void write_com(uchar com) //函数功能:写指令
{
e=0;
rs=0;
rw=0;
P0=com;
delay_50us(10);
e=1;
delay_50us(20);
e=0;
}
void write_data(uchar dat) //函数功能:写数据
{
e=0;
rs=1;
rw=0;
P0=dat;
delay_50us(10);
e=1;
delay_50us(20);
e=0;
}
void init1602(void) //函数功能:初始化1602
{
delay_50us(300);
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x38);
write_com(0x01);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void LcdStyle()
{
uchar j2;
write_com(0x80);
for(j2=0;j2<16;j2++)
{
write_data(table1[j2]);
delay_50us(10);
}
write_com(0x80+0x40);
for(j2=0;j2<16;j2++)
{
write_data(table2[j2]);
delay_50us(10);
}
}
void LcdL1(uchar *p1)
{
uchar j2;
write_com(0x80);
for(j2=0;j2<16;j2++)
{
write_data(p1[j2]);
delay_50us(10);
}
}
void LcdL2(uchar *p2)
{
uchar j2;
write_com(0xC0);
for(j2=0;j2<16;j2++)
{
write_data(p2[j2]);
delay_50us(10);
}
}
/******************************************************************/
void fenli() //显示处理
{
table1[10]=table7[speed1/100];
table1[11]=table7[speed1%100/10];
table1[12]=table7[speed1%10];
table1[14]=table7[bao];
table4[10]=table7[minute/10];
table4[11]=table7[minute%10];
table4[13]=table7[second/10];
table4[14]=table7[second%10];
table2[10]=table7[money/1000];
table2[11]=table7[money%1000/100];
table2[12]=table7[money%100/10];
table2[14]=table7[money%10];
//b1=jishu/1000; //测试用
//b2=jishu%1000/100;
//b3=jishu%100/10;
//b4=jishu%10;
}
void init()
{
TMOD=0x10;//定时器设置 10ms in 12M crystal
TH1=0xd8;
TL1=0xf0;
//IE=0xae; //打开中断
ET1=1;
EX0=1;
EA=1;
TR1=1;
IT0=1;
//start=0;
flag0=0;
flag1=0;
flag2=0;
flag3=0;
flag=0;
speed=0;
t=0;
speed1=0;
sb=20;
start=1;
}
void keyscan()
{
if(s1==0)
{
while(s1==0);
P1=0xff;
flag0=!flag0;
if(flag0)
{
EA=0;
}
else
{
EA=1;
LcdL1(table1);
}
}
if(s2==0)
{
while(s2==0);
P1=0x00;
flag1=!flag1;
}
if(s3==0)
{
while(s3==0);
P1=0xfe;
flag2=!flag2;
if(flag2)
{
sb=15;
table2[1]=table7[1];
table2[3]=table7[5];
}
else
{
sb=20;
table2[1]=table7[2];
table2[3]=table7[0];
}
}
if(s4==0)
{
while(s4==0);
P1=0x01;
flag3=!flag3;
}
}
void jisuan()
{
//speed1=speed1+speed; //每走1米相当于增加一公里,即2圈。
// speed=0;
if(speed1<3)
money=50;
if(speed1>=3)
{
money=50+(speed1-3)*sb+bao*sb/10;
}
}
main()
{
//P3^7=0;
init();
init1602();
LcdStyle();
while(1)
{
keyscan();
jisuan();
fenli();
if(flag1)
LcdL2(table4);
else
LcdL2(table2);
if(flag0)
LcdL1(table3);
else
LcdL1(table1);
while(flag3)
{
second=0;
minute=0;
money=0;
speed1=0;
LcdL1(table5);
LcdL2(table6);
if(s4==0)
{
while(s4==0);
flag3=!flag3;
second=0;
minute=0;
money=0;
speed1=0;
}
}
}
}
/********************************/
/* 定时中断 */
/********************************/
void tim(void) interrupt 3 using 1
{
TH1=0xd8;//重新赋值
TL1=0xf0;
t++;
if(t>=100) //速度检测单位为秒
{
t=0;
jishu=jishu/2;//两圈算一米。
if(jishu<5)
flag=0;
else
flag=1;
jishu=0;
}
if(flag==0) //等待计时
{
count++;
if (count==100)
{
count=0;
second++;//秒加1
if(second==60)
{
minute++;
yy++;
second=0;
if(yy==2)
{
speed1++;
yy=0;
}
}
}
}
//jisuan();
if(speed==5)
{
bao++;
if(bao==10)
{
bao=0;
speed1++;
}
speed=0;
}
}
void zhongduan() interrupt 0
{
jishu++;
cc=!cc;
if(flag) speed++;
ceshi++;
if(cc)
P1=0xaa;
else
P1=0x55;
}
评论0