AnsweredAssumed Answered

How to generate dutycycle for ESC controler using STM32F4

Question asked by anton.bogdan on Feb 12, 2015
Latest reply on Feb 19, 2015 by Chris T
I have some brushless simonk ESC`s driving my brushless motors.
I noticed that these esc`s are woring on a 1 to 2 ms pulse from a 50hz signal.
Wich in duty cycle terms means that i must work with dutycycle range from 1% to maximum 2%.
Can these 1,2% terms to be translated in a numerical value of 1000-2000 units ?
How to set for example 1.5%, some functions do not alow floating  numbers inputs




I have my pwm initialization

GPIO_InitTypeDef GPIO_InitStruct; 
   
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
   
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3); 
 
 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_BaseStruct.TIM_Prescaler =16000;
TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseStruct.TIM_Period = 100; /* 51Hz PWM */
TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_BaseStruct);
TIM_Cmd(TIM3, ENABLE);
 
 
TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
 
 
TIM_OCStruct.TIM_Pulse = 0;
    TIM_OC1Init(TIM3, &TIM_OCStruct);
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
TIM_OCStruct.TIM_Pulse = 0;
    TIM_OC2Init(TIM3, &TIM_OCStruct);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
TIM_OCStruct.TIM_Pulse = 0;
    TIM_OC3Init(TIM3, &TIM_OCStruct);
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
TIM_OCStruct.TIM_Pulse = 0;
    TIM_OC4Init(TIM3, &TIM_OCStruct);
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

And my set dutycycle routine

void set_pwm(uint8_t channel, uint8_t duty){
  if(channel==1){
  TIM3->CCR1 = duty;
  } else if(channel==2){
   TIM3->CCR2 = duty;
  } else if(channel==3){
    TIM3->CCR3 = duty;
  }else if(channel==4){
   TIM3->CCR4 = duty;
  }
   
   
}


Outcomes