cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f303 Output Compare

arganasss
Associate II
Posted on May 09, 2014 at 15:48

Hello, i'd like to do output compare on four pins: PE13, PE9 (LEDS), PA10 and PA12. Everyone works except PA12. I tried to do that with TIM16 and TIM4 with no luck. And i reconfigured the channels, i understand how they are mapped to pins (datasheet). So maybe someone knows why this happens? Here is the code. Thanks for your help.

#include ''stm32f30x.h''

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

void Leds_config(void);

void Timer_config(void);

int main(void)

{

  // Enable clocks for E  and A ports

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  

  Leds_config();

  Timer_config();

    

  while(1)

  {

  }

}

void Leds_config(void)

{

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_13; // use 9 and 10 pins port E

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // use alternate-function mode

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // High speed mode

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull mode

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // don't use pull up resistors

  GPIO_Init(GPIOE, &GPIO_InitStructure); // write values to E port registers

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2); // Connect timer1 to these IO pins

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_2); 

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // use 9 and 10 pins port E

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // pin is in output mode

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // High speed mode

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull mode

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // don't use pull up resistors

  GPIO_Init(GPIOA, &GPIO_InitStructure); // write values to E port

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_10); 

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // 12 pin of PA.

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // High speed mode

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull mode

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // don't use pull up resistors

  GPIO_Init(GPIOA, &GPIO_InitStructure); // write values to A port

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_10); 

}

void Timer_config(void)

{

  TIM_TimeBaseInitStructure.TIM_Period = 500-1; // Pulse period = 500us.

  TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; // Divide timer frequency 72Mhz/72Hz gives 1us ticks.

  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // Count from zero to ARR value.

  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // Dont divide timer clock

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);

  

  TIM_TimeBaseInitStructure.TIM_Period = 500-1; // Pulse period = 500us.

  TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; // Divide timer frequency 72Mhz/72Hz gives 1us ticks.

  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // Count from zero to ARR value.

  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // Dont divide timer clock

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);

  

  TIM_TimeBaseInitStructure.TIM_Period = 500-1; // Pulse period = 500us.

  TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; // Divide timer frequency 72Mhz/72Hz gives 1us ticks.

  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // Count from zero to ARR value.

  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; // Dont divide timer clock

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);

  

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 350;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable );

  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 20;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  

  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable );

  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 350;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC4Init(TIM2, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable );

  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 150;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC2Init(TIM4, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable );

  

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

  TIM_CtrlPWMOutputs(TIM2, ENABLE);

  TIM_CtrlPWMOutputs(TIM4, ENABLE);

  TIM_Cmd(TIM1, ENABLE );

  TIM_Cmd(TIM2, ENABLE );

  TIM_Cmd(TIM4, ENABLE);

}

3 REPLIES 3
Posted on May 09, 2014 at 16:00

For the STM32F3-DISCO board observe that solder bridges SB21,22 need to be MADE for the signals PA11 & PA12 to reach the P2 header.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arganasss
Associate II
Posted on May 09, 2014 at 16:03

Also tried to do this on some random pins on B port and it also was successfull, so maybe PA12 or PA11 interacts with something which is not allowed or acceptable?

arganasss
Associate II
Posted on May 09, 2014 at 16:06

Heh. My lack of experience of not knowing where to look. I was concentrating on the program so much that i didn't even think about hardware. Thanks Clive, now it works.