I want to generate PWM signal on a pin.
First I configure the module.
void PWM_Init(void)
{
//PTE6 - PWM output - FTM3_CH1
PORTE_PCR6 = PORT_PCR_MUX(6);
SIM_SCGC3 |= SIM_SCGC3_FTM3_MASK;
/* Turn off FlexTimer 3 and clear any pending Timer Overflow Flag
(TOF) using the FTM3_SC register (Status and Control register for FlexTimer 3)*/
FTM3_SC;
FTM3_SC = 0;
/* Disable write protection for FlexTimer 3 using the FTM3_MODE register*/
FTM3_MODE = FTM_MODE_WPDIS_MASK;
FTM3_MODE |= FTM_MODE_FTMEN_MASK;
//frequency 60000000/128 = 2.13us
FTM3_MOD = 0x01D6; //2.13us * 470 = 1001 us = 1kHz
//duty cycle
FTM3_C1V = 0x01D6 / 2; //set duty cycle 50%
FTM3_SC = FTM_SC_CLKS(FTM_SC_CLKS_SYSTEM_CLOCK) | FTM_SC_PS(FTM_SC_PS_DIVIDE_BY_128);
//Edge-Aligned PWM - clear Output on match
FTM3_C1SC = 0x028;
//Edge-Aligned PWM - set Output on match
//FTM3_C1SC = 0x024;
}
void PWM_SetDutyCycle(unsigned int duty_cycle)
{
unsigned int freq = FTM3_MOD;
if (duty_cycle > freq)
return;
else
FTM3_C1V = duty_cycle;
}
Then I test it.
while (1)
{
if (duty > 0x01D6)
duty = 0;
else
duty += 50;
PWM_SetDutyCycle(duty);
LPTMR_delay_ms(1000);
}
I see no PWM signal on PTE6. Also FTM3_C1V is always zero, it doesn't change its value.
I found the problem - FTM3_MODE |= FTM_MODE_FTMEN_MASK; the line should be omitted.
Well...If I pause in debug and manually set the FTM3_C1V - it starts to generate PWM.
The question is - why FTM3_C1V is not updated programmatically?