Update PWM period on runtime

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

Update PWM period on runtime

2,226 Views
nadiajarray
Contributor II

I'd like to generate sine waves of different frequencies using PWM that means that I need to vary also the period of PWM to achieve that.

I've used a PWM_LDD component where I can set the period and a TimerUnit_LDD component for the hardware PWM but don't  provide any method to change the period.

what I'm doing wrong ?

Labels (1)
Tags (4)
0 Kudos
4 Replies

1,431 Views
DavidS
NXP Employee
NXP Employee

Hi Nadia,

If you look at the PWM_LDD help, it states the following:

Component PWM_LDD

Pulse width modulation

Component Level: Logical Device Driver

Category: Logical Device Drivers-Timer

This component implements a pulse-width modulation generator that generates a signal with variable duty and fixed frequency (period).

The PWM_LDD Component has the frequency fixed.

But since it is "just software" you could play around with Events.c PWM1_OnEnd() ISR as shown below to allow changing frequency. The frequency range depends on the clock configuration so this is just an example:

void PWM1_OnEnd(LDD_TUserData *UserDataPtr)

{

/* Write your code here ... */

#if 0 //DES 0=change frequency, 1=change duty

PWM1_SetRatio16(MyPWM1Ptr,++ratio); //change duty

#else

static unsigned increment=0;

increment++;

if(!(increment%10)) //DES slow down frequency change to enable watching on oscope

{

FTM_PDD_WriteModuloReg(FTM0_BASE_PTR, ++ratio);

FTM_PDD_WriteChannelValueReg(FTM0_BASE_PTR, 0, (uint16_t)ratio/2);

increment=0;

}

#endif

}

I tested the above using KDS+PE for the TWR-K70.

Regards,

David

1,431 Views
nadiajarray
Contributor II

Thank you !

So to update the PWM period, I only need to update the modulo register with FTM_PDD_WriteModuloReg and set the number of timer ticks.

I only don't get the difference between FTM_PDD_WriteChannelValueReg and and SetRatio16 ..

0 Kudos

1,431 Views
DavidS
NXP Employee
NXP Employee

Hi Nadia,

Glad to help.

Again, the LDD is limited to only change duty cycle of a constant frequency (period).  When PWM1_SetRatio16() called, it reads what the period (aka FTM->MOD register which is constant), uses the ratio value (0-65535  where 0=0% duty cycle, 32767=50% duty cycle, 65535=100% duty cycle) you passed in to calculate the new FTM->FTMx_CnV register VAL to have PWM generate the correct duty cycle.

The code I provided is accessing the FTM/PWM registers directly and by changing the MOD and having VAL=1/2MOD the frequency is changing and the duty cycle is set to 50%.

Regards,

David

1,431 Views
brunotremblay
Contributor III

Hello David,

If I undersand, the PWM value register can only be change du ring the ISR?  Insted of using the PWM_LDD component, I want to use the Init_FTM component to be able to combine pair of PWM channels. I didn't find the Processor Expert settings to generate the ISR yet. Do you have an example for the Init_FTM component?

Regards

Bruno

0 Kudos