View previous topic :: View next topic |
Author |
Message |
Mark30504
Joined: 02 Oct 2003 Posts: 2
|
???? Multiple PWM , How to set to different frequenies ??? |
Posted: Fri Oct 17, 2003 12:53 pm |
|
|
Is there a way set ccp1 and ccp2 (PWM) to diffent frequencies ?
Timer_2 seems to adjust both.
thanks
[email protected] |
|
|
fpgeh
Joined: 07 Sep 2003 Posts: 19 Location: Vancouver, BC
|
|
Posted: Fri Oct 17, 2003 1:19 pm |
|
|
I'm sure you can do a search to find many ways of performing PWM using a timer. Here's a code snipit anyways that uses timer1. It's not as transparent as the PWM peripherals, but it runs automatically without using many resources.
#define TIMER1_OVERFLOW_CNT 65535
static BOOLEAN pinState;
// These need to be initialized
static int16 cyclesPerPeriod; // must be < TIMER1_OVERFLOW_CNT
static int16 cyclesHigh; // must be < cyclesPerPeriod
#INT_TIMER1
timer1_isr() {
// toggle output and reload timer
pinState = !pinState;
if (pinState) {
output_high(PIN_XX);
set_timer1(TIMER1_OVERFLOW_CNT - cyclesHigh);
}
else {
output_low(PIN_XX);
set_timer1(TIMER1_OVERFLOW_CNT - (cyclesPerPeriod -cyclesHigh));
}
} |
|
|
Mark30504
Joined: 02 Oct 2003 Posts: 2
|
thanks |
Posted: Fri Oct 17, 2003 1:47 pm |
|
|
Hello Thanks
I am trying to use the built in PWM fuctions of ccp1 and ccp2
but I would like to run them at different frequencies.
thanks |
|
|
fpgeh
Joined: 07 Sep 2003 Posts: 19 Location: Vancouver, BC
|
|
Posted: Fri Oct 17, 2003 1:50 pm |
|
|
Yes, I understood your question. Unfortunately, as far as I know only CCP1 is capable of generating PWM outputs. Look at the datasheet. I was just giving an example of using a different resource to generate the 2nd PWM output. |
|
|
Roger Courtney
Joined: 15 Oct 2003 Posts: 3 Location: Cleveland, Ohio
|
Re: thanks |
Posted: Fri Oct 17, 2003 2:12 pm |
|
|
Mark30504 wrote: | Hello Thanks
I am trying to use the built in PWM fuctions of ccp1 and ccp2
but I would like to run them at different frequencies.
thanks |
According to the datasheet, when using CCP1 and CCP2 as PWM's, "The PWMs will have the same frequency and update rate (TMR2 interrupt)" It appears you can give them different duty cycles, however. _________________ rnc |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: thanks |
Posted: Fri Oct 17, 2003 2:38 pm |
|
|
Mark30504 wrote: | Hello Thanks
I am trying to use the built in PWM fuctions of ccp1 and ccp2
but I would like to run them at different frequencies.
thanks |
You can get different frequencies on the pic18 series. This is done by settign the PWM modules to seperate timers 2 and 4.
Code: | setup_timer_2 (T2_DIV_BY_1,0x20,4);
setup_timer_4 (T4_DIV_BY_16,0xFF,1);
setup_ccp1(CCP_PWM);
setup_ccp4(CCP_PWM);
SET_PWM4_DUTY (240);
SET_PWM1_DUTY (16);
bit_set(*0xFB1,6);
bit_clear(*0xFB1,3);
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Oct 17, 2003 2:44 pm |
|
|
You can use the Compare modules to toggle the output pins. With a little thought and careful programming, you can output 2 different PWM frequencies that are "psuedo" hardware controlled. The output is controlled by hardware but you manually load the on off times. Is the frequency a multiple of the other? What frequencies are you wanting and duty cycle? Does the duty cycle change? This method is easier to do on the 18F's since you can use timer 1 & 3 to control the CCP module. |
|
|
|