Discussion in "8051 Discussion Forum" started by    shama3ey    Oct 26, 2009.
Mon Oct 26 2009, 03:30 am
#1
Hi all,

I'm trying to make my microcontroller generate a pulse of 40 KHZ to send to my sonar sensor attached to the UC ... how can that be done? both in code and in hardware?

I'm using Parallax Ping))) sonar which is TTL compatible in which I believe don't need an A/D converter.

Plz help
Mon Oct 26 2009, 04:14 am
#2
The Parallax Ping))) sonar will take care of producing the 40KHz signal.

You have to send a short pulse about 5 uS long to the Parallax "signal" pin
as a trigger to start a scan.

After 750uS the Parallax board pulls "signal" pin high.
It remains high for a period that indicates the distance to the target.

The signal is TTL compatible so you don't need an A/D converter.

As Trigger and Echo are on the same pin (oddly) it is important to know exactly
how the Sonar board drives the signal pin, and it is not clear from the PDF.

I recommend you look for examples of the board in use, on the internet.
I may be harder to do with an 8051 than with a PIC because of the way their
ports work.
EDIT:
As it turns out it is probably easier to use the 8051


http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf


[ Edited Mon Mar 15 2010, 03:42 am ]
 shama3ey like this.
Tue Oct 27 2009, 08:47 pm
#3
so do you think its possible to use (say P1.4) to generate the pulse to the sensor and use a different pin ( say P1.5) to read the input of the sensor after detecting something?
Thu Oct 29 2009, 02:34 am
#4


so do you think its possible to use (say P1.4) to generate the pulse to the sensor and use a different pin ( say P1.5) to read the input of the sensor after detecting something?

shama3ey


I'm sure you can, I'm just not sure how

It may be easy, but you would have to experiment.
There are similar units around with separate trigger and echo pins, that are cheaper.
Google "SRF04"

Parallax say that once they stop selling a product they remove
all documents, downloads etc from their site.
I'm not impressed with their customer support.



[ Edited Thu Oct 29 2009, 02:41 am ]
Fri Nov 06 2009, 08:22 am
#5
Ok based on the PING manual, I wrote some psedu code to operate the sensor.. but I don't know how interpret it in terms of codes since I don't know how to generate delay loops that runs exactly for a specific time... So can you help me implement this?

#include <reg51f.h>

sonarO = 0x90; //P1.0
unsigned char distance;
unsigned char time;
void sonar()
{

sonarO = 0xff;
//wait 7 uS
sonarO = 0x00;
//wait 750 uS
//reset timer used to avoid overflow
//while(get time when sonarO goes high || quit loop if time passed time =18.5 ms);
//Speed_of_Sound * Time_Passed / 2 = Distance_from_Object
//distance = 344 m/s * Time Passed in S / 2

distance = 344 *( time/2);

}


[ Edited Fri Nov 06 2009, 08:23 am ]
Fri Nov 06 2009, 04:06 pm
#6
i give you pseudo code to follow..
sonarO connected to INT0 pin P3.2 not P1.0 to make things easier.


sonarO = 0; //make it o/p pin and o/p low on P3.2
sonarO = 1; //
5uS delay// or whatever u want to keep
sonarO = 0;// pulse complete L-H-L
//no delay required, prepare your controller for calculating time..
sonarO = 1;//make it i/p pin to read the pulse width
TMOD = 0x09; //Timer0, 16bit gate controlled(P3.2)
TR0 = 1;//Timer will only run when Pin becomes high.
while(sonarO==0);//wait till pin is low
while(sonarO==1);//wait for pulse to finish
//timer stopped automatically as soon as P3.2 goes low
TR0 = 0;//completely stop timer.

now value in your TH0-TL0 will have width of pulse in clock cycle, to convert it into uS multiply it by time of single clock cycle e.g. for 12Mhz clock cycle is 1uS.

copy this in a variable divide it by 2 to get one side delay. Now you can easily convert it to distance.
 shama3ey like this.
Tags ultrasonic distance measurement sensorparallax ping)) sensor 80518051 distance measurement
Fri Nov 06 2009, 06:10 pm
#7
so in psedu code you wrote, I only need to find a way to delay for uS, and take the values in TH0-TL0 and multiply them with the time of a single clock cycle, then use the distance formula

distance = 344 *( time/2);

so for a 11.059 MHz crystal, the time for single clock cycle is still 1uS?

When you say one side delay you mean what?

and to calculate the time, I calculate whats in TH0 then add it to whats calculated from TL0?

Appreciate your patience I'm trying to learn and you are doing great job
Fri Nov 06 2009, 06:18 pm
#8
[quote]so for a 11.059 MHz crystal, the time for single clock cycle is still 1uS?
[/code]

no its 1.085uS calculated from (1/fosc) *12

When you say one side delay you mean what?

shama3ey


sound burst first goes and then come back so it travels the distance twice so we need distance of one way only. hence divide by 2.

and to calculate the time, I calculate whats in TH0 then add it to whats calculated from TL0?

shama3ey


well in C its fairly simple.
unsigned int temp;
temp = TH0;
temp = (temp<<8) | TL0;

so temp will have the 16-bit count. you can use it in calculations.
Fri Nov 06 2009, 06:24 pm
#9
Thank you very very much...

So now I multiply the value in temp by 1.08uS to get total time traveled then divide it by 2 to get the time of one side correct?
Fri Nov 06 2009, 06:30 pm
#10
correct.. now you're at your own for now. (till i come back online )
see ya Ali ile

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

sssrsait
Sun Feb 05 2023, 04:57 am
Richardfap
Sat Feb 04 2023, 01:56 pm
klining_Lob
Sat Feb 04 2023, 11:52 am
Stevenplaix
Sat Feb 04 2023, 02:28 am
ufa16zbnow
Sat Feb 04 2023, 02:14 am
NelsonHeard
Fri Feb 03 2023, 07:52 pm
dracermen
Thu Feb 02 2023, 10:32 pm
RowenaSteve
Thu Feb 02 2023, 12:58 am