Skip to main content
Explorer
October 21, 2025
Solved

Using HRTIM to Output PWM input from the other Timers

  • October 21, 2025
  • 1 reply
  • 200 views

Hello everyone,

I am doing a project of sampling a PWM signal and reproduce it dynamically using the HRTIM. The following code:

uint16_t oldPer = 0, oldDur = 0, update = 0;

uint16_t newPeriod = 0;

uint16_t newDuration = 0;

 

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

 

if (htim->Instance == htim2.Instance && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)

    {

    newPeriod = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

    newDuration = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

 

    if (oldPer > (newPeriod + 100) || oldPer < (newPeriod - 100))

    {

                     HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].PERxR  = newPeriod;

       update++;

    oldPer = newPeriod;

    }

    if (oldDur > (newDuration + 100) || oldDur < (newDuration - 100))

    {

                     HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].CMP1xR = newDuration;

       update++;

    oldDur = newDuration;

}

    if (update)

        {

            HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].CNTxR = 0;  // reset counter

    HAL_HRTIM_SoftwareUpdate(&hhrtim1, HRTIM_TIMERUPDATE_A);

    }

    update = 0;

    }

}

 

works just fine, in respect of changing on the fly the period and duty cycle from the TIM2 for the HRTIM, meaning i hook it on the oscilloscope and check that the output frequency and duty cycle match the input. However, as the attached video shows, the output (blue) pulse 1) "drifts" in respect of the input, 2) there are some spikes as also 3) the output is in mVolts. Does Anyone know what's wrong? For the set up, all grounds (AWG, probes) are connected to the same pin (the GND of the stm), the connections are tight and in the video the input is a 10KHz 20% PWM. The code programmed in the stm is in release mode. I use an stm32g4. If i can provide you with any more information, please let me know.

Thank you!

    This topic has been closed for replies.
    Best answer by TDK

    1) There's nothing in the code to prevent drift, or to synchronize the output to the input. You could use a timer ETR pin for this.

    2) Traces and probes will pick up some amount of noise. Nothing to worry about.

    3) Is probe in 10:1 mode? Input signal also is 330 mV, suggests mismatch between probe setting and setting on the scope.

     

    You can't reproduce a signal exactly, but you could get close, within a few timer clocks perhaps. I don't see the usefulness of this. Use a voltage follower if you need the signal for something else.

    1 reply

    TDKAnswer
    Super User
    October 23, 2025

    1) There's nothing in the code to prevent drift, or to synchronize the output to the input. You could use a timer ETR pin for this.

    2) Traces and probes will pick up some amount of noise. Nothing to worry about.

    3) Is probe in 10:1 mode? Input signal also is 330 mV, suggests mismatch between probe setting and setting on the scope.

     

    You can't reproduce a signal exactly, but you could get close, within a few timer clocks perhaps. I don't see the usefulness of this. Use a voltage follower if you need the signal for something else.