2012-08-30 11:28 AM
Hi Please help me with synching 4 timers
I need 4 PWM from 4 different timers to sync it's output so that i get this result: ________________ ________________ _| |____________| |___________ TIM9_CH1 ________________ ________________ _| |____________| |___________ TIM1_CH1 ________________ ________________ _| |____________| |___________ TIM8_CH1 ________________ ________________ _| |____________| |___________ TIM10_CH1I have tried to look in the examples but can not find on suitable, i need the PWM to run at 70kHz.My setup is as follows RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM9 | RCC_APB2Periph_TIM8 | RCC_APB2Periph_TIM10 , ENABLE); /* ============================== TIMER 1(PWM) ============================== */ /*PWM GPIO Configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_8; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1710; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(PWMTIMER1, &TIM_TimeBaseStructure); TIM_TimeBaseInit(PWMTIMER2, &TIM_TimeBaseStructure); TIM_TimeBaseInit(PWMTIMER3, &TIM_TimeBaseStructure); TIM_TimeBaseInit(PWMTIMER4, &TIM_TimeBaseStructure); //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_; //TIM_TimeBaseInit(TIM11, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel 1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(PWMTIMER1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(PWMTIMER1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel 2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(PWMTIMER2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(PWMTIMER2, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel 3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(PWMTIMER3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(PWMTIMER3, TIM_OCPreload_Enable); // /* PWM1 Mode configuration: Channel 4*/ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(PWMTIMER4, &TIM_OCInitStructure); TIM_OC1PreloadConfig(PWMTIMER4, TIM_OCPreload_Enable); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM9); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM10); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); /* Enable Auto Reload Register Preload */ TIM_ARRPreloadConfig(PWMTIMER1, ENABLE); TIM_CtrlPWMOutputs(PWMTIMER1, ENABLE); /* Enable Auto Reload Register Preload */ TIM_ARRPreloadConfig(PWMTIMER2, ENABLE); TIM_CtrlPWMOutputs(PWMTIMER2, ENABLE); /* Enable Auto Reload Register Preload */ TIM_ARRPreloadConfig(PWMTIMER3, ENABLE); TIM_CtrlPWMOutputs(PWMTIMER3, ENABLE); /* Enable Auto Reload Register Preload */ TIM_ARRPreloadConfig(PWMTIMER4, ENABLE); TIM_CtrlPWMOutputs(PWMTIMER4, ENABLE); TIM_Cmd(PWMTIMER1, ENABLE); TIM_Cmd(PWMTIMER2, ENABLE); TIM_Cmd(PWMTIMER3, ENABLE); TIM_Cmd(PWMTIMER4, ENABLE); #stm32-timer-sync-pwm2012-08-30 12:37 PM
Stupid question, but why wouldn't you use the 4 channels of the same timer if you have the same period? This is how one would normally drive things like servos.
Also for 70 KHz you might want to run the CPU at some thing other than 120 MHz, say 105 MHz2012-08-30 01:05 PM
To synchronize you'd probably want to use Slave mode reset. I think you've got a poorly chosen set of timers to make that work internally, you might be able to route externally using the CH2 pins attached to a common source.
Pay particular attention to the ''TIMx internal trigger connection'' tables.http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/REFERENCE_MANUAL/CD002257pdf
/* Select the TIM4 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM4, ENABLE);
TIM_TS_ITR0, TIM_TS_ITR1, TIM_TS_ITR2, TIM_TS_ITR3 could be used in place of TIM_TS_TI2FP2