Discussion in "8051 Discussion Forum" started by    shama3ey    Oct 25, 2009.
Sun Oct 25 2009, 10:00 PM
#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
Sun Oct 25 2009, 10:44 PM
#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 Sun Mar 14 2010, 10:12 PM ]
 shama3ey like this.
Tue Oct 27 2009, 03:17 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?
Wed Oct 28 2009, 09:04 PM
#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 Wed Oct 28 2009, 09:11 PM ]
Fri Nov 06 2009, 02:52 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, 02:53 AM ]
Fri Nov 06 2009, 10:36 AM
#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, 12:40 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, 12:48 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, 12:54 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, 01:00 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

Conversation

No messages yet.

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 177, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123