#include<reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char void delay(uint z) { uint a,b; for(a=z;a>0;a--) for(b=60;b>0;b--); } sbit LcdRs = P2^0; sbit LcdRw = P2^1; sbit LcdEn = P2^2; #define DBPort P0 #define LCD_COMMAND 0 #define LCD_DATA 1 #define LCD_CLEAR_SCREEN 0x01 #define LCD_HOMING 0x02 void LCD_Write(bit style, unsigned char input) { LcdRs=style; LcdRw=0; DBPort=input; delay(5); LcdEn=1; delay(5); LcdEn=0; } #define LCD_SHOW 0x04 #define LCD_HIDE 0x00 #define LCD_CURSOR 0x02 #define LCD_NO_CURSOR 0x00 #define LCD_FLASH 0x01 #define LCD_NO_FLASH 0x00 void LCD_SetDisplay(unsigned char DisplayMode) { LCD_Write(LCD_COMMAND, 0x08|DisplayMode); } #define LCD_AC_UP 0x02 #define LCD_AC_DOWN 0x00 #define LCD_MOVE 0x01 #define LCD_NO_MOVE 0x00 void LCD_SetInput(unsigned char InputMode) { LCD_Write(LCD_COMMAND, 0x04|InputMode); } void LCD_Initial() { LcdEn=0; LCD_Write(LCD_COMMAND,0x38); delay(5); LCD_Write(LCD_COMMAND,0x38); delay(5); LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); delay(5); LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); delay(5); LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); delay(5); } void GotoXY(unsigned char x, unsigned char y) { if(y==0) LCD_Write(LCD_COMMAND,0x80|x); if(y==1) LCD_Write(LCD_COMMAND,0x80|(x-0x40)); } void Print(unsigned char *str) { while(*str) { LCD_Write(LCD_DATA,*str); str++; delay(1); } } void Timer1Init(void) //50ms@12.000MHz { TMOD &= 0x0F; TMOD |= 0x10; TL1 = 0xB0; TH1 = 0x3C; TF1 = 0; TR1 = 1; ET1=1; EA=1; } unsigned char count=0; unsigned int speed=0; long temp1=0,temp2=0; long s=0; void Timer1_IRQ() interrupt 3 { TR1 = 0; TL1 = 0xB0; TH1 = 0x3C; TR1 = 1; count++; if(count==10) { temp2=TH0; temp2<<=8; temp2+=TL0; if(temp2<temp1) s+=65535; speed=(temp2-temp1)*5; temp1=temp2; count=0; } } void Count0Init(void) { TMOD &= 0xF0; TMOD |= 0x05; TL0 = 0; TH0 = 0; TF0 = 0; TR0 = 1; } unsigned int c=20; long u=0; sbit k1=P3^0; sbit k2=P3^1; void main() { Timer1Init(); Count0Init(); LCD_Initial(); while(1) { GotoXY(0,0); Print("V:"); LCD_Write(1,speed/100+0x30); LCD_Write(1,speed%100/10+0x30); LCD_Write(1,'.'); LCD_Write(1,speed%10+0x30); LCD_Write(1,' '); Print("C:"); LCD_Write(1,c/100+0x30); LCD_Write(1,c%100/10+0x30); LCD_Write(1,'.'); LCD_Write(1,c%10+0x30); Print("CM"); GotoXY(1,1); Print("Total:"); u=(s+temp1)*c/40; LCD_Write(1,u/100000+0x30); LCD_Write(1,u%100000/10000+0x30); LCD_Write(1,u%10000/1000+0x30); LCD_Write(1,u%1000/100+0x30); LCD_Write(1,'.'); LCD_Write(1,u%100/10+0x30); LCD_Write(1,'M'); if(k2==0) { delay(5); if(k2==0) { while(k2==0); c++; } } if(k1==0) { delay(5); if(k1==0) { while(k2==0); if(c>0) c--; } } } }
|