generate different freq with mcu

Discussion in 'General Electronics Chat' started by aamirali, Aug 17, 2012.

Feb 2, 2012
415
1
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: Aug 17, 2012
2. t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
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

3. ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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

Code ( (Unknown Language)):
1. Freq  Multiple
2. 100    300
3. 200    150
4. 250    120
5. 300    100
6. 400    75
7. 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.

4. Markd77 Senior Member

Sep 7, 2009
2,803
594
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 likes this.
5. ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

6. THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
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.