Dynamically change pwm frequency on FTM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dynamically change pwm frequency on FTM

Jump to solution
1,446 Views
Marc852
Contributor II

 

Hello everyone,

 

I use an FTM function to generate PWM output. I based my code on the SDK example "driver_examples => simple_PWM".

Actually to change PWM frequency I use "FTM_Deinit", then "FTM_CalculateCounterClkDiv" to modify frequency and "FTM_Init". But it's a really slow solution and it's not working properly if I modify my frequency many time too fast.

Is there a way to change dynamically the frequency (like duty cycle on the example) ? Have you got any example ? 

 

Thanks in advance for your help.

---------------------------

MCUXpresso 11.5.0

K22FN256M1

SDK_2_11_0_MK22FN256xxx12

---------------------------

Tags (5)
0 Kudos
1 Solution
1,289 Views
RaRo
NXP TechSupport
NXP TechSupport

Hello @Marc852,

  • Just to double check, could you please check the configuration of the port for the FTM0_CH3 in pin_mux.c file of your project? Here is the configuration from the driver example "ftm_simple_pwm" using CH6:

RaulRomero_0-1670433187047.png

 

  • In order to change frequencies, you may need the following line FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true); without it as you point out the frequency will not change. Could you please try to add the line every time you want to change the frequency after FTM_SetupPwm? Here is a sample of how the testing was made.

RaulRomero_1-1670433187052.png

Best regards, Raul.

View solution in original post

0 Kudos
6 Replies
1,392 Views
RaRo
NXP TechSupport
NXP TechSupport

Hello @Marc852,

In the example you have function: FTM_SetupPwm(). This function has a parameter named pwmFreq_Hz, that is for PWM signal frequency in Hz.

RaulRomero_2-1668614342907.png

[Function FTM_SetupPwm in main]

RaulRomero_1-1668614250634.png

[Declaration of function in fsl_ftm.c]

You could experiment with that function and modify the frequency of the PWM, without the use of FTM_DeInit() and FTM_CalculateCounterClkDiv.

Regards, Raul.

0 Kudos
1,381 Views
Marc852
Contributor II

Hello Raul,

Thanks for your help.

I had already seen this function but it did not work dynamically.

I tried again with FRDM-K22 demo board. I started PWM with 1 KHz and I modified it to 10 KHz with "FTM_SetupPwm()". Nothing happened on output, I kept a 1KHz frequency.

Did I made a mistake ?

I attach my test source files.

 

Cordially,

Marc

0 Kudos
1,353 Views
RaRo
NXP TechSupport
NXP TechSupport

Hello @Marc852,

Could you please check if you are entering this condition?

RaulRomero_0-1669139740756.png

[Driver fsl_ftm.c -> function FTM_SetupPWM()]

Due the range of frequency you are wanting to work, the function will not work it out. This is because the number of ticks the counter needs will overflow the mod register every time. FTM_SetupPWM() then, will only work with a lower range.

As you can read in the comment, the best solution for your range would be: Changing clock source to get the desired frequency. As you were doing in the first place.

Regards, Raul

0 Kudos
1,343 Views
Marc852
Contributor II

Hello @RaRo,

 That doesn’t seem to be the problem, the function return "kStatus_Success" each time.

I still tried to change frequency in lower range 1KHz => 1.1KHz but same result, the frequency keep 1KHz output.

 

Note: In comparaison to your screen the mod test return "kStatus_OutOfRange" instead of "kStatus_Fail".

 

Cordially,

Marc

0 Kudos
1,290 Views
RaRo
NXP TechSupport
NXP TechSupport

Hello @Marc852,

  • Just to double check, could you please check the configuration of the port for the FTM0_CH3 in pin_mux.c file of your project? Here is the configuration from the driver example "ftm_simple_pwm" using CH6:

RaulRomero_0-1670433187047.png

 

  • In order to change frequencies, you may need the following line FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true); without it as you point out the frequency will not change. Could you please try to add the line every time you want to change the frequency after FTM_SetupPwm? Here is a sample of how the testing was made.

RaulRomero_1-1670433187052.png

Best regards, Raul.

0 Kudos
1,281 Views
Marc852
Contributor II

Hello @RaRo,

This works perfectly by adding the function FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true) !

Looks like I missed it.. 

 

Thank you for your time and help.

Cordialy,

Marc

0 Kudos