基于51单片机和LCD1602的自行车测速系统的设计
#include<reg52.h>#include<string.h>#include<intrins.h>typedefunsignedcharuchar;typede...
#include<reg52.h>
#include<string.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#define lcd_db P1
#define R 1
#define W 0
#define busy 0x80
#define L 1.5
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
bit flag=0;
uchar STR0[16],STR1[16];
ulong s=0;
ulong count=0;
ulong num=0;
ulong time;
ulong speed=0;
void display();
void init();
void write_cmd();
void delay(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=120;j>0;j--)
;
}
bit read_state()
{
rs=0;
rw=1;
en=1;
delay(5);
if(lcd_db&0x80)
{
en=0;
return 1;
}
else
{
en=0;
return 0;
}
}
void write_cmd(uchar cmd,bit cd)
{
while(read_state());
lcd_db=cmd;
rs=cd;
rw=0;
en=1;
delay(5);
en=0;
}
void disp_byte(uchar x,uchar y,uchar z)
{
if(x)
{ y+=0x40; }
y+=0x80;
write_cmd(y,W) ;
write_cmd(z,R) ;
}
void disp_list(uchar x,uchar y,uchar table[])
{
uchar t, z=0;
t=strlen(table)+y;
while(y<t)
{
disp_byte(x,y,table[z]);
y++;
z++;
}
}
void init()
{
write_cmd(0x01,W);
write_cmd(0x38,W);
write_cmd(0x08,W);
write_cmd(0x06,W);
write_cmd(0x0c,W);
}
void tim_0rupt()
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
time=num*0.05;
speed=s/time;
}
void out0_rupt() interrupt 0
{
count++;
s=s*count;
}
void STR_0()
{
STR0[0]='S';
STR0[1]='p';
STR0[2]='e';
STR0[3]='e';
STR0[4]='d';
STR0[5]=':';
STR0[6]=speed/10000+0x30;
STR0[7]=(speed%10000)/1000+0x30;
STR0[8]=(speed%1000)/100+0x30;
STR0[9]='.';
STR0[10]=(speed%100)/10+0x30;
STR0[11]=(speed%10)+0x30;
STR0[12]='k';
STR0[13]='m';
STR0[14]='/';
STR0[15]='h';
}
void STR_1()
{
STR1[0]='S';
STR1[1]=':';
STR1[2]=s/10000+0x30;
STR1[3]=(s%10000)/1000+0x30;
STR1[4]=(s%1000)/100+0x30;
STR1[5]=(s%100)/10+0x30;
STR1[6]=s%10+0x30;
STR1[7]='m';
}
void main()
{
IE=0x81 ;
IT0=1;
TMOD=0x01;
TCON=0x11;
IP|=0x01 ;
init();
disp_list(0,0,STR0);
disp_list(0,0,STR1);
while(1) ;
}
模拟运行后,无法显示? 展开
#include<string.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#define lcd_db P1
#define R 1
#define W 0
#define busy 0x80
#define L 1.5
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
bit flag=0;
uchar STR0[16],STR1[16];
ulong s=0;
ulong count=0;
ulong num=0;
ulong time;
ulong speed=0;
void display();
void init();
void write_cmd();
void delay(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=120;j>0;j--)
;
}
bit read_state()
{
rs=0;
rw=1;
en=1;
delay(5);
if(lcd_db&0x80)
{
en=0;
return 1;
}
else
{
en=0;
return 0;
}
}
void write_cmd(uchar cmd,bit cd)
{
while(read_state());
lcd_db=cmd;
rs=cd;
rw=0;
en=1;
delay(5);
en=0;
}
void disp_byte(uchar x,uchar y,uchar z)
{
if(x)
{ y+=0x40; }
y+=0x80;
write_cmd(y,W) ;
write_cmd(z,R) ;
}
void disp_list(uchar x,uchar y,uchar table[])
{
uchar t, z=0;
t=strlen(table)+y;
while(y<t)
{
disp_byte(x,y,table[z]);
y++;
z++;
}
}
void init()
{
write_cmd(0x01,W);
write_cmd(0x38,W);
write_cmd(0x08,W);
write_cmd(0x06,W);
write_cmd(0x0c,W);
}
void tim_0rupt()
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
time=num*0.05;
speed=s/time;
}
void out0_rupt() interrupt 0
{
count++;
s=s*count;
}
void STR_0()
{
STR0[0]='S';
STR0[1]='p';
STR0[2]='e';
STR0[3]='e';
STR0[4]='d';
STR0[5]=':';
STR0[6]=speed/10000+0x30;
STR0[7]=(speed%10000)/1000+0x30;
STR0[8]=(speed%1000)/100+0x30;
STR0[9]='.';
STR0[10]=(speed%100)/10+0x30;
STR0[11]=(speed%10)+0x30;
STR0[12]='k';
STR0[13]='m';
STR0[14]='/';
STR0[15]='h';
}
void STR_1()
{
STR1[0]='S';
STR1[1]=':';
STR1[2]=s/10000+0x30;
STR1[3]=(s%10000)/1000+0x30;
STR1[4]=(s%1000)/100+0x30;
STR1[5]=(s%100)/10+0x30;
STR1[6]=s%10+0x30;
STR1[7]='m';
}
void main()
{
IE=0x81 ;
IT0=1;
TMOD=0x01;
TCON=0x11;
IP|=0x01 ;
init();
disp_list(0,0,STR0);
disp_list(0,0,STR1);
while(1) ;
}
模拟运行后,无法显示? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询