PDA

View Full Version : Generating tones with decimals using PWM?



atomski
- 23rd March 2004, 11:47
Here's another one,

How would one generate tones using
PWM if decimal numbers are involved
(e.g. 123.4 Hz)? Cute, huh?

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Melanie
- 30th March 2004, 11:24
Sometimes, I think people ask too much of the PIC and expect it to do things when other external hardware exists that would do it better. But since you mailed me off-list to give you a solution, then here’s an obvious one you really should have thought of yourself... which is one reason I didn't reply to this thread originally.

Why not just calculate the period time of your requirement and write a routine to produce it?

123.4Hz has a period of 8.103mS, therefore it has a High of 4.052mS and a Low similarly. There will then be 1000/8.103=123.4 cycles produced in a second.

CounterA var Byte
TonePin var PortB.0
.. ..

‘ Approximate 1 second burst of 123.4Hz

BurstLoop:
For CounterA=0 to 123
High TonePin
PauseUS 4052
Low TonePin
PauseUS 4048
‘ slightly lower to account for approximate
‘ propogation delay through For-Next Loop
Next CounterA

Accuracy will depend on how good your PIC’s clock is. Use a Scope to ensure linearity of High and Low Pulses along with a Frequency Counter to check the result. Adjustments are made by obviously increasing or decreasing the appropriate pauses. I’ll let you do that.

Melanie

atomski
- 30th March 2004, 11:52
Mel,

Thank you very much for the reply.
Sometimes, the most obvious solution
is the one that seems least probable to
the person with little experience. I did
think about this matter a lot and even
considered using a 10x the output freq.
from the PIC dividing it with an ext.
IC to the original frequency. However
my project is confined to small area of
16 x 12 mm and this solution wasn't
possible. I like your suggestion and I
will most definitely use it for my project
with a slight modification. I will need to
make a lookup table for the generation
of tones to cover all standard freq. From
67 - 250.3 Hz (CTCSS) & non-standard
EIA tones in that range. Mel, I really appreciate your help and apologize for
any inconvenience I might have caused
by my persistence.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

atomski
- 1st April 2004, 06:41
Mel,

I've implemented your solution and it
worked 100% (why shouldn't it, right?).
However as I suspected, dividing the
signal of e.g. 10ms (100Hz) into 2 half-
cycles would produce a square wave
shaped signal not a 256 step sinewave.
I did use a double RC filter (1K, 100n)
at the output, however while observing
the signal with an oscilloscope and a
spectrum analyzer all odd harmonics
were still there looking pretty strong,
thus deforming the overall signal shape.
I was wondering if you could elaborate
on how would I implement PWM in order
to generate already mentioned freq. in
the 67.0 - 250.3Hz range. This way I
could generate a sine-wave-like signal
right from the pic and filter out all the
harmonics with a double RC filter in
order to get a "true sinewave". Thank
you very much in advance...

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Melanie
- 1st April 2004, 10:25
You're not listening when I say that there are times a PIC alone is unsuitable for an application.

Hardware PWM will not go down to the frequencies you required (consult your PIC's Datasheet if you don't believe me). PBP's software PWM doesn't allow for frequency changes. However if you insist on the PWM route, you may like to do a search for 'Slow Software PWM' or 'SSPWM' in the MeLabs archives that I directed you to on another thread. I cannot comment on the software or it's suitability, but I'd calculate that generating nice sine waves will have a major software overhead.

Why don't you just use a CTCSS encoder/decoder chip (like an MX165CP) and have the PIC control that? A simple web search will give you heaps of ways of generating CTCSS tones cheaply and most can be interfaced to a PIC quite easily... example...

http://www.xs4all.nl/~pa0nhc/Projects/CTCSS/English/icl8038encoder/icl8038encoder.htm

Melanie

atomski
- 1st April 2004, 12:11
Mel,

I understand. I will try something else and
post it in this thread if it should work...

PS MX IC's are sweet but as I said my
project is limited to a 16 X 12 X 3 mm area
plus it has to be done on a very tight
buget ;)

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President