AnsweredAssumed Answered

STM32F0308 TIM15 PWM

Question asked by Lesueur.Rick on Jan 11, 2017
Latest reply on Jan 12, 2017 by waclawek.jan

I am really stuck on this portion of code..

 

Attempting to run to 12v DC motors from STM32F0308  PINS PA2 = Channel 1 PA3 = Channel 2 of TIMER TIM15.

 

I want to be able to do the following for each output - TURN on or OFF, and change PWM timing (OK if timing is the same for each channel)

 

PA2 & PA3 are each tied low with 4.7k OHM resistor to ground. 

 

Below is the code for the PA2.  TIM15 CHANNEL1

 

I "think" this has something do do with TIMER15 the provided driver indicates both AF0 and AF3. 

Additionally, the reference manual makes note of special circumstances for TIMER15.. (I can't find any example code for this timer) I know the easier answer is to move to a different timer/ pins.. but that is a separate issue....

 

 

 

void MotorPWM_Init() { // This is for PA2 TIM15 Channel 1

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  uint16_t TimerPeriod = 0;
  uint16_t ChannelPulse = 0;

  GPIO_InitTypeDef GPIO_InitStructure;

  / / GPIOA Clocks Enable
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 
// GPIO Configuration Channel as alternate function
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;// Works if OUT not AF !!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, & GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_3); // Have tried AF0.. AF3

 

// TIM15 Clock ENABLE
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);

 

// TIM15 Configuration
// Generate PWM Signals with specified Duty Cycles

 

// TIM15 input clock is set to APB2 clock

TimerPeriod = (SystemCoreClock/ 17570) - 1;

// Formula for Duty Cycle
// Duty Cycle = (uint16_t) (((uint32_t) [DUTYCYCLE_VALUE (0-to 1000)]
// (TimerPeriod -1))/1000)
// Note Value is OFF time, not ON time... eg, 125 is off 125 of 1000 cycles
// DUTY CYCLE is never utilized in code or init.. ?? 


ChannelPulse = (uint16_t) (((uint32_t) TESTVALUE * (TimerPeriod -1))/1000);

  // NOTE TESTVALUE toggles between values of  0 and 1000. 

// TIM15 clock enable //
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);

// Time Base Configuration
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);

// Channel 1 Configuration in PWM Mode

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = ChannelPulse;
TIM_OC1Init(TIM15, &TIM_OCInitStructure);

// TIM15 Counter Enable
TIM_Cmd(TIM15, ENABLE);

// TIM Main Output Enable
TIM_CtrlPWMOutputs(TIM15, ENABLE);

}

Outcomes