cancel
Showing results for 
Search instead for 
Did you mean: 

How to adjust Compare Value in HRTIM Basic PWM

Matt1234
Associate II

Hello,

currently I am using the NUCLEO-G474RE and the goal is to output a PWM signal simulating a sinewave. Therefore the duty cycle must continuously be adjusted.

My issue is that I seemingly cannot do so.

The Timer is configured as Timer D, TD1 output active

I selected the Basic configuration (though I also tried out the Advanced Configuration) and set the Period to 54400.

The PWM on TD1 was used and the Compare value was set to 27000. All of that works fine and I get my PWM.

However, when trying to change the Compare value using the HAL method or even by writing directly to the register, I don't see a change in my PWM signal.

With the normal timers I just used:

__HAL_TIM_SET_COMPARE

Now for the HRTIM I thought I'd use the equivalent:

__HAL_HRTIM_SETCOMPARE

But the latter does not work for me. It does set the correct register and if I use the __HAL_HRTIM_GETCOMPARE, it correctly reads the new value. But that does not influence the duty cycle of my PWM.

Am I missing something? Do I have to transfer it to another register somehow? Am I using the wrong timer configuration?

1 ACCEPTED SOLUTION

Accepted Solutions
Matt1234
Associate II

Nevermind, I have the solution. One needs to use the waveform timer.

View solution in original post

3 REPLIES 3
Matt1234
Associate II

Nevermind, I have the solution. One needs to use the waveform timer.

What do you mean with waveform timer? I am also struggling with the same problem. The correct register is set and I can read it with __HAL_HRTIM_GETCOMPARE but PWM doesnt change.

I was struggling with this too, in the end I started copying the parts of the auto-generated HRTIM stuff from main.c until I understood what he meant:

I am using higher-level commands, but here is what I have whittled it down to so far to successfully change compare value and see it reflected in the duty cycle of my signal: 

 

HRTIM_TimerCtlTypeDef pTimerCtl = {0};
pTimerCtl.UpDownMode = HRTIM_TIMERUPDOWNMODE_UP;
pTimerCtl.DualChannelDacEnable = HRTIM_TIMER_DCDE_DISABLED;

if (HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, &pTimerCtl) != HAL_OK)
{
Error_Handler();
}
HRTIM_SimplePWMChannelCfgTypeDef PWMCfg = {0};

PWMCfg.Pulse = 0x0380;
PWMCfg.Polarity = HRTIM_OUTPUTPOLARITY_HIGH;
PWMCfg.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
if (HAL_HRTIM_SimplePWMChannelConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1, &PWMCfg) != HAL_OK)
{
Error_Handler();
}

 

Before I was not including the HAL_HRTIM_WaveformTimerControl part and adding it in, as per the kinda vague advice above, is what made it work.