PWM prescale

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PWM prescale

2,244 Views
StefX
Contributor I
    hello, I'm using an MCS12C32 and I want to generate a 60Hz PWM signal. On paper every thing is ok, but not in the real world. I'm using a LED to test, and it blinks every 4-5 sec. Can anyobody help me? The qurtz works at 16Mhz, this means a 8Mhz bus clock(right?).


SA=BUS clk/2*PSCLA     
BUS clk=8Mhz/128=62.5Khz
SA=62.5Khz/2*512=61Hz

here is the init():

void init_pwm(void)
{




 PWMPOL_PPOL0=1; //start pol
 PWMPOL_PPOL1=1;
 PWMPOL_PPOL2=1;
 PWMPOL_PPOL3=1;
 
 
 
 PWMCLK_PCLK0=1;//ch1- SA
 PWMCLK_PCLK1=1;
 PWMCLK_PCLK2=1;
 PWMCLK_PCLK3=1;
 
 
 PWMPRCLK_PCKA0=1;//prescale bus clk  with 128     8Mhz/128=62.5Khz
 PWMPRCLK_PCKA1=1;
 PWMPRCLK_PCKA2=1;
 
 PWMPRCLK_PCKB0=1;
 PWMPRCLK_PCKB1=1;
 PWMPRCLK_PCKB2=1;
 
 PWMSCLA=0xFFFF;  //prescale SA with 512
 PWMSCLB=0xFFFF;                  

 PWMPER0=255;//perioada
 PWMPER1=255;
 PWMPER2=255;
 PWMPER3=255;

 
}

Labels (1)
0 Kudos
3 Replies

516 Views
allawtterb
Contributor IV


StefX wrote:
SA=BUS clk/2*PSCLA     
BUS clk=8Mhz/128=62.5Khz
SA=62.5Khz/2*512=61Hz

This is the wrong way to calculate the PWM frequency.  Also, the maximum prescale by the SA clock is 512 (256 * 2).  Here is how the PWM frequency should be calculated.
 
A Clk = Bus Clk / Prescale A (Set by PWMPRCLK_PCLKA[2:0])
SA Clk = A Clk / Prescale SA (Set by PWMSCLA)
PWM Freq = SA Clk / PWM Period (Set by PWMPERx) 
0 Kudos

516 Views
StefX
Contributor I
    Yes....you are right.

Thank you all for your answers!
0 Kudos

516 Views
Steve
NXP Employee
NXP Employee
I haven't checked your code but it sounds like you're about 256 times slower than expected.
Each PWM period is 256 counts (PWMPERx) so the total repetition period is 256 clock ticks. You don't mention the value of the duty registers.
The PWM clock period is one count on the PWM counter, so you need to reduce the PWM period or increase the clock frequency.
0 Kudos