I am a begginer in STM. I have STM32 NUCLEO-F411RE, Pololu 1570 6V 2220RPM DC Motor, L298N DC Motor Driver and 6V 1,3Ah Xtreme Acumulator. I want my motor to be just rotating for example with 80% duty cycle pwm. I have it connected as in this picture:Here is picture of my connection But instead of 2 motors I have one and instead of Arduino I have STM. In my case pin ENA from motor driver is connected to PB6 pin where I set TIM4 with Channel 1 PWM generation. And IN1 pin from motor driver is connected to PA11 and IN2 to PA12. Here is the code which I add by myself to main():

/* USER CODE BEGIN 2 */
  HAL_GPIO_WritePin(GPIOA,Dir1_Pin,GPIO_PIN_SET);   // Start  motor clock wise rotation, Dir1_Pin is PA11 and Dir2_Pin is PA12
  HAL_GPIO_WritePin(GPIOA,Dir2_Pin,GPIO_PIN_RESET);
  HAL_TIM_Base_Start(&htim4);
  HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
     
      htim4.Instance->CCR1=????; //What number should I put here to have my motor rotating with 80% duty cycle?
  }
  /* USER CODE END 3 */
}

I have already been looking for explanations on the internet, make calculations for a week, but didn't find anything which would work for my case. Red light on motor driver is lighting. With some of results from my calculations it was making a "tick tick" sound, but the motor was not rotating. I do not know exactly what values can be send to ENA Pin for my specific DC motor.

1

There are 1 best solutions below

0
On

Duty cycle is just a high-to-low ratio of the PWM clock.

To calculate value of CCR, you have to know PWM length in timer clocks, that is usually set up in ARR, that is mapped to Init.Period structure member:

void PWM_SetDutyCycle(uint16_t dc_percent) {
    // recalculate into pulse width
    uint16_t dc = htim1.Init.Period * ((uint32_t)dc_percent) / ((uint32_t) 100);
    htim4.Instance->CCR1 = dc;
}