#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0 //数据口
sbit LCD_RS = P2^6; //寄存器选择输入
sbit LCD_RW = P2^5; //液晶读/写控制
sbit LCD_EN = P2^7; //液晶使能控制
sbit LCD_PSB = P3^2; //串/并方式控制
uchar code dis1[]={"现在速度:"};
uchar dis2[5];
float f=0;
uint k,count=0;
uint speed;
/*定时器初始化*/
void initime()
{
TMOD=0x51; //T1计数器,T0定时器,方式1
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
EA=1;
TL1=0;
TH1=0;
ET0=1;
}
void delay_1ms(uint xms)
{
uint y,z;
for(y=xms;y>0;y--)
for(z=110;z>0;z--);
}
//写指令
void write_cmd(uchar cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
P0 = cmd;
delay_1ms(5);
LCD_EN = 1;
delay_1ms(5);
LCD_EN = 0;
}
//写数据
void write_dat(uchar dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delay_1ms(5);
LCD_EN = 1;
delay_1ms(5);
LCD_EN = 0;
}
//显示地址
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
pos = X+Y ;
write_cmd(pos);
}
//LCD初始化
void lcd_init()
{
LCD_PSB = 1; //并口方式
write_cmd(0x30); //基本指令操作
delay_1ms(5);
write_cmd(0x0C); //显示开,关光标
delay_1ms(5);
write_cmd(0x01); //清除LCD的显示内容
delay_1ms(5);
}
void main(void)
{
uchar i;
initime();
lcd_init();
TR0=1;
TR1=1;
lcd_pos(0,0);
i=0;
while(dis1[i]!= '\0')
{
write_dat(dis1[i]); //显示文字
i++;
}
while(1)
{
lcd_pos(1,0);
f=f*10;
k=f;
dis2[0]=k/1000+0x30;
dis2[1]=(k%1000)/100+0x30;
dis2[2]=((k%1000)%100)/10+0x30;
dis2[3]='.';
dis2[4]=((k%1000)%100)%10+0x30;
for(i=0;i<5;i++)
{
write_dat(dis2[i]);
}
}
}
void timer()interrupt 1
{
if(count==20)
{
f=(TH1*256+TL1)*0.0418;
count=0;
TL1=0;
TH1=0;
}
else
{
count=count+1;
}
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
} |