cancel
Showing results for 
Search instead for 
Did you mean: 

synchronize two timers with each other

debug
Associate III

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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.
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

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.
If you feel a post has answered your question, please click "Accept as Solution".