thanks,paul
as u rightly mentioned in practise it can measure upto 400cm(4000mm).
i did put the max limit,the object need to be detected within 100cm if the distance calculated is above that then the leds display nothing as programmed i will put up my code:
this is my first code only with one sensor interfaced & working fine
port P1 PIN 0 (echo) PIN 1(trig) connected .
port P2 LED connected it displays binary value of the measured distance in cm.
here is the code with only one sensor interfaced:
Code:
//INITIALISATION
#include <reg51.h>
void delay(unsigned int); // to give a pulse of 10us delay to activate ultrasound sensor
void leddelay(unsigned int);
sbit echo1=P1^0; //echo of ultrasound sensor
sbit trig1=P1^1; //trig of ultrasound sensor
sfr16 DPTR =0x82;
//MAIN PROGRAM
void main()
{
unsigned int range=0,y=0;
TMOD=0X01;
trig1=0; //o/p
echo1=1; //i/p
P2=0X00; //O/P port leds connected
while(1)
{
trig1=1;
delay(1); 10us delay for activation
trig1=0;
while(echo1==0);
TR0=1;
while(echo1==1); wait for object detecetion
TR0=0;
DPH=TH0;
DPL=TL0;
if(DPTR<30000) actually had to take 38000 but fine
range=DPTR*0.01847091; //0.01847091 is value got to convert timer value into cm
else
range=0;
if(range<100)
P2=range;
else
P2=0X00;
leddelay(999); //some delay for display to be visible
}
}
//10us delay routine
void delay(unsigned int g)
{
for(;g!=0;g--)
{
TMOD=0X01;
TL0=0XF6;
TH0=0XFF;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}
}
//some delay for leds
void leddelay(unsigned int x)
{
unsigned int y,z=0;
for(z=0;z<30;z++)
{
for(y=0;y<9000;y++)
for(;x!=0;x--);
}
}
- - - Updated - - -
yes klaus i did feel so ..
hence, what i did was i did not set any maximum limit and with only one sensor interfaced i tried the program and with no object and good ceiling and floor clearance i tried but, i got the display of leds indicating some obstruction no matter the value what the leds displayed was large may be the max limit the sensors can measure(400cm). But, they did not display zero(which actually had to be ) that is what is worrying me as the sensor will always keep on detecting though no obstruction
- - - Updated - - -
- - - Updated - - -
paul ,klaus
this is my second program where in i have interfaced two ultrasound sensors
port p1 pin0,pin1 first ultras sensor. port p1 pin6,pin7 second ultras sensor.
port p2 leds connected.
But this program is displaying no o/p .
it is displaying only when both sensors go low at once
here is the program:
Code:
#include <reg51.h>
void delay(unsigned int);
void leddelay(unsigned int);
sbit echo1=P1^0;
sbit trig1=P1^1;
sbit echo2=P1^6;
sbit trig2=P1^7;
sfr16 DPTR =0x82;
void main()
{
unsigned int range=0,y=0;
TMOD=0X01;
trig1=0; //o/p
echo1=1; //i/p
trig2=0; //o/p
echo2=1; //i/p
P2=0X00; //O/P
while(1)
{
trig1=1; //delay of 10us for both sensors
trig2=1;
delay(1);
trig1=0;
trig2=0;
while(echo1==0&&echo2==0); //wait till echo pin goes high as they
// will be low initially and will go high after a 10us pulse at trig pin of sensors
TR0=1; //start timer
while(echo1!=0||echo2!=0); //if echo1 goes lowfirst that mean object moving toward left
//& if echo2 goes low first then object moving towards right if both go low then object exactly in front of sensor..
TR0=0;
DPH=TH0;
DPL=TL0;
if(DPTR<30000)
range=DPTR*0.01847091; //everything same as prev program
else
range=0;
if(range<100)
P2=range;
else
P2=0X00; //don't dispaly if object far than 100cm
leddelay(999);
}
}
//10us delay subroutine
void delay(unsigned int g)
{
for(;g!=0;g--)
{
TMOD=0X01;
TL0=0XF6;
TH0=0XFF;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}
}
void leddelay(unsigned int x)
{
unsigned int y,z=0;
for(z=0;z<30;z++)
{
for(y=0;y<9000;y++)
for(;x!=0;x--);
}
}