AnsweredAssumed Answered

how to update a duty value of KEA128 FlexTimer

Question asked by Ikki Shingu on Oct 2, 2015
Latest reply on Nov 1, 2015 by Kerry Zhou

Hello,

 

I make the sample code to update a duty value of KEA128 FlexTimer during running timer count

(i.e. real time duty update),

but it doesn't work well.

 

This is my sample code, then could you let me know what should be modified?

Or could you provide the sample code to update a duty value?

 

 

[Condition]

PWM output : FTM2 Ch1

PWM setting mode : Edge-Aligned

Modulo : 0x000000FF(256counts)

Clock Source : system clock

 

[Observation]

the fisrt duty value(0x40) is effected,

but the second duty value(0x01) is not effected.

 

[Source Code]

--

/* global valiables */

volatile unsigned long duty = 0x00000040;

 

void Clk_Init()
{
        ICS_C1|=ICS_C1_IRCLKEN_MASK;   /* Enable the internal reference clock*/
while(!(ICS_S & ICS_S_LOCK_MASK));   /* Wait for FLL lock, now running at 48 MHz (1280 * 37.5Khz) */
        ICS_C2 = 0x00;    /*BDIV=1, Bus clock = 48 MHz*/
ICS_S |= ICS_S_LOCK_MASK ;    /* Clear Loss of lock sticky bit */
}

 

void FTM_Init()
{
SIM_SCGC |= SIM_SCGC_FTM2_MASK; /* Enable Clock for FTM2 */
       
        FTM2_MODE |= FTM_MODE_FTMEN_MASK; /* FTM2 enabled */
       
        /* Mask the output channels for safe state */
        FTM2_OUTMASK |= FTM_OUTMASK_CH1OM_MASK; /* FTM2 ch1 output mask */
       
        /* Set Modulo */
        FTM2_MOD = 0x000000FF; /* 0xFF(255) + 1 = 256 counts */

       /* Channel 1 : Edge-alined PWM */
        FTM2_C1SC |= FTM_CnSC_MSB_MASK;  /* Channel as Edge-aligned PWM mode */
        FTM2_C1SC |= FTM_CnSC_ELSB_MASK; /* High-true pulses */

        /* Set period value */
        FTM2_C1V = duty;

        /* write any value to CNT in order to reset FTM Counter and update the channels output according to new configuration */
        FTM2_CNT = 0x00000001;
       
        /* Clock setting */
        FTM2_SC |= FTM_SC_PS(0); /* Select Prescaler in this case 1. 48 Mhz /1 =48Mhz. */
        /* Counter increase by one every 20.8 ns */
        FTM2_SC |= FTM_SC_CLKS(1); /*FTM2 use system clock*/

       
        FTM2_SYNCONF |= FTM_SYNCONF_SYNCMODE_MASK; /* for PWM duty updating sync by SWSYNC */
        FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK; /* for PWM duty updating sync by SWSYNC */
        FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK; /* for PWM duty updating sync by SWSYNC */

        /* Unmask the output channels */
        FTM2_OUTMASK &= ~FTM_OUTMASK_CH1OM_MASK;
       
}

void main(void)
{
 
Clk_Init();     /* Configure clocks to run at 48 Mhz */
FTM_Init();    /* Initialize the FTM module */

        FTM2_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK; /* Loading updated values is enabled */
       
for(;;) {
          FTM2_C1V = duty;
          duty = 0x01; /* for updating a duty value on running */
          FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;
}

}

 

--

 

Best Regards,

 

Ikki

Outcomes