Discussion in "8051 Discussion Forum" started by    shama3ey    Oct 25, 2009.
Sun Oct 25 2009, 06:00 PM
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, 06:44 PM
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.
As it turns out it is probably easier to use the 8051


[ Edited Sun Mar 14 2010, 06:12 PM ]
 shama3ey like this.
Tue Oct 27 2009, 11:17 AM
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, 05:04 PM

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?


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, 05:11 PM ]
Thu Nov 05 2009, 09:52 PM
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 Thu Nov 05 2009, 09:53 PM ]
Fri Nov 06 2009, 05:36 AM
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, 07:40 AM
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, 07:48 AM
[quote]so for a 11.059 MHz crystal, the time for single clock cycle is still 1uS?

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

When you say one side delay you mean what?


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?


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, 07:54 AM
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, 08:00 AM
correct.. now you're at your own for now. (till i come back online )
see ya Ali ile

Get Social


Powered by e107 Forum System


Tue May 19 2020, 06:29 PM
Does anyone have microcontroller programming software for STC15F104W (core 8051, 4 k, 128 byte, 6 I/O pin, UART ... DIL8), that works?
Sat May 02 2020, 03:38 PM
Tamu Gurung
Hi I am looking for a assembly language program for a digital decimal counter. Time counts max limit of 2 mins starting from 2.00 mins and ending at 0.00. time is to be displayed on 4 7-segment LED displays. Edsim51 simulator to be used. Thanks
Wed Mar 25 2020, 09:39 AM
Sir kindly send the code file i2c for 8051
Fri Mar 13 2020, 06:36 PM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 08:55 AM
how can get hadwere of this project
Sat Mar 07 2020, 12:11 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 11:58 AM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 05:12 AM
Pearl Olin
Wed Jan 22 2020, 01:16 PM
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 02:08 AM
shubham kharade
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me



Thu May 21 2020, 08:44 AM
Wed May 20 2020, 09:28 PM
Wed May 20 2020, 03:05 PM
Wed May 20 2020, 07:52 AM
Wed May 20 2020, 03:15 AM
Tue May 19 2020, 06:08 PM
Tue May 19 2020, 02:22 PM
Mon May 18 2020, 09:50 PM


Guests: 68, Members: 0 ...

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

Members: 38211
Newest member: Davidthils
Forum Activity
xrxx Avatar
Posted By xrxx
Thu Apr 30 2020, 06:59 AM
BCD MULTIPLICATIONAssume that you have to 2 BCD nu...
liqian Avatar
Posted By liqian
Tue Apr 28 2020, 05:26 AM
I happened to see your post and I got many useful ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Apr 28 2020, 04:21 AM
The code is hard to read.Please zip it and attach ...
rogermado Avatar
Posted By rogermado
Sun Apr 26 2020, 01:53 AM
@ExperimenterUK, thanks for your reply. You'll fin...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Apr 25 2020, 05:42 PM
Some simulators handle displays better than others...
rogermado Avatar
Posted By rogermado
Sat Apr 25 2020, 03:59 PM
Hello everyone, sorry in advance if i'm not in the...
PHANTOM28 Avatar
Posted By PHANTOM28
Fri Feb 28 2020, 10:25 AM
Hi.I hope you all can help me. I am relatively ne...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Feb 21 2020, 05:05 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Feb 21 2020, 04:45 AM
Yes you can do that.1. Create a channel @Thingspea...
ananyagupta Avatar
Posted By ananyagupta
Thu Feb 20 2020, 03:58 AM
With regards to the particular characters sent to ...