View previous topic :: View next topic |
Author |
Message |
piciT Guest
|
two tones together.. |
Posted: Fri Nov 13, 2009 5:58 am |
|
|
Hi
i am using the pic16f series, and i want to create 2 tones at the same time.
for example , my pwm can create one wave of 400hz , and i need to create at the same time another 200hz ,in other pwm pin .
it says that i have couple of pwm chanels,but how do i use them together?
is theres any way of doing that ?
thanks. |
|
|
yerpa
Joined: 19 Feb 2004 Posts: 58 Location: Wisconsin
|
|
Posted: Fri Nov 13, 2009 11:07 am |
|
|
Why not mix the two tones together in your program then output the result to a single PWM channel? For example: compute sample for tone 1, compute sample for tone 2, add them together, scale the result if necessary, then output to PWM. You can mix many tones using this method. |
|
|
picit Guest
|
what?? |
Posted: Fri Nov 13, 2009 3:48 pm |
|
|
what ???
how ??
I know how to produce one square wave with the pwm function.
I have only one timer, so I can't do 2 pwm at the same time anyway.
So how do I produce any other tone at the same time ?
I couldn't understand what exactly you mean...
Program can do only one pwm at a time... isn't it ? |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Sat Nov 14, 2009 9:42 am |
|
|
The two PWM outputs can be different duty cycles, but they have to be at the same frequency.
But if you're working in the range of a few hundred Hz, you could use one hardware PWM and generate a second tone with software. |
|
|
picit Guest
|
no// |
Posted: Sat Nov 14, 2009 9:53 am |
|
|
no i cant use hardware because the tones has to be changed many times.
"yerpa" , what did you mean by:
"compute sample for tone 1, compute sample for tone 2 " ??
i can produce only one freq' at a time.. i need two.. |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Sat Nov 14, 2009 11:18 am |
|
|
Maybe I wasn't being clear. What I meant by "hardware" was the processor's built-in PWM generator. And the tone produced by "software" would be generated using an interrupt occurring at twice the desired frequency--setting the output high and low alternately. |
|
|
yerpa
Joined: 19 Feb 2004 Posts: 58 Location: Wisconsin
|
|
Posted: Mon Nov 16, 2009 2:57 pm |
|
|
I setup the PWM channel like this:
Code: |
// PWM
setup_ccp1 (CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty (0x7f);
|
The PIC's PWM output is low-pass filtered filtered by a cap and resistor to remove the PWM clock frequency which is above audio range anyway. Then just treat it as a regular DAC, writing your output samples sequentially at an audio sample rate. For example, if your current sample is called "audio_sample", then use this line:
Code: |
set_pwm1_duty(audio_sample);
|
Notice that you are changing the PWM duty cycle, not the PWM frequency.
One method you could use to compose your samples is to make a lookup table, say 1024 bytes, with a single cycle of your single-frequency tone. Then, by indexing through the table you can get your sample values. If you increment the index by a different value, you get a different frequency. If you maintain two indexes into the same table, with different increments for each index, then add the two values numerically before outputting the result to the DAC, you will hear the two frequencies superimposed.
This explanation is grossly over simplified. If it still seems confusing, I recommend Hal Chamberlin's book "Musical Applications of Microprocessors". Also Microchip has some appnotes on DTMF (dual tone multi frequency) signal generation.
I hope this helps. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 16, 2009 3:34 pm |
|
|
You're right, that complex signals (starting with dual tone or any arbitrary waveform) can be only generated by
using a modulation method, e.g. PWM modulation, and of course the PWM frequency has to fulfill the Nyquist criterion,
being higher than twice the highest signal frequency.
Unfortunately all other contributors, also the originator are targetting to "PWM" (= generated by the PIC
PWM output) square waves. Obviously, you can't generate more than one frequency on a pin using this method,
and you're stuck with a rather boring waveform. |
|
|
piripic
Joined: 15 Jan 2008 Posts: 25
|
|
Posted: Tue Nov 17, 2009 10:09 am |
|
|
I think yerpa is right. Maybe it is a bit "resource consuming"..but with one PWM, one Timer and some code you're probably done: use a timer and every timer interrupt you increment two tables index (use two different tables and check for roll over), read the tables values, sum them together and scale the result. Load the result into the PWM duty cycle register. Of course the tables must be different in length to obtain two different freq., you must calculate two different table. Maybe it is possible to use only one table and skip some sample on one index. The above method if not for square wave but for sine wave, triangular wave, sawtooth wave.. etc. If you want generate two square wave at a different freq. use two timer with interrupts.
I hope this help,
Claudio |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Tue Nov 17, 2009 8:01 pm |
|
|
If you want to generate two square wave at a different freq, use two timers with interrupts.
I don't think that works. What happens when the two timers need to operate at the same instant? Obviously one will block the other, and one wave won't be delivered at the right frequency. It's worst when the two frequencies are close, but not exactly the same.
But if one wave is generated automatically by the processor's PWM output, there only needs to be one timer operating, and that problem doesn't occur. |
|
|
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Wed Nov 18, 2009 9:38 am |
|
|
John P wrote: | I don't think that works. What happens when the two timers need to operate at the same instant? Obviously one will block the other, and one wave won't be delivered at the right frequency. |
It depends on how precise you need the outputs to be. At audio frequencies, a little jitter of a few microseconds is probably not going to be a problem. If the two timer interrupts occur at the same time both will be serviced; one will just have to wait until the other is finished. If all you're doing is toggling an output pin, the delay would be pretty small. |
|
|
|