AnsweredAssumed Answered

How to change PWM duty cycle on s32k144?

Question asked by Alex Alexandrov on May 9, 2018
Latest reply on May 18, 2018 by Daniel Martynek

Hello, I have initialized PWM, but I can change duty cycle in runtime.

 

The strange moment, if I put breakpoint on this line (FTM0->CONTROLS[channel].CnV = (FTM0->MOD * duty) / 100;), and step over, after that press continue - dc changed.

If I just stop on this line, and press continue - dc unchanged.

 

This is my initialization:

    /* Select and enable clock for FTM0 */
    PCC->PCCn[PCC_FTM0_INDEX] = PCC_PCCn_PCS(1) | PCC_PCCn_CGC_MASK;
    /* Set PORTB pins for FTM0 */
    PORTD->PCR[15] = PORT_PCR_MUX(2);     // FTM0, Channel0
    PORTD->PCR[16] = PORT_PCR_MUX(2);     // FTM0, Channel1
    /* Enable registers updating from write buffers */
    FTM0->MODE = FTM_MODE_FTMEN_MASK | FTM_QDCTRL_QUADEN_MASK | FTM_MODE_PWMSYNC(1);
    /* Enable sync, combine mode and dead-time for pair channel n=1 and n=2 */
    FTM0->COMBINE = FTM_COMBINE_COMP0_MASK | FTM_COMBINE_DTEN0_MASK | FTM_COMBINE_COMP1_MASK | FTM_COMBINE_DTEN1_MASK;
    /* Set Modulo in initialization stage (10kHz PWM frequency @112MHz system clock) */
    FTM0->MOD = FTM_MOD_VAL();
    /* Set CNTIN in initialization stage */
    FTM0->CNTIN = 0;
    /* Enable high-true pulses of PWM signals */
    FTM0->CONTROLS[0].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK) & ~FTM_CnSC_ELSA_MASK;
    FTM0->CONTROLS[1].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK) & ~FTM_CnSC_ELSA_MASK;
    /* Set channel value in initialization stage */
    FTM0->CONTROLS[0].CnV=FTM_CnV_VAL(700); // 50% duty cycle
    FTM0->CONTROLS[1].CnV=FTM_CnV_VAL(100); // 50% duty cycle
    /* Reset FTM counter */
    FTM0->CNT = 0;
    /* Insert deadtime (1us) */
    FTM0->DEADTIME = FTM_DEADTIME_DTPS(3) | FTM_DEADTIME_DTVAL(7);

 

    /* Clock selection and enabling PWM generation */
    FTM0->SC = FTM_SC_PS(1) | FTM_SC_CLKS(1) | FTM_SC_CPWMS_MASK | FTM_SC_PWMEN0_MASK | FTM_SC_PWMEN1_MASK;
}

 

 

What am I doing wrong?

Outcomes