AnsweredAssumed Answered

square waves with different frequencies on FTM2 of KEA-128

Question asked by vignesh balaji on Oct 31, 2015
Latest reply on Nov 1, 2015 by xiangjun.rong

Hi,

I want to generate square waves of multiple frequencies by using FTM2 of KEA-128.For this I have to update the values of FTM2_CnV,FTM2_MOD and FTM2_CNTIN.I am not able to update these values properly.

 

volatile unsigned long duty =0x00000080;

 

 

void Clk_Init()

 

{

ICS_C1|=ICS_C1_IRCLKEN_MASK;  /* Enable the internal reference clock*/

ICS_C3= 0x90; /* Reference clock frequency = 31.25 KHz*/

while(!(ICS_S & ICS_S_LOCK_MASK));   /* Wait for PLL lock, now running at 40 MHz (1280 * 31.25Khz) */

    ICS_C2|=ICS_C2_BDIV(1)  ;  /*BDIV=2, Bus clock = 20 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 */

SIM_PINSEL1 |= 0x00000008;      //selecting pin F1

       

  //      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 = 0xFFFFFFFF;

         ; /* 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(4); /* Select Prescaler in this case 1.  20Mhz /16 =1.25Mhz. */

        /* (1/1.25Mhz) = 0.8 microsec */ /*Counter increase by one every 0.8 microsec */

        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 */ // selects software trigger as PWM synchronization

        /* Unmask the output channels */

        FTM2_OUTMASK &= ~FTM_OUTMASK_CH1OM_MASK;

       

}

 

 

void main(void)

{

 

Clk_Init();     /* Configure clocks to run at 20 Mhz */

FTM_Init();    /* Initialize the FTM module */

        FTM2_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK; /* Loading updated values is enabled */

       

for(;;) {

 

    UINT8 i;

   

 

          FTM2_COMBINE |= FTM_COMBINE_SYNCEN1_MASK;       /* PWM synchronization between pair of channels */

              FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

          FTM2_C1V = 0x952;

          FTM2_MOD = 0x12A4;  

          FTM2_CNTIN = 0x00000001;                                                                                                                                             //HAP

          FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value

 

 

 

 

  

 

      FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

      FTM2_C1V = 0x952;

      FTM2_MOD = 0x12A4;

      FTM2_CNTIN = 0x00000001;                                                                                                                                                        //py

      FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value 

 

   

   

 

 

  FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

  FTM2_C1V = 0x86C;

  FTM2_MOD = 0x109C;                                                                                                                                                                          //birth

  FTM2_CNTIN = 0x00000001;

   FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value 

 

 

  FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

FTM2_C1V = 0x952;                                                                                                                                                                                        //Day

FTM2_MOD = 0x12A4;

FTM2_CNTIN = 0x00000001;

FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value   

 

   

 

 

FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

FTM2_C1V = 0x6FF; 

FTM2_MOD = 0xDFE; 

FTM2_CNTIN = 0x00000001;                                                                                                                                                                                   //TO

FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value  

 

   

 

 

FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;

FTM2_C1V = 0x766; 

FTM2_MOD = 0xECC;                                                                                                                                                                                             //YOU

FTM2_CNTIN = 0x00000001;                                                                                               

FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;// generate software trigger to change the duty value 

 

       

}

}

Outcomes