AnsweredAssumed Answered

stm32f3 can't run 2 timer together??

Question asked by prasetyo.yoyok on May 26, 2015
Latest reply on May 30, 2015 by Clive One
hello i have problem with my timer periph, i want to use 8 channel, so i use tim 3 and tim 4 together, but if i run the code together, it doesn't work, if i run just 1 timer(4 channel) its working correctly.. i have try to tim1 and tim 4 also but it does't work too..

this is my code in initialitation :

void init_pwm_gpio1()
{
     // The Timer 3 and 4 channels 1,2 ,3 and 4
     // To drive the ECSs they could to be connected to other pins if needed.






     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
     
     GPIO_InitTypeDef GPIO_InitStructure;


     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Use the alternative pin functions
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO speed - has nothing to do with the timer timing
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; // Setup pull-up resistors
     GPIO_Init(GPIOC, &GPIO_InitStructure);


     GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_2); // TIM3_CH1
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_2); // TIM3_CH2
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_2); // TIM3_CH3
     GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_2); // TIM3_CH4


//     GPIO_InitTypeDef GPIO_InitStructure;
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Use the alternative pin functions
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO speed - has nothing to do with the timer timing
          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull
          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; // Setup pull-up resistors
          GPIO_Init(GPIOD, &GPIO_InitStructure);


          // Check the alternative function mapping in the CPU doc
          GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_2); // TIM4_CH1
          GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_2); // TIM4_CH2
          GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_2); // TIM4_CH3
          GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_2); // TIM4_CH4
}


int init_pwm1()
{
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE );//  Enable the TIM3 peripherie
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE );//  Enable the TIM4 peripherie
     TIM_OCInitTypeDef  TIM_OCInitStructure;
     //Calculates the timing. This is common for all channels
     int clk = 72e6; // 72MHz -> system core clock. This is default on the stm32f3 discovery
     int tim_freq = 2e6; // in Hz (2MHz) Base frequency of the pwm timer
     int prescaler = ((clk / tim_freq) - 1);


     // Calculate the period for a given pwm frequency
     int pwm_freq = 200; // in Hz
     int pwm_period = tim_freq/pwm_freq;          // 2MHz / 200Hz = 10000
                                                            // For 50Hz we get: 2MHz / 50Hz = 40000
     // Calculate a number of pulses per millisecond.
     // Not used in this rutine but I put it here just as an example
     int ms_pulses = (float)pwm_period / (1000.0/pwm_freq); // for 200Hz we get: 10000 / (1/200 * 1000) = 2000






     //Setup the timing and configure the TIM1 timer
     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
     TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);
     TIM_TimeBaseStructure.TIM_Prescaler = prescaler;
     TIM_TimeBaseStructure.TIM_Period = pwm_period - 1;
     TIM_TimeBaseStructure.TIM_ClockDivision = 1;
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
     // Initialise the timer channels
     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = ms_pulses*2; // preset pulse width 0..pwm_period
     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Pulse polarity
     //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
     // These settings must be applied on the timer 1.
     //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
     //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
     //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;


     // Setup four channels
     // Channel 1
     TIM_OC1Init(TIM3, &TIM_OCInitStructure);
     TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
     // Channel 2
     TIM_OC2Init(TIM3, &TIM_OCInitStructure);
     TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
     // Channel 3
     TIM_OC3Init(TIM3, &TIM_OCInitStructure);
     TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
     // Channel 4
     TIM_OC4Init(TIM3, &TIM_OCInitStructure);
     TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
     TIM_Cmd(TIM4 , ENABLE);
     // The PWM is running now. The pulse width can be set by
     // TIM1->CCR1 = [0..pwm_period] -> 0..100% duty cycle
     //
     // For example:
     // int pulse_width = 3000;
     // TIM1->CCR1 = pulse_width;
     //
     // The firmware offers a API to do this:
     // TIM_SetCompare1(TIM1 , pulse_width); // This is a wrapper for TIM1->CCR1, the same as TIM1->CCR1=pulse_width;


     // Setup the timing and configure the TIM1 timer
     //     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
          //TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
          TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);
          TIM_TimeBaseStructure.TIM_Prescaler = prescaler;
          TIM_TimeBaseStructure.TIM_Period = pwm_period - 1;
          TIM_TimeBaseStructure.TIM_ClockDivision = 1;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
          TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);




     // Initialise the timer channels
          //TIM_OCInitTypeDef  TIM_OCInitStructure;


          TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
          TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;


          TIM_OCInitStructure.TIM_Pulse = ms_pulses*2; // preset pulse width 0..pwm_period
          //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Pulse polarity
          TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
          TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;


          // These settings must be applied on the timer 1.
          //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
          //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
          //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
     // Setup four channels
          // Channel 1
          TIM_OC1Init(TIM4, &TIM_OCInitStructure);
          TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);


          // Channel 2
          TIM_OC2Init(TIM4, &TIM_OCInitStructure);
          TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);


          // Channel 3
          TIM_OC3Init(TIM4, &TIM_OCInitStructure);
          TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);


          // Channel 4
          TIM_OC4Init(TIM4, &TIM_OCInitStructure);
          TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
          // Starup the timer
          //TIM_ARRPreloadConfig(TIM1, DISABLE);
          //TIM_ARRPreloadConfig(TIM4, DISABLE);
          //TIM_CtrlPWMOutputs(TIM4, ENABLE);
          TIM_Cmd(TIM4 , ENABLE);




     return pwm_period;
}

Outcomes