有关于超声波测距的程序看看哪错了
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include <intrins.h>
sbit in1=P1^0;
sbit in2=P1^1;
sbit in3=P1^2;
sbit in4=P1^3;
sbit trig=P2^0;
sbit echo=P2^1;
unsigned int s=0,time=0;
void DelayUs2x(unsigned int t)
{
while(--t);
}
void DelayMs(unsigned int t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void fasheng()
{
trig=0;
trig=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig=0;
}
void jisuan()
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=time/58;
DelayMs(60); //算出来是CM
if(s<15)
{
in1=0;
in2=1;
in3=0;
in4=1;
}
else if(s>15)
{
in1=1;
in2=0;
in3=1;
in4=0;
}
else
{
in1=0;
in2=0;
in3=0;
in4=0;
}
}
void zd0() interrupt 1
{
DelayMs(1);
}
void main()
{
while(1)
{
echo=0;
fasheng();
TMOD=0x01; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
ET0=1; //允许T0中断
EA=1; //开启总中断
while(1)
{
while(!echo); //当RX为零时等待
TR0=1; //开启计数
while(echo); //当RX为1计数并等待
TR0=0;
}
jisuan();
}
}
#include <intrins.h>
sbit in1=P1^0;
sbit in2=P1^1;
sbit in3=P1^2;
sbit in4=P1^3;
sbit trig=P2^0;
sbit echo=P2^1;
unsigned int s=0,time=0;
void DelayUs2x(unsigned int t)
{
while(--t);
}
void DelayMs(unsigned int t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void fasheng()
{
trig=0;
trig=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig=0;
}
void jisuan()
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=time/58;
DelayMs(60); //算出来是CM
if(s<15)
{
in1=0;
in2=1;
in3=0;
in4=1;
}
else if(s>15)
{
in1=1;
in2=0;
in3=1;
in4=0;
}
else
{
in1=0;
in2=0;
in3=0;
in4=0;
}
}
void zd0() interrupt 1
{
DelayMs(1);
}
void main()
{
while(1)
{
echo=0;
fasheng();
TMOD=0x01; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
ET0=1; //允许T0中断
EA=1; //开启总中断
while(1)
{
while(!echo); //当RX为零时等待
TR0=1; //开启计数
while(echo); //当RX为1计数并等待
TR0=0;
}
jisuan();
}
}
main中第二个while语句难道不是个死循环吗?!
来学习的。
学习了!
GATE=1时TMOD=0x01吗?你再仔细算一下。