超声波测距 新手 求助
时间:10-02
整理:3721RD
点击:
刚接触单片机不久,自己想做个超声波测距仪,用的超声波型号是srf05,用1602显示屏显示距离,单片机换成12m的晶振,自己写的如下程序,烧录后 屏幕没办法正确显示距离,我也不知道程序哪里错了,求高人指点!
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^4;
sbit rw=P3^6;
sbit rs=P3^5;
sbit start=P1^0;
sbit back=P1^2;
uint count;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay_20us()
{
uchar bt ;
for(bt=0;bt<100;bt++);
}
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
TH0=0;
TL0=0;
TMOD=0X01;
TR0=1;
ET0=1;
EA=0;
lcden=0;
rs=0;
rw=0;
dula=0;
wela=0;
back=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_data(0x80);
}
void conversion()
{
uchar a,b;
a=TH0;
b=TL0;
count=a*256+b;
count=count*12;
count=count/58;
}
void display(uint date,uchar add)
{
uchar ge,shi,bai;
bai=date/100;
shi=date%100/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void main()
{
init();
while(1)
{
start=1;
delay_20us();
start=0;
if(back==1)
{
EA=1;
while(back!=0);
EA=0;
conversion();
TH0=0;
TL0=0;
TF0=0;
display(count,3);
}
delay(50);
}
}
void T0_time() interrupt 1
{
TH0=0;
TL0=0;
TF0=0;
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^4;
sbit rw=P3^6;
sbit rs=P3^5;
sbit start=P1^0;
sbit back=P1^2;
uint count;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay_20us()
{
uchar bt ;
for(bt=0;bt<100;bt++);
}
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
TH0=0;
TL0=0;
TMOD=0X01;
TR0=1;
ET0=1;
EA=0;
lcden=0;
rs=0;
rw=0;
dula=0;
wela=0;
back=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_data(0x80);
}
void conversion()
{
uchar a,b;
a=TH0;
b=TL0;
count=a*256+b;
count=count*12;
count=count/58;
}
void display(uint date,uchar add)
{
uchar ge,shi,bai;
bai=date/100;
shi=date%100/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void main()
{
init();
while(1)
{
start=1;
delay_20us();
start=0;
if(back==1)
{
EA=1;
while(back!=0);
EA=0;
conversion();
TH0=0;
TL0=0;
TF0=0;
display(count,3);
}
delay(50);
}
}
void T0_time() interrupt 1
{
TH0=0;
TL0=0;
TF0=0;
}
光这样看不好找问题,得边调边找
找出问题了,已经解决了!计数的问题
额。。
额。兄弟。能不能把你的图发给我看看。我也准备做这个的额。