synchronize two timers with each other
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-03-19 3:42 PM - edited ‎2024-03-19 4:07 PM
Hi,
I'm working with a STM32F439 and I set both timers, timer3 & timer4 to produce a 460ns pulse at a 50Hz frequency with their PWM feature. Now I need to be able to control their offset to each other with the ability to set it to 0. But it appears like the lowest offsety I can achieve is 1.23us. I've reimplemented the Start function like this:
HAL_StatusTypeDef HAL_TIM_PWM_Start_custom(TIM_HandleTypeDef *htim, uint32_t Channel)
{
uint32_t tmpsmcr;
/* Check the parameters */
assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
/* Check the TIM channel state */
if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
{
return HAL_ERROR;
}
/* Set the TIM channel state */
TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
/* Enable the Capture compare channel */
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
{
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(htim);
}
/* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
{
tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
{
//__HAL_TIM_ENABLE(htim);
}
}
else
{
//__HAL_TIM_ENABLE(htim);
}
note the commented out __HAL_TIM_ENABLE(htim); lines. I call this on both etuimers and then, to enable them, I do:
__disable_irq();
__HAL_TIM_ENABLE(&htim3);
__HAL_TIM_ENABLE(&htim4);
__enable_irq();
which gives me an offset of 708ns. How can I further improve this?
Thanks!
Solved! Go to Solution.
- Labels:
-
STM32F4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-03-19 7:49 PM
Three options
- You can access the registers directly to enable them. Will cut down on delay.
- You can set the CNT of one to be a few counts in front of the other one so that when it starts a little delayed, the counters match. Will eliminate delay, but offset will depend on compiler settings.
- You can drive both timer with a master timer. Will be the most complex code, but will eliminate the delay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-03-19 7:49 PM
Three options
- You can access the registers directly to enable them. Will cut down on delay.
- You can set the CNT of one to be a few counts in front of the other one so that when it starts a little delayed, the counters match. Will eliminate delay, but offset will depend on compiler settings.
- You can drive both timer with a master timer. Will be the most complex code, but will eliminate the delay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-27 1:23 AM
As @TDK said "You can drive both timer with a master timer."
https://www.youtube.com/watch?v=a1ynzt_RVww
