Hello community,
I encounter a problem with the PWMMC peripheral on the 8-bits microcontroler MC68HC908MR16 from freescale. Let me present you the problem.
I. Configuration
II. Project presentation
I'm working on an old software that control a three-phase motor with a scalar control approach (open loop), based on the application note AN1857 (attachments). The principle is to loop through a sine wave (with its third harmonic) look up table each 0.4ms (2.5kHz) and output PWM signal describing this sine wave with the good duty cycle. I would like to generate a 220Hz sine wave with 2.5kHz carrier frequency => 11 points for the whole signal.
III. Problem description
When I see the 6 PWM generated on a scope, I notice an inconsistent duty cycle. So, I try a simple approach to test PWMMC's configuration with only 2 duty cycle values (250 and 500 corresponding to 25% and 50% duty cycle respectively).
//The table wih the dummy duty cycle values
const unsigned long aul_pval[2] = {
250,
500
};
In the PWM interrupt, I cycle between these two values and load them into the PVAL1, PVAL3 and PVAL5 registers :
unsigned int uiPhase = 0;
void vPWMcalc (void){
COPCTL=0x00;
PCTL1.R &= 0xE3; /* clear PWMF bit */
PVAL1 = aul_pval[uiPhase];
PVAL3 = aul_pval[uiPhase];
PVAL5 = aul_pval[uiPhase];
if (uiPhase >= 1) {
uiPhase = 0;
}
else {
uiPhase++;
}
PCTL1.R |= 0x02; /* set LDOK bit */
}
However, when I ran this code, the PWM output does not work as I expected :
On the next figures, the yellow signal is the High PWM and purple signal the Low PWM. The blue one is just a port that I toggle in the PWM interrupt to show the center aligned mode.
Figure 1 : Duty cycle of 50% OK, but the next pulse is 50% too
Figure 2 : Duty cycle of 25% OK, but the next pulse is 25% too
Figure 3 : unknown PWM duty cycle (35% in principle)
Its look likes there is a reload every 2 cycles, whereas I configure the PWMMC peripheral with a reload every cycle, but is does not explain the unknown duty cycle on figure 3. Moreover, it look likes the bufferisation of PVAL1, PVAL3 and PVAL5 are not applied every cycle.
III. PWMMC peripheral configuration
Here the configuration of the PWMMC peripheral in the project :
I attach too the code and PWMMC configuration of Freescale Application Note 1857 to see differences.
IV. What I try to solve the problem (but does not work)
After many searches on the web and in the documentation, I don't understand the problem
Thank you in advance for your answers !
Regards.
Cruvix.
Solved! Go to Solution.
OK,
Problem solved, the PWMMC device did not start correctly. Instead of enabling the PWMMC with the PWMEN bit only, we enabled it with other bits. In addition, we wrote (with an ‘=’) instead of reading and writing (with ‘|=’) to the PCTL1 register. As the documentation says, the PWMMC device buffers the registers and requires the register to be read before writing.
OK,
Problem solved, the PWMMC device did not start correctly. Instead of enabling the PWMMC with the PWMEN bit only, we enabled it with other bits. In addition, we wrote (with an ‘=’) instead of reading and writing (with ‘|=’) to the PCTL1 register. As the documentation says, the PWMMC device buffers the registers and requires the register to be read before writing.