速度里程都能正常显示,只是存储总里程有问题,由于课题要求总里程最大值9999.99km,所以里程要分成四部分分别写进存储器。每次读的时候数值都不对,不知道是读还是写的问题,或者是其他问题。大神帮忙看看。
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- sbit lcd_rs = P1^0;
- sbit lcd_rw = P1^1;
- sbit lcd_en = P1^2;
- sbit scl=P3^6;
- sbit sda=P3^7;
- sbit key1=P2^0;
- sbit key2=P2^1;
- sbit key3=P2^2;
- sbit key4=P2^3;
- uint speed;
- uint ave_speed;
- ulong mileage;
- ulong all_mileage;
- uchar eeprom_mileage;
- uchar str_speed[16];
- uchar str_ave_speed[16];
- uchar str_mileage[16];
- uchar str_all_mileage[16];
- uint int1_count;
- uint all_int1_count;
- uint int1_count100;
- uint time;
- uint all_time;
- bit start;
- uchar key;
- uchar q_count;
- uchar de;
- void eeprom_init(); //存储读写
- void eeprom_start();
- void eeprom_stop();
- void eeprom_ack();
- void eeprom_noack();
- void eeprom_write(uchar dat);
- uchar eeprom_read();
- void eeprom_writedat(uchar add,uchar dat);
- uchar eeprom_readdat(uchar add);
- bit lcd_busy(); //lcd显示
- void lcd_wcmd(uchar cmd);
- void lcd_wdat(uchar dat);
- void lcd_init();
- void lcd_pos(uchar pos);
- void display(uchar str1[],uchar str2[]);
- void str_speed_init(); //修改显示缓冲区
- void str_ave_speed_init();
- void str_mileage_init();
- void str_all_mileage_init();
- void str_change();
- void mileage_clean();
- void system_init();
- void system_start();//系统启动,停止
- void system_stop();
- void write(uint dat);//
- /*************延时函数*********************/
- void delayNOP()
- {
- _nop_();_nop_();_nop_();_nop_();
- }
- void delay()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- void delayms(uchar x)
- {
- uchar i,j;
- for(;x>0;x--)
- for(i=4;i>0;i--)
- for(j=113;j>0;j--);
- }
- void delay10ms(uchar x)
- {
- uchar i,j;
- for(;x>0;x--)
- for(i=151;i>0;i--)
- for(j=29;j>0;j--);
- }
- void delay1s(uchar x)
- {
- uchar a,b,c;
- for(;x>0;x--)
- {
- for(c=46;c>0;c--)
- for(b=152;b>0;b--)
- for(a=70;a>0;a--);
- _nop_();
- }
- }
- /*************延时函数*********************/
- /*************中断函数*********************/
- void int0() interrupt 0
- {
- int1_count++;
- int1_count100++;
- all_int1_count++;
- }
- void int1() interrupt 2
- {
- delay10ms(2);
- if(key1==0)
- {
- key=1;
- }else if(key2==0)
- {
- key=2;
- }else if(key3==0)
- {
- key=3;
- }else if(key4==0)
- {
- key=4;
- }
- }
- void timer0() interrupt 1
- {
- TL0=(65536-50000)%256;
- TH0=(65536-50000)/256;
- time++;
- if(time>=20)
- {
- all_time++;
- speed=0.6*3.14*int1_count/36*1000;
- mileage=0.6*3.14*all_int1_count;
- ave_speed=mileage/36*1000/all_time;
- all_mileage=eeprom_mileage+mileage;
- de=0;
- int1_count=0;
- time=0;
- }
- }
- /*************中断函数*********************/
- /*************EEPROM存储函数*********************/
- void eeprom_start(void)
- {
- sda=1;
- delay();
- scl=1;
- delay();
- sda=0;
- delay();
- }
- void eeprom_stop(void)
- {
- sda=0;
- delay();
- scl=1;
- delay();
- sda=1;
- delay();
- }
- void eeprom_ack(void)
- {
- uchar i;
- scl=1;
- delay();
- while((sda==1)&&(i<200))i++;
- scl=0;
- delay();
- }
- void eeprom_noack(void)
- {
- sda=1;
- delay();
- scl=1;
- delay();
- scl=0;
- delay();
- }
- void eeprom_init(void)
- {
- sda=1;
- scl=1;
- }
- void eeprom_write(uchar dat)
- {
- uchar i;
- scl=0;
- for(i=0;i<8;i++)
- {
- if(dat&0x80)
- {
- sda=1;
- }
- else
- {
- sda=0;
- }
- dat=dat<<1;
- delay();
- scl=1;
- delay();
- scl=0;
- delay();
- }
- sda=1;
- delay();
- }
- uchar eeprom_read(void)
- {
- uchar i;
- uchar dat;
- scl=0;
- delay();
- sda=1;
- delay();
- for(i=0;i<8;i++)
- {
- scl=1;
- delay();
- dat=dat<<1;
- if(sda)
- {
- dat++;
- }
- scl=0;
- delay();
- }
- return dat;
- }
- void eeprom_writedat(uchar add,uchar dat)
- {
- eeprom_init();
- eeprom_start();
- eeprom_write(0xa0);
- eeprom_ack();
- eeprom_write(add);
- eeprom_ack();
- eeprom_write(dat);
- eeprom_ack();
- eeprom_stop();
- }
- uchar eeprom_readdat(uchar add)
- {
- uchar a;
- eeprom_init();
- eeprom_start();
- eeprom_write(0xa0);
- eeprom_ack();
- eeprom_write(add);
- eeprom_ack();
- eeprom_start();
- eeprom_write(0xa1);
- eeprom_ack();
- a=eeprom_read();
- eeprom_noack();
- eeprom_stop();
- return a;
- }
- /*************EEPROM存储函数*********************/
- /*************LCD函数*********************/
- bit lcd_busy()
- {
- bit result;
- lcd_rs = 0;
- lcd_rw = 1;
- lcd_en = 1;
- delayNOP();
- result = (bit)(P0&0x80);
- lcd_en = 0;
- return result;
- }
- void lcd_wcmd(uchar cmd)
- {
- while(lcd_busy());
- lcd_rs = 0;
- lcd_rw = 0;
- lcd_en = 0;
- _nop_();
- _nop_();
- P0 = cmd;
- delayNOP();
- lcd_en = 1;
- delayNOP();
- lcd_en = 0;
- }
- void lcd_wdat(uchar dat)
- {
- while(lcd_busy());
- lcd_rs = 1;
- lcd_rw = 0;
- lcd_en = 0;
- P0 = dat;
- delayNOP();
- lcd_en = 1;
- delayNOP();
- lcd_en = 0;
- }
- void lcd_init()
- {
- delayms(15);
- lcd_wcmd(0x38);
- delayms(5);
- lcd_wcmd(0x38);
- delayms(5);
- lcd_wcmd(0x38);
- delayms(5);
-
- lcd_wcmd(0x0c);
- delayms(5);
- lcd_wcmd(0x06);
- delayms(5);
- lcd_wcmd(0x01);
- delayms(5);
- }
- void lcd_pos(uchar pos)
- {
- lcd_wcmd(pos | 0x80);
- }
- void display(uchar str1[],uchar str2[])
- {
- uchar m;
- lcd_init();
- lcd_pos(0);
- m=0;
- while(str1[m] != '\0')
- {
- lcd_wdat(str1[m]);
- m++;
- }
- lcd_pos(0x40);
- m=0;
- while(str2[m] != '\0')
- {
- lcd_wdat(str2[m]);
- m++;
- }
- }
- /*************LCD函数*********************/
- /*************修改显示缓冲区函数*********************/
- void str_speed_init()
- {
- str_speed[0]='S';
- str_speed[1]='p';
- str_speed[2]='e';
- str_speed[3]='e';
- str_speed[4]='d';
- str_speed[5]=':';
- str_speed[6]=(speed%100000)/10000+0x30;
- str_speed[7]=(speed%10000)/1000+0x30;
- str_speed[8]=(speed%1000)/100+0x30;
- str_speed[9]='.';
- str_speed[10]=(speed%100)/10+0x30;
- str_speed[11]=speed%10+0x30;
- str_speed[12]='k';
- str_speed[13]='m';
- str_speed[14]='/';
- str_speed[15]='h';
- }
- void str_ave_speed_init()
- {
- str_ave_speed[0]='A';
- str_ave_speed[1]='v';
- str_ave_speed[2]='e';
- str_ave_speed[3]='-';
- str_ave_speed[4]='S';
- str_ave_speed[5]=':';
- str_ave_speed[6]=(ave_speed%100000)/10000+0x30;
- str_ave_speed[7]=(ave_speed%10000)/1000+0x30;
- str_ave_speed[8]=(ave_speed%1000)/100+0x30;
- str_ave_speed[9]='.';
- str_ave_speed[10]=(ave_speed%100)/10+0x30;
- str_ave_speed[11]=ave_speed%10+0x30;
- str_ave_speed[12]='k';
- str_ave_speed[13]='m';
- str_ave_speed[14]='/';
- str_ave_speed[15]='h';
- }
- void str_mileage_init()
- {
- str_mileage[0]='M';
- str_mileage[1]='i';
- str_mileage[2]='l';
- str_mileage[3]='e';
- str_mileage[4]='a';
- str_mileage[5]='g';
- str_mileage[6]=':';
- str_mileage[7]=(mileage%1000000)/100000+0x30;
- str_mileage[8]=(mileage%100000)/10000+0x30;
- str_mileage[9]=(mileage%10000)/1000+0x30;
- str_mileage[10]=(mileage%1000)/100+0x30;
- str_mileage[11]='.';
- str_mileage[12]=(mileage%100)/10+0x30;
- str_mileage[13]=mileage%10+0x30;
- str_mileage[14]='k';
- str_mileage[15]='m';
-
- }
- void str_all_mileage_init()
- {
- str_all_mileage[0]='A';
- str_all_mileage[1]='l';
- str_all_mileage[2]='l';
- str_all_mileage[3]='-';
- str_all_mileage[4]='M';
- str_all_mileage[5]='i';
- str_all_mileage[6]=':';
- str_all_mileage[7]=(all_mileage%1000000)/100000+0x30;
- str_all_mileage[8]=(all_mileage%100000)/10000+0x30;
- str_all_mileage[9]=(all_mileage%10000)/1000+0x30;
- str_all_mileage[10]=(all_mileage%1000)/100+0x30;
- str_all_mileage[11]='.';
- str_all_mileage[12]=(all_mileage%100)/10+0x30;
- str_all_mileage[13]=all_mileage%10+0x30;
- str_all_mileage[14]='k';
- str_all_mileage[15]='m';
-
- }
- void str_change()
- {
- str_speed_init();
- str_ave_speed_init();
- str_mileage_init();
- str_all_mileage_init();
- if(q_count)
- {
- display(str_ave_speed,str_all_mileage);
- }else if(~q_count)
- {
- display(str_speed,str_mileage);
- }
- }
- /*************修改显示缓冲区函数*********************/
- /*************系统初始化函数*********************/
- void system_init()
- {
- EA=1;
- EX1=1;
- IT1=1;
- }
- /*************系统初始化函数*********************/
- /*************启动函数*********************/
- void system_start()
- {
- EA=1;
-
- EX0=1;
- IT0=1;
- TMOD=0x01;
- ET0=1;
- TR0=1;
- TL0=(65536-50000)%256;
- TH0=(65536-50000)/256;
-
-
-
- eeprom_mileage = eeprom_readdat(00)+eeprom_readdat(01)*256+eeprom_readdat(02)*65536+eeprom_readdat(03)*256*65536;
- }
- void system_stop()
- {
- EA=0;
- lcd_wcmd(0x01);
- }
- /*************启动函数*********************/
- /*************里程清零函数*********************/
- void mileage_clean()
- {
- write(all_mileage);
- delay10ms(3);
- eeprom_mileage=all_mileage;
- mileage=0;
- all_int1_count=0;
- }
- /*************里程清零函数*********************/
- void write(uint dat)
- {
- eeprom_writedat(00,dat%65536%256);
- delay10ms(3);
- eeprom_writedat(01,dat%65536/256);
- delay10ms(3);
- eeprom_writedat(02,dat/65536%256);
- delay10ms(3);
- eeprom_writedat(03,dat/65536/256);
- delay10ms(3);
- }
- void main()
- {
- while(1)
- {
- if(~start)
- {
- system_init();
- }
- if(int1_count100>=100)
- {
- write(all_mileage);
- int1_count100=0;
- }
- switch(key)
- {
- case 1:if(~start)
- {
- key=0;
- start=~start;
- system_start();
- str_change();
- }
- break;
- case 2: if(start)
- {
- key=0;
- start=~start;
- system_stop();
- }
- break;
- case 3:if(start)
- {
- key=0;
- q_count=~q_count;
- }
- str_change();
- break;
- case 4: if(start)
- {
- key=0;
- mileage_clean();
- str_change();
- }
- break;
- default:if(start)
- {
- de=1;
- key=0;
- str_change();
- while(1&de){}
- }
- break;
- }
- }
- }
复制代码
|