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;
{
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
}
}
Hi, Vignesh,
I suggest you enable FTM overflow interrupt, in the ISR of FTM overfolw, you can update the FTM2_C1V, FTM2_MOD, FTM2_CNTIN. If you do want to update the registers in for() loop, I think you can set a flag in the ISR of FTM overflow, in the for() loop, poll the flag, if the flag is set, update the registers, and clear the flag so that ISR can set it again.
There are several methods to update the registers, I suggest you read the an5142 from the website:
The application note include the code to update the register with software updating or with LOAD updating.
It is not appropriate to update the register in main for() loop as you have done, because it is too frequent to update the FTM register in the for() loop.
Hope it can help you
BR
XiangJun Rong