|
|
View previous topic :: View next topic |
Author |
Message |
mike1234
Joined: 06 Dec 2007 Posts: 6 Location: Chicago, IL
|
2 PWM frequencies at the same time |
Posted: Thu Dec 06, 2007 5:10 pm |
|
|
Hi everyone,
I need to output a different 2 different PWM frequencies at the same time. I am using a PIC 18F4520, which does have 2 CCP channels. I just don't know how to configure it exactly. I have to use a different timer than the default (timer 2) for the second CCP module, right? How do I do that? Here is my simple code for the first channel (that works). What should I add for the second one?
Code: |
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000) //clock freq = 20 MHz
int16 duty; //16-bit PWM duty variable
void main()
{
//Set CCP1 to PWM mode and set up timer2
setup_ccp1(CCP_PWM); //pin 17
setup_timer_2(T2_DIV_BY_1, 150, 1); //PWM freq = ~33kHz
//= 20MHz / (4*1*(150+1))
while(1)
{
duty = 151; //25% duty since 151/(4*151) = .25
set_pwm1_duty(duty);
}
}
|
I tried to search for this with no success. If the answer is already posted somewhere, could some one direct me to it?
Thanks,
Mike |
|
|
Guest_7068 Guest
|
|
Posted: Thu Dec 06, 2007 6:00 pm |
|
|
You will need to use the Timer4 for the second PWM.
Code: |
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM |CCP_USE_TIMER3 ); // sets up CCP2 to use alternate Timer
// setup your timers 2 and 4
setup_timer_2(T2_DIV_BY_1, 150, 1);
setup_timer_4( T4_DIV_BY_1, 50, 1);
while(1)
{
set_pwm1_duty(value);
set_pwm2_duty(value);
}
|
The header file only has 1 constant - CCP_USE_TIMER3 , but this will set the correct bits for the PWM mode to use alternate timer for that particular CCP module. You should get different frequencies, so make sure you use different duty cycles or else cause the output to be high all the time. |
|
|
Ttelmah Guest
|
|
Posted: Fri Dec 07, 2007 5:24 am |
|
|
Er.
The setup has constants for Timer3, because this is the _only_ other timer that can be used by the CCP. However this is _not_ available for PWM.
Look at tables 15-1, and 15-2 in the data sheet. Timer1, or Timer3, are available for use in _CCP_ mode, but in PWM mode, only Timer2 is supported.
There isn't even a tmer4 module on this chip...
It is rare though, to need different PWM frequencies. At the end of the day, normally the frequency selected, is not that critical (faster for smaller inductors, lower to reduce switching losses), so multiple frequencies are not supported. If you need different frequencies, you need to select a more complex chip that does support multiple PWM timers.
Best Wishes |
|
|
Guest_7068 Guest
|
|
Posted: Fri Dec 07, 2007 9:38 am |
|
|
Oops !
That was the wrong chip, sorry about that.
It should work on a 8722 family part though. |
|
|
mike1234
Joined: 06 Dec 2007 Posts: 6 Location: Chicago, IL
|
Uh oh |
Posted: Fri Dec 07, 2007 12:19 pm |
|
|
So am I out of luck? I need to vibrate two different things at their respective resonant frequencies, so I can't really get by with one freq. |
|
|
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
How about two different duty cycles but same frequency? |
Posted: Fri Dec 07, 2007 10:10 pm |
|
|
Ttelmah wrote: |
It is rare though, to need different PWM frequencies.......... If you need different frequencies, you need to select a more complex chip that does support multiple PWM timers.
|
Can you get different duty cycles simultaneously on CCP1 and CCP2 even though they both use the same timer and are running at the same frequency just by using the set_pwmX_duty() statement? |
|
|
Ttelmah Guest
|
|
Posted: Sat Dec 08, 2007 3:23 am |
|
|
Of course.
This is the normal use of the two PWM's.
There are three distinct hardware configurations, with some setups designed to confuse:
1) The standard PWM's. These normally allow two channels, operating at the same frequency, with different duty cycles. Great for most applications, but not for somebody who requires different frequencies.
2) Some chips with multiple PWM components (power PWM, plus normal PWM for example), which in some cases allow separate timebases for these components. One of these would solve the original requirement here, as in the 8722 family parts mentioned.
3) Units like the power PWM, which have more connections, than they have channels. So (for instance), in some cases, you have four outputs, but these can only be driven as H-Bridge drivers, or a single pin, etc., not allowing four distinct PWM's. This is where great care in selecting the chips is needed, since these are documented as having a number of PWM outputs, but if you look carefully, only a much lower number of actual PWM modules...
Best Wishes |
|
|
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
|
Posted: Sat Dec 08, 2007 8:47 am |
|
|
Ttelmah wrote: | Of course.
This is the normal use of the two PWM's.......
|
Hopefully I didn't steal this thread.... Originally I had also planned two frequencies as well, using a 16F887, which was chosen for it's SR latches used in capacitive touch button scanning.
I plan to use two outputs, one for dimming control of an LCD backlight, the other to control contrast. The contrast control may be a bit tricky but I'm in experimentation mode on this one |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|