generate different freq with mcu

Thread Starter

aamirali

Joined Feb 2, 2012
412
i have to generate 6 diff simultaneous square waves of varying freq, 100Hz, 200,250,300,400HZ, 500hz at different port pins.
How to do that..


Can it be done using timer
 
Last edited:

t06afre

Joined May 11, 2009
5,934
Yes it can be done with onboard timers on your micro controller. Also in software but timers are much more accurate. You should also note many of the frequencies are harmonic. You can easily divide down the 500Hz signal to get the 250Hz. With some external components just Google digital frequency divider
 

ErnieM

Joined Apr 24, 2011
8,377
Actually I found it a bit tough to find a common frequency to cover all these rates... probably as I don't know of any general method for this and had to make one up on the fly.

Anyway, one way to bag all the frequencies is to find a common frequency that all are a integer divisor of. "By guess and by gosh" (Excel helps for this) I found this to be 30,000Hz:

Multiple = 30,000/Freq

Rich (BB code):
Freq  Multiple
100    300
200    150
250    120
300    100
400    75
500    60
So you need to generate a timing rate of 1/30,000 or 33.33333uS, and count how many you get. If you put all outputs on the same I/O port you can compute the *next* output state during a timer ISR... then when you state the ISR you immediately output the last computation, then compute the next one. That way you avoid any jitter due to computations.

As you need to get all 6 computations complete in one ISR you need to be pretty fast. If your instruction clock is 128 times the rate you will run at 3.84MHz and have 128 instructions total to compute the frequency and complete the ISR, and do anything else this app may require.
 

Markd77

Joined Sep 7, 2009
2,806
I had a try and got 12000Hz, all the multiples are now even, so can be divided in half for a 50% duty square wave.
So the crystal should be 6 or 12 MHz.
(ErnieM, all your multiples have 5 as a factor).
Freq| Multiple
100| 120
200| 60
250| 48
300| 40
400| 30
500| 24
 

ErnieM

Joined Apr 24, 2011
8,377
Mark: Cool. Didn't see I could reduce by the factor of 5 and forgot it's a square wave so need to half the period. That's what I get for posting before I finish my coffee. :D
 

THE_RB

Joined Feb 11, 2008
5,438
As another simplification, frequencies;
500
250

and
400
200
100

are binary multiples, in the old days you would just inc the output PORT, like;
PORTB++;
then 3 pins of the output port do this;
pin0 = 400 Hz
pin1 = 200 Hz
pin2 = 100 Hz

So with a couple of ports like most PICs have, you could generate two binary frequency chains.

Another option would be to have a lookup table where each bit is one "frequency" output, and just step through all the lookup table entries at a fixed rate. Using MarkD77's table that would just need a lookup table of 120 bytes, and output one byte every 12000 Hz.
 
Top