Skip to main content
Associate III
May 9, 2024
Solved

starting two PWM timers with a defined offset between each other

  • May 9, 2024
  • 1 reply
  • 1062 views

Hi,

I want to start two PWM timers at the same frequency but with an offset of a few 100 us to few ms range but it doesn't appear to work. 

I've been experimenting with something like this:

PCLK1Freq = HAL_RCC_GetPCLK1Freq()*2;
TIM3->ARR = (uint32_t)(((double)PCLK1Freq / (double)m_dat.fI) / ((double)TIM3->PSC + 1)) - 1;
TIM4->ARR = (uint32_t)(((double)PCLK1Freq / (double)m_dat.fI) / ((double)TIM4->PSC + 1)) - 1;
TIM3->CCR1 = TICKPERNANOSEC * (float)m_dat.fW;
TIM4->CCR1 = 1;
HAL_TIM_PWM_Start_custom(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start_custom(&htim4, TIM_CHANNEL_1);
__HAL_TIM_ENABLE(&htim3); // Ensure the timer is actually enabled
vTaskDelay(delay_cnt);
__HAL_TIM_ENABLE(&htim4); // Ensure the timer is actually enabled

The function HAL_TIM_PWM_Start_custom() is nothing else than HAL_TIM_PWM_Start() with the __HAL_TIM_ENABLE calls commented out to be able to set the timers up and then start them back to back.

Now turns out that the call to vTaskDelay() doesn't actually allow me to properly offset thenm to eachother they appear to be in a fix phase with each other. How do I achieve the desired?

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

    I'd cofigure two timers as slaves driven by third timer, than pre-set & start both timers. They 'd wait clock from master , so start it as last step. To change PWM parameters do same things, stop master, reset & pre-set slaves, than restart.

    1 reply

    MasterT
    MasterTBest answer
    Lead II
    May 10, 2024

    I'd cofigure two timers as slaves driven by third timer, than pre-set & start both timers. They 'd wait clock from master , so start it as last step. To change PWM parameters do same things, stop master, reset & pre-set slaves, than restart.