2025-10-21 12:56 PM
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!