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
Solved! Go to Solution.
Hi ikki shingu,
Because your FTM mode is in synchronization enhanced mode, please change your code
FTM2_C1V = duty;
duty = 0x01; /* for updating a duty value on running */
FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;
in for(;;) to the following code:
FTM2_COMBINE |= FTM_COMBINE_SYNCEN0_MASK; |
FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;
FTM2_SYNC = 0x00;
FTM2_C1V = duty; | |
FTM2_SYNC = 0x80;// generate software trigger to change the duty value |
Then you will find your FTM2_C1V changed to the new data duty.
Besides, we have an application code about this, AN4560: pwm synchronization using kinetis flextimers, the link is:
http://cache.freescale.com/files/microcontrollers/doc/app_note/AN4560.pdf?fsrch=1&sr=1&pageNum=1
Wish it helps you!
If you still have question, please contact me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi ikki shingu,
Because your FTM mode is in synchronization enhanced mode, please change your code
FTM2_C1V = duty;
duty = 0x01; /* for updating a duty value on running */
FTM2_SYNC |= FTM_SYNC_SWSYNC_MASK;
in for(;;) to the following code:
FTM2_COMBINE |= FTM_COMBINE_SYNCEN0_MASK; |
FTM2_SYNCONF |= FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SYNCMODE_MASK;
FTM2_SYNC = 0x00;
FTM2_C1V = duty; | |
FTM2_SYNC = 0x80;// generate software trigger to change the duty value |
Then you will find your FTM2_C1V changed to the new data duty.
Besides, we have an application code about this, AN4560: pwm synchronization using kinetis flextimers, the link is:
http://cache.freescale.com/files/microcontrollers/doc/app_note/AN4560.pdf?fsrch=1&sr=1&pageNum=1
Wish it helps you!
If you still have question, please contact me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Jingjing-san,
Thanks to your advice, I can change the duty during running.
I understand settings of COMBINE[SYNCEN0] and SYNCONF[SWRSTCNT] were missing in my original code.
Thank you for your help!
Best Regards,
Ikki
Where you able to update the FTM2_MOD also?
Hi vignesh balaji,
Please also follow application note AN4560, then you can update the MOD register.
Wish it helps you!
Jingjing