cancel
Showing results for 
Search instead for 
Did you mean: 

Sync PWM timers together

Posted on March 09, 2016 at 12:23

Hello there,

I am trying to write a stepper motor driver on STM32F4 platform. I intend to use TIM1 (channel 1, 2 and 3) and TIM8 (channel 1) for driving the motor. I have noticed that after configuration the timers for center aligned PWM, the 1 channel from TIM8 is not in sync with other channels from TIM1. The pwm waves should be set at same position, but the wave from tim8 is with offset, not center aligned with channel 3, 2 or 1 from TIM1.

I need to make TIM1 and TIM8 to work exacly in sync. Is that possible? I would really apreciate all help regarding this matter.

EDIT: I have noticed that the offset is depending on how late i start TIM8 after TIM1. Is there a way for them to start at the same time?
6 REPLIES 6
Posted on March 09, 2016 at 13:06

TIM8 can be slave of TIM1. Refer to RM's sub-chapter ''Using one timer to start another timer''.

[EDIT] or, maybe even better, the next one, ''Starting 2 timers synchronously in response to an external trigger''

JW

Posted on March 09, 2016 at 13:19

Yes, I am at the moment experimenting with that. I have set TIM8 to be a slave of TIM1. The problem is I cant seem to get the timers to start at exacly same moment. the slave timer starts either after TIM1 finished counting up or down. Would you please help me out in the configuration? At the moment this is the tim 1 and 8 config:

void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
TIM_OC_InitTypeDef sConfigOC;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim1);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim1);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC3REF;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 100;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
}
void MX_TIM8_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_SlaveConfigTypeDef sSlaveConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
TIM_OC_InitTypeDef sConfigOC;
htim8.Instance = TIM8;
htim8.Init.Prescaler = 0;
htim8.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim8.Init.Period = 0xFFFF;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim8);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim8);
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_ITR0;
HAL_TIM_SlaveConfigSynchronization(&htim8, &sSlaveConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 100;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_1);
}

Posted on March 09, 2016 at 13:28

Yes thats the case. Thank you very much.

Walid FTITI_O
Senior II
Posted on March 09, 2016 at 15:09

Hi Bremen, 

You can use a master timer to control the start/stop of both TIM1 and TIM8 which you should configure as slave timers.

Take a look to the ''TIM_Synchronization'' example in

http://www.st.com/web/en/catalog/tools/PF259243

 at this path :

STM32Cube_FW_F4_V1.11.0\Projects\STM324xG_EVAL\Examples\TIM

-Hannibal-

Posted on March 09, 2016 at 15:31

I have configured TIM1 as master. TRGO as enable. TIM8 as slave, TR0 selected, trigger mode. I then start both timers manually and they are in sync, i could probably only start timer 1.

Walid FTITI_O
Senior II
Posted on March 09, 2016 at 18:21

Hi Bremen, 

That's good. thanks for your contribution.

-Hannibal-