generate 40KHz square wave:

Status
Not open for further replies.

remas

New Member
hi,
i want to know how can i generate a 40KHz square wave from any kind of microcontroller and by any programming language (assembly/c) it doesn't matter.
i only need to know how it happens??
so plz if anyone read my question i'm asking u to write even a short code that explains about that operation!! even a littel hint maybe it will help me
 
Hint: look for information on crystal/oscillator timings, clock speeds, and timers. And division.


Torben
 
Hii, lemme give u a hint!
if u use 89c2051(20pin) uc with 12Mhz crystal, n if u put following loop in yr code,it'll give u 40khz square wave (unipolar,ofcourse!!) at pin 7 of port1.

Code:
LOOP:	SETB P1.7	;1
	MOV R0,#02H	;1
BACK1:	NOP		;1  |
	NOP		;1  |X2 = 8
	DJNZ RO,BACK1	;2  |
	NOP		;1
	NOP		;1
	NOP		;1
	CLRB P1.7	;1
	MOV R0,#02H	;1
BACK2:	NOP		;1  |
	NOP		;1  |X2 = 8
	DJNZ RO,BACK2	;2  |
	SJMP LOOP	;2	
	;	       _______
	;TOTAL: 	25 CYCLES

now, the problem is, OFFtime will be 12usec and ONtime will be 13usec!!
well, this was supposed to be a hint only!
 
hi ,
and thanx for ur replies but i want to ask u about the following code it's in basic language for microcontroller PIC16F84:
Code:
main:
dim i as integer
TRISA=0
for i = 1 to 9
PORTA.4=1
Delay_ms(2)
PORTA.4=0
Delay_ms(0.5)
next i
so is it correct???!!!!
plz some one answer me
 
Hi Remas,
Here is my thoughts..

16F84 is a very outdated PIC. Use something like a 16F88 with a hardware PWM port. That way, you can set up the pwm to run the 38khz IR pulse in the background, while the pic handles the logic.

Get some IR logic modules @ approx £0.50 each. The IR modules are usually held low by the IR beam, and go high when its broken. These are good because they are fairly immune to sunlight/fluorescent light etc..

Plan a flow chart of what the circuit must do, and work out how many inputs/outputs you will need.
Read Nigel's Tutorials..Even if you intend to code in Basic, there is a lot of useful info in there..

Break the circuit into smaller parts....
INPUTS
OUTPUTS
MICROCONTROLLER
POWER SUPPLY
Google each section, as individual problems.Read the datasheet of the parts you choose.
Test each section on a breadboard, then when all your sub-circuits are working, then put it all together.

I would personally reccomend Proton Basic to program your PIC.., but others swear by Swordfish or others.

Good luck!
Jim
 
thank u jim for ur beautiful way of explaning and advicing, and i understood every thing u said but except this:
you can set up the pwm to run the 38khz IR pulse in the background, while the pic handles the logic.
what do u mean, plz anyone answer me ??maybe it's impotrant thing that he said??!!!
 
Read the datasheet of the PIC to learn more about PWM (pulse width modulation).

The short of it is: You can have the PIC output the 40khz signal using the PWM module. This way you can have the signal running while your PIC is busy running other code.
 
Remas,
as Boomslang said, have a look at a datasheet for a 16f690 or similar PIC. Also Google hardware PWM (Pulse Width Modulation). It's basically a seperate circuit, (portc.5 I think) that can be used to output a pulse, independent of the software. So you don't waste processing time on it.
Its a bit hard to explain here, you need to spend a couple of hours reading up on it.
What Basic compiler are you using?

Jim
 
hi,
jim u sked me about which compiler am i using? my answer is that i didn't decide yet..
coz i just started to learn microcontroller for my project, i didn't take it in my institute so i'm open for all choises,
i know what i want from the MCU to do, but i just don't know how to get it doing?!
u see my project is about finding distance range using ultrasound sensor.
and as u all know that the ultrasound sensor is devided into two parts: tranasmitter and receiver.
the transmitter needs 40KHz square wave to work, and as i wanted the MCU to control the TX and RX ,
so i said to my self maybe the MCU can generate the 40KHz square wave signal
and i want to tell u that i'm really starting to learn all kind of programming language such as basic, c and assembly ,and up till now i read the datasheet of 3 MCU which are AT89c51 , PIC16F84 ,ATmega32 and finally the one that jim told me about which is PIC16F887
and i don't want u to do it for me! no i really don't.
i tried to write a program above and i asked for ur thoughts? i mean is it right or wrong ..
if it's right i'ld like to complete my project by that MCU and programming lanagage !!
but if it's wrong or u don't know it? so plz i am seeeking for ur advice about which MCU and programming language should i use? that gives me what i want
plz i really like straight commands i mean i prefer using c or basic more than assembly coz i don't like counting cycls for creating delay ..
coz that’s my real problem it’s the delay I mean
want to generate 40 KHz and start a timer that stops when the reciver get the transmitter pulses
thank u for ur time
 
Hi Remas,
Have a read of this, should tell you all you need to know

Jim

**broken link removed**
 
Last edited:
hi jim thank u for the great file, but there is somthing in that file i couldn't understand look at this code:
Code:
PING:
PING_LOOP = 8 ' Number of cycles in ping
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
Djnz PING_LOOP,PING1 ' Special mnemonic to form a fast loop
Return
i mean they r saying that this code suppose to generate 40 KHz!! how ?
i know i ask alot, but plz can any one answer me..
 

I hope that made some sense to you. Do you see how it works now?


Torben
 
Djnz means 'DECREMENT and JUMP if NOT ZERO' Its like a very fast loop.
The timing is coming from the internal hardware timer. the delays are what set the frequency. (Based on a 4Mhz clock)
As the document says, PortB.4 goes high, and portb.5 goes low for 10 uS, then they switch over. This happens 8 times until it has decremented (counted down) to zero.

Have another look , and see if it makes sense now
Jim
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…