cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f3 can't run 2 timer together??

mailkaosku9
Associate II
Posted on May 26, 2015 at 05:58

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;

}

#stm32f3-timer
3 REPLIES 3
Posted on May 26, 2015 at 19:39

TIM_Cmd(TIM4 , ENABLE); //

This won't enable TIM3

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mailkaosku9
Associate II
Posted on May 30, 2015 at 18:04

i've changed that, 

to

TIM_Cmd(TIM3 , ENABLE); 

still not working

i also run timer 1 (4) channels in pwm mode, its working butif i enable other timer to pmw mode, they are not working

Posted on May 30, 2015 at 19:20

A quick blind implementation.

// STM32 TIM3/4 PWM 200 Hz - STM32F3-Discovery - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD, ENABLE);
/* Enable TIM3/4 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// I have not confirmed the availability of these pins
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_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
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/**************************************************************************************/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 36 - 1; // 72 to 2 MHz
TIM_TimeBaseStructure.TIM_Period = 10000 - 1; // 2 MHz to 200 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // Not used
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 10000 / 4; // 25%
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_Pulse = 10000 / 2; // 50%
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM_Configuration();
while(1); // Don't want to exit
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..