- #include <reg52.h>
- #include <intrins.h>
- #include <stdlib.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define AD_data P1
- #define LCD_data P0
- sbit intr=P3^0; //AD0804
- sbit adwr=P3^2; //AD0804写信号
- sbit adrd=P3^1; //AD0804读信号
- sbit LCD_RS=P2^5;//lcd
- sbit LCD_RW=P2^6; //lcd
- sbit LCD_EN=P2^7; //lcd
- sbit s1=P3^3; //按键,单程公里清零
- sbit scl=P3^6; //24c02
- sbit sda=P3^7; //24c02
- uchar code dis1[]={"筿溃"};
- uchar code dis2[]={"虫祘"};
- uchar code dis3[]={"羆祘"};
- uchar code dis4[]={"承禤搂筿笆ó "};
- uchar code dis5[]={"."};
- uchar code dis6[]={"V"};
- uchar code dis7[]={"KM"};
- uchar a,b,c,d,e,f,g,y,z;
- ulong num,num1;
- uint zongcheng,dancheng,zongcheng1,dancheng1;
- void delay()
- {;;}
-
- void delayms(uint xms) //延时
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void AD_init()//ad0804初始化
- {
- adwr=1;
- adrd=1;
- }
- void start()//开始
- {
- sda=1;
- delay();
- scl=1;
- delay();
- sda=0;
- delay();
- }
- void stop()//停止
- {
- sda=0;
- delay();
- scl=1;
- delay();
- sda=1;
- delay();
- }
- void respons()//应答
- {
- uchar i;
- scl=1;
- delay();
- while((sda==1)&&(i<250))i++;
- scl=0;
- delay();
- }
- void init()//初始化
- {
- sda=1;
- delay();
- scl=1;
- delay();
- }
- void write_byte(uchar date )//写1字
- {
- uchar i,temp;
- temp=date;
- for(i=0;i<8;i++)
- {
- temp=temp<<1;
- scl=0;
- delay();
- sda=CY;
- delay();
- scl=1;
- delay();
- }
- scl=0;
- delay();
- sda=1;
- delay();
- }
- uchar read_byte()//读1字
- {
- uchar i,k;
- scl=0;
- delay();
- sda=1;
- delay();
- for(i=0;i<8;i++)
- {
- scl=1;
- delay();
- k=(k<<1)|sda;
- scl=0;
- delay();
- }
- return k;
- }
- void write_add(uchar address,uchar date)//写字节
- {
- start();
- write_byte(0xa0);
- respons();
- write_byte(address);
- respons();
- write_byte(date);
- respons();
- stop();
- }
- uchar reda_add(uchar address)//读字节
- {
- uchar date;
- start();
- write_byte(0xa0);
- respons();
- write_byte(address);
- respons();
- start();
- write_byte(0xa1);
- respons();
- date=read_byte();
- stop();
- return date;
- }
- void write_cmd(uchar cmd)//写指令到lcd
- {
- LCD_RS=0;
- LCD_RW=0;
- LCD_EN=0;
- P0=cmd;
- delayms(5);
- LCD_EN=1;
- delayms(5);
- LCD_EN=0;
- }
- void write_dat(uchar dat)//写显示数据到lcd
- {
- LCD_RS=1;
- LCD_RW=0;
- LCD_EN=0;
- P0=dat;
- delayms(5);
- LCD_EN=1;
- delayms(5);
- LCD_EN=0;
- }
- void put_str(char i,char j,unsigned char date) //指针定位;
- {
- if(i==1)
- write_cmd(0x83+j-1);
- // else write_cmd(0xc0+j-1);
- write_dat('0'+date) ;
- }
- void ADqd() //ad0808启动
- {
- float i=1;
- uint j;
- uchar ge,shi,bai;
- int a[8];
- P1=0xff;
- intr=1;
- adwr=1;
- _nop_();
- adwr=0;
- _nop_();
- adwr=1;
- while(intr==1);
- adrd=1;
- _nop_();
- adrd=0;
- _nop_();
- i=P1;
- a[0]=P1^0;
- a[1]=P1^1;
- a[2]=P1^2;
- a[3]=P1^3;
- a[4]=P1^4;
- a[5]=P1^5;
- a[6]=P1^6;
- a[7]=P1^7;
- i=i/255;
- i*=6;
- i*=100;
- j=(int)i;
- ge=j%10;
- put_str(1,4,ge);
- j-=ge;
- shi=j/10;
- shi=shi%10;
- put_str(1,2,shi);
- bai=j/100;
- put_str(1,1,bai);
- for(i=0;i<8;i++);
- delayms(10);
- adrd=1;
- }
- void lcd_init()//lcd初始化
- {
- LCD_RW=0;
- write_cmd(0x30);
- delayms(5);
- write_cmd(0x0c);
- delayms(5);
- write_cmd(0x06);
- delayms(5);
- write_cmd(0x01);
- delayms(5);
- }
- void lcd_pos(uchar X,uchar Y)//设定汉字显示位置
- {
- uchar pos;
- if(X==0)
- {X=0x80;}
- else if(X==1)
- {X=0x90;}
- else if(X==2)
- {X=0x88;}
- else if(X==3)
- {X=0x98;}
- else if(X==4)
- {X=0x85;}
- else if(X==5)
- {X=0x87;}
- else if(X==6)
- {X=0x97;}
- else if(X==7)
- {X=0x8f;}
- pos=X+Y;
- write_cmd(pos);
- }
- void main()
- {
- uchar i;
- init();
- a=reda_add(1);
- delayms(5);
- b=reda_add(2);
- delayms(5);
- c=reda_add(3);
- delayms(5);
- d=reda_add(4);
- delayms(5);
- e=reda_add(5);
- delayms(5);
- f=reda_add(11);
- delayms(5);
- g=reda_add(12);
- delayms(5);
- y=reda_add(13);
- delayms(5);
- z=reda_add(14);
- delayms(5);
- zongcheng1=(a*10000)+(b*1000)+(c*100)+(d*10)+e;
- dancheng1=(f*1000)+(g*100)+(y*10)+z;
-
- TMOD=0X55;//设T0,T1为计数
- TL0=0xff;
- TH0=0xff;
- TL1=0xff;
- TH1=0xff;
- TR0=1;
- TR1=1;
- EA=1;
- ET0=1;
- ET1=1;
- lcd_init();
- lcd_pos(0,0);
- i=0;
- while(dis1[i]!='\0')
- {
- write_dat(dis1[i]);
- i++;
- }
-
- lcd_pos(1,0);
- i=0;
- while(dis2[i]!='\0')
- {
- write_dat(dis2[i]);
- i++;
- }
- lcd_pos(2,0);
- i=0;
- while(dis3[i]!='\0')
- {
- write_dat(dis3[i]);
- i++;
- }
-
-
- lcd_pos(3,0);
- i=0;
- while(dis4[i]!='\0')
- {
- write_dat(dis4[i]);
- i++;
- }
- lcd_pos(4,0);
- i=0;
- while(dis6[i]!='\0')
- {
- write_dat(dis5[i]);
- i++;
- }
- lcd_pos(5,0);
- i=0;
- while(dis6[i]!='\0')
- {
- write_dat(dis6[i]);
- i++;
- }
- lcd_pos(6,0);
- i=0;
- while(dis7[i]!='\0')
- {
- write_dat(dis7[i]);
- i++;
- }
- lcd_pos(7,0);
- i=0;
- while(dis7[i]!='\0')
- {
- write_dat(dis7[i]);
- i++;
- }
-
- while(1)
- {
- AD_init();
- delayms(50);
- ADqd();
- delayms(50);
- zongcheng=num*1.3/1000+zongcheng1;//16寸轮胎周长为130cm
- a=zongcheng/10000;
- b=zongcheng%10000/1000;
- c=zongcheng%1000/100;
- d=zongcheng%100/10;
- e=zongcheng%10;
- put_str(1,8,a);
- put_str(1,9,b);
- put_str(1,10,c);
- put_str(1,11,d);
- put_str(1,12,e);
-
-
- dancheng=num1*1.3/1000+dancheng1;
- f=dancheng%10000/1000;
- g=dancheng%1000/100;
- y=dancheng%100/10;
- z=dancheng%10;
-
- put_str(1,17,f);
- put_str(1,18,g);
- put_str(1,19,y);
- put_str(1,20,z);
- if(s1==0)
- {
-
- dancheng=0;
- dancheng1=0;
- }
- write_add(1,a);
- delayms(5);
- write_add(2,b);
- delayms(5);
- write_add(3,c);
- delayms(5);
- write_add(4,d);
- delayms(5);
- write_add(5,e);
- delayms(5);
- write_add(11,f);
- delayms(5);
- write_add(12,g);
- delayms(5);
- write_add(13,y);
- delayms(5);
- write_add(14,z);
- delayms(5);
- }
- }
- void T0_time()interrupt 1//总程中断计数
- {
- TH0=0xff;
- TL0=0xff;
- num++;
- }
-
- void T1_time()interrupt 3//单程中断计数
- {
- TH1=0xff;
- TL1=0xff;
- num1++;
- }
复制代码 |