单片机源码:
- #include<reg52.h>
- sbit LCD_RS=P1^0;
- sbit LCD_RW=P1^1;
- sbit LCD_E=P1^2;
- #define LCD_DATA P2
- #define LCD_BUSY 0x80
- #define uchar unsigned char
- #define uint unsigned int
- uint speed;
- uchar test=1;
- uchar normal=0;
- uchar nor_speed;
- uchar exc_speed;
- uchar cnt_ovtime;
- uint data dis[5]={0x00,0x00,0x00,0x00,0x00};
- void lcd_init(void);
- void display_string(uchar x,uchar y,uchar *s);
- void disp_selec(uchar bit_selec,uchar seg);
- void init_timer0();
- void init_timer1();
- void delay_ms(uint i)
- {
- uint j;
- while(i--)
- {
- for(j=0;j<125;j++);
- }
- }
- void dellay(uint h)
- {
- while(h--);
- }
- void init_timer0()
- {
- TMOD=0x51;
- TH0=0xD8;
- TL0=0xF0;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void init_timer1()
- {
- TMOD=0x51;
- TH1=0x00;
- TL1=0x00;
- ET1=1;
- EA=1;
- TR1=1;
- }
- void it_timer0() interrupt 1
- {
- uchar counter;
- TF0=0;
- TH0=0xD8;
- TL0=0xF0;
- counter++;
- if(counter==100)
- {
- speed=(TH1*256+TL1+65536*cnt_ovtime)/100;
- if(speed>=1200)
- {
- exc_speed=1;
- nor_speed=0;
- }
- else
- {
- nor_speed=1;
- exc_speed=0;
- }
- TH1=TL1=0x00;
- counter=0;
- cnt_ovtime=0;
- }
- }
- void it_timer1()interrupt 3
- {
- TF1=0;
- cnt_ovtime++;
- }
- void WriteDataLcd(uchar wdata)
- {
- LCD_DATA=wdata;
- LCD_RS=1;
- LCD_RW=0;
- LCD_E=0;
- dellay(100);
- LCD_E=1;
- }
- void WriteCommandLcd(uchar wdata)
- {
- LCD_DATA=wdata;
- LCD_RS=0;
- LCD_RW=0;
- LCD_E=0;
- dellay(100);
- LCD_E=1;
- }
- void lcd_init(void)
- {
- LCD_DATA=0;
- WriteCommandLcd(0x38);
- dellay(1000);
- WriteCommandLcd(0x38);
- WriteCommandLcd(0x08);
- WriteCommandLcd(0x01);
- WriteCommandLcd(0x06);
- WriteCommandLcd(0x0c);
- }
- void display_xy(uchar x,uchar y)
- {
- if(y==1)
- x+=0x40;
- x+=0x80;
- WriteCommandLcd(x);
- }
- void display_char(uchar x,uchar y,uchar dat)
- {
- display_xy(x,y);
- WriteDataLcd(dat);
- }
- void display_string(uchar x,uchar y,uchar *s)
- {
- display_xy(x,y);
- while(*s)
- {
- WriteDataLcd(*s);
- s++;
- }
- }
- void main()
- {
- lcd_init();
- init_timer0();
- init_timer1();
- if(test)
- {
- P3 |=0x07;
- display_string(0,0,"Life's precious!");
- display_string(0,1,"Safe Driving!");
- delay_ms(2000);
- P3 &=~0x07;
- lcd_init();
- test=0;
- normal=1;
- delay_ms(2000);
- }
- if(normal)
- {
- while(1)
- {
- if(nor_speed)
- {
- P3 |=0x01;
- P3 &=~0x06;
- display_string(0,0,"Speed:");
- dis[3]=speed/1000;
- dis[4]=speed%1000;
- dis[2]=dis[4]/100;
- dis[4]=dis[4]%100;
- dis[1]=dis[4]/10;
- dis[0]=dis[4]%10;
- if(dis[3]!=0)
- {
- display_char(6,0,dis[3]+0x30);
- }
- else
- {
- display_char(6,0,0xA0);
- }
- display_char(7,0,dis[2]+0x30);
- display_char(8,0,dis[1]+0x30);
- display_char(10,0,dis[0]+0x30);
- display_string(9,0,".");
- display_string(11,0,"km/h");
- display_string(0,1,"Status:Normal");
- }
- if(exc_speed)
- {
- display_string(0,0,"Speed:");
- dis[3]=speed/1000;
- dis[4]=speed%1000;
- dis[2]=dis[4]/100;
- dis[4]=dis[4]%100;
- dis[1]=dis[4]/10;
- dis[0]=dis[4]%10;
- if(dis[3]!=0)
- {
- display_char(6,0,dis[3]+0x30);
- }
- else
- {
- display_char(6,0,0xA0);
- }
- display_char(7,0,dis[2]+0x30);
- display_char(8,0,dis[1]+0x30);
- display_char(10,0,dis[0]+0x30);
- display_string(9,0,".");
- display_string(11,0,"km/h");
- display_string(0,1,"Status:Speeding!");
- P3 &=~0x01;
- P3 |=0x06;
- delay_ms(500);
- display_string(0,1,"Warning!");
- P3 &=~0x06;
- dellay(500);
- }
- }
- }
- }
复制代码
|