Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

generate 40KHz square wave:

Status
Not open for further replies.
hi ,thank u both jim and torben ,
but u see i still don't get it???
i mean the ultrasound transmitter has two legs:
one for the square signal and the other is for the ground!! so why in the above code they used both legs for transmitting signal?
and if i remove the loop will the MCU generate 40KHz too?
and how much time does the square wave stay high? and how much time does it stay low??
sorry for the too many questions, but if u help through this point "generating 40KHz square wave" the rest will be just fine. and i'll always be thankfull to u
 
hi ,thank u both jim and torben ,
but u see i still don't get it???
i mean the ultrasound transmitter has two legs:
one for the square signal and the other is for the ground!! so why in the above code they used both legs for transmitting signal?

The transducer doesn't really care which leg has a higher voltage and which is lower. By driving the legs against each other like that you can get a higher apparent voltage between the legs, which increases the power output of the transducer.

and if i remove the loop will the MCU generate 40KHz too?

Well, no. It will just produce one high-low cycle, which by itself is not a 40kHz tone but is just a click. Not usable.

and how much time does the square wave stay high? and how much time does it stay low??

Look at the code and explanation again. It goes high for 10 microseconds and low for 9 microseconds. Grab a calculator, check out how many microseconds are in one second, and compare that with how many microseconds must be spent high and low to generate 40kHz.


Torben
 
200mS pulse at different interval using PIC16F887

Hi,

I am actually new using PICs. But i have been developing some code now, and have some basic foundation.

I need help writing a code for the stipulated function above using MPSM.

I just want an output of 200ms at varying interval and..let say..50s..100s...150s e.t.c.

I also is required to use a rotary analogue switch for input to vary the speed and frequency.

How do i start, interms of the main code word atfer initializing my PORTD and PORTA.?

How do i set up my timer?

And how do i convert A/D?
 
hi ,
thanks torben for ur reply but can u plz see my answer to it:
F=1/T , right?
ok, i have F which is equal to 40KHz, so if i want T it will be equal to 1/40K, right?
so by using that equation T must equal to 0.025ms which is also equal to 2.5us right?
but in the code here:
Code:
PING1:
PORTB = %00010000 ' 1st half of cycle
Delayus 10 ' Create a delay of 10uS
PORTB = %00100000 ' 2nd half of cycle
Delayus 9 ' Create a delay of 9uS
if as u said we supposed that the o/p square signal stayed high for 10us & low for 9us,
then the total time will be 19us and when i convert it into a frequency it will be equal to 52.6KHz!!!
so it won't really give 40KHz?!
i want to ask a question depending on the above code and the circuit connection "which i'll send later", is the output for the ultrasound transmitter sensor will be like the foolowing the circuit i send.
plz anyone check and tell me ?
thank u for ur help
 

Attachments

  • TX connection.JPG
    TX connection.JPG
    22.3 KB · Views: 159
  • output of the code.JPG
    output of the code.JPG
    14.4 KB · Views: 184
Last edited:
Hi Remas,

I haven't got time to explain properly, as I am going out. You need to
add in the time to execute the instructions too. The delays just make up the remaining time period..
I'm sure somebody else would be able to explain if you are still not sure..

Jim
 
Hi remas,

Sorry that I am so late to reply. I have been at a softball tournament since early this morning.

hi ,
thanks torben for ur reply but can u plz see my answer to it:
F=1/T , right?

Right.

ok, i have F which is equal to 40KHz, so if i want T it will be equal to 1/40K, right?

Also right.

so by using that equation T must equal to 0.025ms which is also equal to 2.5us right?

Right and wrong. 1/40,000 = 0.000025 = 0.025ms = 25µs, not 2.5µs. I suspect this is just a typo since below you have the math right.

but in the code here:
Code:
PING1:
PORTB = %00010000 ' 1st half of cycle
Delayus 10 ' Create a delay of 10uS
PORTB = %00100000 ' 2nd half of cycle
Delayus 9 ' Create a delay of 9uS
if as u said we supposed that the o/p square signal stayed high for 10us & low for 9us,
then the total time will be 19us and when i convert it into a frequency it will be equal to 52.6KHz!!!
so it won't really give 40KHz?!

The math is correct, and the final conclusion is valid--except that as Jimmy said, µc instructions take time.

From the document Jimmy posted, on page 9:

After clearing the TIMER1 overflow flag (TMR1IF), the program then calls the
PING subroutine which transmits a 40KHz signal 8 times from PORTB.4.
Timing within the PING subroutine is deliberately accurate, as the amount of
cycles taken by each instruction dictates the frequency and duration of the
output signal.

Now I will admit that I have not built this particular circuit, nor have I actually done the math to confirm that the given timings are correct. However I have designed my own simple sonar rangefinder using an AT89C4051, and the delay numbers I had to use to use did not exactly match what the simple math would indicate, because the instructions themselves take time to execute.

As I said, I have not done the math on this, but given that (as far as I know) these chips take at least 1µs per instruction, and that we only have to account for (25 - 19) = 6 missing microseconds, the missing time could easily be eaten up by the instruction timing.

i want to ask a question depending on the above code and the circuit connection "which i'll send later", is the output for the ultrasound transmitter sensor will be like the foolowing the circuit i send.
plz anyone check and tell me ?
thank u for ur help

No, that's not what comes out. The signals aren't end-to-end, they are being driven against each other. The way I would think of it is more like this:

draft1-png.21587


I don't know if that makes more sense but it's clearer to me. Note that the timings are greatly exaggerated in this drawing. :)


Torben
 

Attachments

  • draft1.png
    draft1.png
    13.1 KB · Views: 569
hi and i really thank u torben and jimmy , u helped me to understand the 19us thing :)
but now there's something else that i don't understand which is why the designer of that code used both legs of the transmitter with the MCU.
i mean what's the benifit of doing such a thing?
if he used one leg and let's suppose it's the one connected to RB4 and connected the other leg to the GND, and then he sent the following code:
Code:
PING1:
PORTB = %00010000 ' 1st half of cycle "[B]RB4=1[/B]"
Delayus 10 ' Create a delay of 10uS
PORTB = %00000000 ' 2nd half of cycle "RB4=0" 
Delayus 9 ' Create a delay of 9uS
isn't that going to make the same job??!!
if not plz tell me why?
thanx alot for ur time
 
hi and i really thank u torben and jimmy , u helped me to understand the 19us thing :)
but now there's something else that i don't understand which is why the designer of that code used both legs of the transmitter with the MCU.
i mean what's the benifit of doing such a thing?
if he used one leg and let's suppose it's the one connected to RB4 and connected the other leg to the GND, and then he sent the following code:
Code:
PING1:
PORTB = %00010000 ' 1st half of cycle "[B]RB4=1[/B]"
Delayus 10 ' Create a delay of 10uS
PORTB = %00000000 ' 2nd half of cycle "RB4=0" 
Delayus 9 ' Create a delay of 9uS
isn't that going to make the same job??!!
if not plz tell me why?
thanx alot for ur time

No it's not, the original way is a bridged connection, this doubles the voltage supplied to the transducer, quadrupling it's output power.
 
hi nigel u said:
Code:
No it's not, the original way is a bridged connection, this doubles the voltage supplied to the transducer, quadrupling it's output power.
if the only reason for that way of connection is more power for ultrasound sensor, i can use a transistor as switch for more power,right?
i mean will that make the above code i wrote work??
thanx in advance
 
Hi HSM,

You code would work with a transistor, if you needed more current, but why add components to your circuit when there are spare pins on the PIC? The way shown in the diagram is an efficient solution with minimum components.. :)

Regards
Jim
 
if the only reason for that way of connection is more power for ultrasound sensor, i can use a transistor as switch for more power,right?
i mean will that make the above code i wrote work??
thanx in advance

No, unless you use a higher supply voltage to feed the transistor - the simple (no components required) original circuit provides 10V for the transducer - so you would need at least a 10V supply for the transistor, plus the extra components, all to achieve just the same thing.

Rather pointless, using more parts, making it more complicated, just to achieve the same thing?
 
hi nigel, and thanx coz now i got ur point.
but u see i still have one last question, and i don't know if i should post it here or in the datasheet section,
is the input voltage of the transeducer affect the range that it can measure??
i tried to search that in google but i didn't find alot about it!!
i mean is all the ultrasound sensor can measure up to 10 meter??
i don't think so,
that's why i'm asking how can i calculate the distance that the sensor can measure.
the datasheet of the sensor that i'm using is attached to the file, so if anyone can take a look and tell me if u find out anything indicate the max distance
thank u for ur time
 

Attachments

  • t400s16.pdf
    96.4 KB · Views: 178
Distance is directly proportional to transmitted power, and the transmitter is rated at 20V RMS, and even bridged it's only been fed 10V p-p (about 3.5V RMS). So range is going to be limited - but it also depends on your receiver circuitry, and how much gain is provided there.
 
hi agian
ok suppose that i provided the ultrasound sensor with 20v rms, what's the max distance that it can measure???
would anyone plz explain this to me
thanx in advance
 
As Nigel mentioned, it depends on a lot of things, including the sensitivity of the receiving circuitry, how the transducers are mounted, the temperature, the air pressure, whether there is wind, and the composition, orientation, and size of the target--among other things.

For instance, the rangefinder will be able to "see" a hard wall perpendicular to the alignment of the sensor at a greater distance than it will be able to "see" a pillow.

To determine the range I suppose you could try to figure out all the possible variables and work out a mathematical model for them, but I believe what is usually done is to build the unit and then test it using a defined target. For instance, a broom handle might make a reasonable target to use for testing if the unit is expected to see and navigate around table and chair legs; if the thing only has to see walls, then a hard flat surface would make a better test target.


Torben
 
An easy way to boost the power to an ultrasonic transceiver is to use a max232 or equivalent to drive it. simple interface to a microcontroller..
 
hi again;
i just wanna say thank u all for ur help and ur time.
i'm sure that your responces helped alot of people "including me"
and i hope that God plesses all with scince and wisedom.
YOU ARE THE BEST
 
Any CMOS inverting gate (NAND, INV etc) with one resistor and one capacitor to give the 50:50 square wave at 40kHz. Then parallel the remaining gates in the chip to bump up the power, or connect 3 inverting gates to make push-pull outputs.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top