cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F429i]PWM problem on few timers/channels

marjano89
Associate II
Posted on June 17, 2014 at 13:45

Hello everyone. I am creating a hexapod with 18 independent servos, using 18 channels on General - Purpose timers. I have problem with few timers, simply servo is not responding to new compare value. I am using timers:

TIM2 - 4 channels working TIM3 - 4 channels working TIM4 - 4 channels working TIM5 - chanel 2 and 3 not working TIM12 - not working Do I have some conflicts with alternative configuration ? My init for timers/pwm:


void initTimer()

{

RCC_TIMCLKPresConfig(RCC_TIMPrescActivated);


/* TIM2 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);


/* TIM3 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);


/* TIM4 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);


/* TIM5 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);


/* TIM12 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE);


TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


/* Compute the prescaler value */

uint16_t u16PrescalerValue = (uint16_t) ((SystemCoreClock) / 1000000)- 1;

TIM_TimeBaseStructure.TIM_Prescaler = u16PrescalerValue; // 1MHz

TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20ms)

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;


TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

TIM_TimeBaseInit(TIM12, &TIM_TimeBaseStructure);

}


void initPWM()

{

/*

* Servo min(1ms) = 1000

* Servo neutral (1,5ms) = 1500

* Servo max(2ms) = 2000

*

*/


TIM_OCInitTypeDef TIM_OCInitStructure;


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = 0;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


/*********************TIM2**************************/

/* PWM1 Mode configuration: Channel1 (GPIOA Pin 15)*/

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOB Pin 3)*/

TIM_OC2Init(TIM2, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel3 (GPIOB Pin 10)*/

TIM_OC3Init(TIM2, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel4 (GPIOB Pin 11)*/

TIM_OC4Init(TIM2, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable );


/*********************TIM3**************************/

/* PWM1 Mode configuration: Channel1 (GPIOB Pin 4)*/

TIM_OC1Init(TIM3, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOB Pin 5)*/

TIM_OC2Init(TIM3, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel3 (GPIOB Pin 0)*/

TIM_OC3Init(TIM3, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel4 (GPIOB Pin 1)*/

TIM_OC4Init(TIM3, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable );


/*********************TIM4**************************/

/* PWM1 Mode configuration: Channel1 (GPIOD Pin 12)*/

TIM_OC1Init(TIM4, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOD Pin 13)*/

TIM_OC2Init(TIM4, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel3 (GPIOD Pin 14)*/

TIM_OC3Init(TIM4, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel4 (GPIOD Pin 15)*/

TIM_OC4Init(TIM4, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable );


/*********************TIM5**************************/

/* PWM1 Mode configuration: Channel1 (GPIOA Pin 0)*/

TIM_OC1Init(TIM5, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOA Pin 1)*/

TIM_OC2Init(TIM5, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel3 (GPIOA Pin 2)*/

TIM_OC3Init(TIM5, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel4 (GPIOA Pin 3)*/

TIM_OC4Init(TIM5, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable );


/*********************TIM12*************************/

/* PWM1 Mode configuration: Channel1 (GPIOB Pin 14)*/

TIM_OC1Init(TIM12, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM12, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOB Pin 15)*/

TIM_OC2Init(TIM12, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM12, TIM_OCPreload_Enable );


/*********************TIM9*************************/

/* PWM1 Mode configuration: Channel1 (GPIOE Pin 5)*/

TIM_OC1Init(TIM9, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable );


/* PWM1 Mode configuration: Channel2 (GPIOE Pin 6)*/

TIM_OC2Init(TIM9, &TIM_OCInitStructure);

TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable );


//enable timers

TIM_Cmd(TIM2, ENABLE);

TIM_Cmd(TIM3, ENABLE);

TIM_Cmd(TIM4, ENABLE);

TIM_Cmd(TIM5, ENABLE);

TIM_Cmd(TIM9, ENABLE);

TIM_Cmd(TIM12, ENABLE);

}


void initServos()

{

GPIO_InitTypeDef GPIO_PWM;


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);


GPIO_PWM.GPIO_Mode = GPIO_Mode_AF;

GPIO_PWM.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_PWM.GPIO_OType = GPIO_OType_PP;

GPIO_PWM.GPIO_PuPd = GPIO_PuPd_UP;


//PORT A

GPIO_PWM.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_15;

GPIO_Init(GPIOA, &GPIO_PWM);


GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5 );

GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5 );

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM5 );

GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5 );

GPIO_PinAFConfig(GPIOA, GPIO_PinSource15,GPIO_AF_TIM2 );


//PORT B

GPIO_PWM.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_14 | GPIO_Pin_15;

GPIO_Init(GPIOB, &GPIO_PWM);


GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_TIM2 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource10,GPIO_AF_TIM2 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource11,GPIO_AF_TIM2 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_TIM12 );

GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_TIM12 );


//PORT D

GPIO_PWM.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

GPIO_Init(GPIOD, &GPIO_PWM);


GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4 );

GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4 );

GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4 );

GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4 );



}

#pwm #stm32f4 #discovery #output
15 REPLIES 15
marjano89
Associate II
Posted on June 18, 2014 at 07:57

It might be this, but how can I get rid of this conflict? This is not enough?:4

///////////////GPIO CONFIGURATION///////////////
GPIO_InitTypeDef GPIO_PWM;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PWM.GPIO_Mode = GPIO_Mode_AF;
GPIO_PWM.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_PWM.GPIO_OType = GPIO_OType_PP;
GPIO_PWM.GPIO_PuPd = GPIO_PuPd_UP;
//PORT A
GPIO_PWM.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_PWM);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5 );
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5 );
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM5 );
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5 );

Posted on June 18, 2014 at 13:21

It might be this, but how can I get rid of this conflict? This is not enough?

Think about this for a second, how does reconfiguring the STM32 change the behaviour of pins driven by external devices?

Your options sometimes come down to removing devices or cutting traces.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
marjano89
Associate II
Posted on June 19, 2014 at 11:14

So I can't use pins which can used as  TIM5 even if in datasheet there is info that PA1 PA2 are alternatively TIM5?

marjano89
Associate II
Posted on June 19, 2014 at 14:45

Could you please help me with TIM5? I really need this timer working

Posted on June 19, 2014 at 16:24

Could you please help me with TIM5? I really need this timer working

Look I don't have magic powers, does it work if you remove the other part?

Can you use some other pins/timers to get what you need?

TIM1_CH1 PA8, TIM1_CH2 PA9, TIM1_CH3 PA10, TIM1_CH4 PA11
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 19, 2014 at 16:28

So I can't use pins which can used as  TIM5 even if in datasheet there is info that PA1 PA2 are alternatively TIM5?

YOU HAVE AN EXTERNAL DEVICE DRIVING VOLTAGE INTO THE PINS, REMOVE U3 AND TRY YOUR EXPERIMENT AGAIN
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..