Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me generate square wave using 8051

Not open for further replies.


Newbie level 3
Jul 20, 2009
Reaction score
Trophy points
Activity points
I want to generate square wave oscillation using uc8051 with fixed duty cycle say 50% but i want to be able to change the frequency of oscillation from 1Hz to 2KHz by keying in the required value and make the uc generate it. 12MHz crystal oscillator is used. pls, i need help on how to do this.
I use asm code. Thanks all. :D

how to generate low frequency to 8051 port pin

You have to use timer interrupt...

So first calculate the Value for registers TH and TL for required frequency..
And activate timer interrupt...

In Interrupt loop toggle any one Port pin..
means for odd time make port pin high and for even time make it low...

You want to change frequency using key connected to port pin..
so Meanwhile also check status of key and change the value of Timer register according to the key pressed.....


generating a square wave in 8051 port

Dear osoenbolsa ,

If you need to vary the frequency from the minimum to the maximum , The Tie should be varied from the maximum to minimum.

As VIPUL said you need to use the timer interrupt for the perfect waveform generation.

Try with the usage of interrupts , else if you find it as difficult try with the delay routines .

For 1 Khz Generation , the Time will be , 500us OFF and 500 us ON . Better you write a delay rutine for 250us , and call the delay routines that much frequencyyou need to generate in multiles of 250us.

for example , for 1Khz generation , Set 1^0 to 1 and call 250us delay twice , Clr 1^0 to 0 and call 250us delay twice. Do that continously .

But the Best way is to generate square wave with Interrupt routines.

Vikeee ..

8051 assembly square wave

Thanks all.

I use the Interrupt routines. But my question is how to obtain the values of tl0, th0 and r0 (3 bytes for low frequency) by program.

For example:
• I select with a Keypad 1Hz --> T/2 =500.000 us --> th0=3c ;tl0 = afh; r3=10 (10*5000us)
• If i select a new frequency of 1500Hz --> T/2 = 333 US --> TH0=FEH ; TL0=B2H; R3=0

How can i obtain, by program in asm, the values of th0, tl0 and r3? (with a table with 2000 values, division...)

Thanks all.:D

Not open for further replies.

Part and Inventory Search

Welcome to