找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

51单片机超声波测距程序

[复制链接]
admin 发表于 2012-9-2 08:19:07 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
帮忙看看问题出在哪里?头疼啊!

#include <reg52.h>
#include <1602.h>
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define nop _nop_()
sbit SquareWave_Send=P2^0; //超声波发射端
sbit SquareWave_Rec =P2^1; //超声波接受端
uchar counter=0,i,j;
double Interval_Time=0;
uint Wave_Speed=345,temp_D=0,temp=0,Distance=0;
void Init_Interrupt() //Interruption初始化
{
TMOD=0x01;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
EA=1;
ET0=1;
TR0=0;
SquareWave_Rec=1;
}
uint operating() //处理函数,返回处理后测得的距离
{
TR0=1; //开定时器
for(i=0;i<40;i++)
{
SquareWave_Send=~SquareWave_Send;
nop;
nop;
nop;
nop;
nop;
nop;
nop;
nop;
} //超声波发生
delay(2); //延时,避免干扰
while(SquareWave_Rec==0) //等待触发
{
TR0=0;
Interval_Time=TH0*256+TL0-65535+10000;
if(Interval_Time>9000)
Interval_Time=0;
Distance=Interval_Time*Wave_Speed/200;
}
return Distance;
}

void main()
{
Init_Interrupt();
display_static(); //lcd显示的不变的数据
while(1)
{
for(i=0;i<100;i++)
{
temp_D=operating();
temp=temp+temp_D;
}
Distance=temp/100+temp%100; //每一百个数据求平均值
temp=0;
display_trends(Distance);
delay(5);

}
}
void Timer0() interrupt 1
{
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
}
/*******************************************
1602.h
********************************************/
#define uchar unsigned char
#define uint unsigned int
uchar table[]="The distance is";
sbit lcdrs=P2^7;
sbit lcden=P2^6;
uchar num,ge,shi,yi,er,san;
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=100;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
lcden=1;
delay(1);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
lcden=1;
delay(1);
lcden=0;
}
void Init_1602()
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display_static()
{
Init_1602();
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(1);
}
write_com(0xc0+0x06);
delay(200);
write_data('c');
write_data('m');
}

void display_trends(uint Distance)
{
shi=Distance/10000;
ge=Distance%10000/1000;
yi=Distance%10000%1000/100;
er=Distance%10000%1000%100/10;
san=Distance%10000%1000%100%10;

write_com(0x80+0x40);
delay(200);
write_data(0x30+shi);
write_data(0x30+ge);
write_data(0x2e);
write_data(0x30+yi);
write_data(0x30+er);
write_data(0x30+san);
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-24 09:36 , Processed in 0.050006 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表