[STM32] cubeMX configures HAL library to drive L298N to control DC brushed motor

Table of contents

1. Why does the motor need a driver board? Why can't the controller directly control the motor?

2. H-bridge circuit introduction

3. Introduction to the principle of L298N

4. Physical wiring diagram

5.CUBEMX configuration

6. Code

7. Demonstration video


L298N is a motor driver board commonly used to drive small DC brushed motors (two) and stepper motors (one). Its circuit principle is two H-bridge circuits. The control principle is to use PWM to realize the speed regulation of the DC motor. Therefore, it is necessary to supplement the knowledge of PWM first, and you can jump to the following link.

【STM32】PWM output function detailed introduction

[STM32] HAL library PWM realizes breathing light experiment

1. Why does the motor need a driver board? Why can't the controller directly control the motor?

The reason is that the voltage provided by the controller itself is not enough for the motor to move, so an external drive board is needed to drive the motor to move.

The schematic diagram of using STM32 as the controller and L298N as the driver to drive a small motor (DC brushed motor) is as follows:

2. H-bridge circuit introduction

For the control circuit that needs to drive a DC motor with a large current load, the H-bridge is the most classic, because its circuit shape is like the letter "H", so it is called "H-bridge". The H-bridge generally consists of four MOS transistors (Q1, Q2, Q3, Q4) as independently controlled switching components. The motor states corresponding to different switch states are shown in the following table:

motor status

switch status

Forward

Q1, Q4 on, Q2, Q3 off

reverse

Q1, Q4 off, Q2, Q3 on

stop

Q1, Q3 off, Q2, Q4 on

Usually the most commonly used control of the H-bridge is pulse width modulation (PWM).

3. Introduction to the principle of L298N

The internal circuit diagram of L298N is shown in the figure below:

It is not difficult to see that its internal circuit is composed of two H-bridges. ENA and ENB are enable signals, In1 and In2 are a group of logic signals that control the group of output signals OUT1 and OUT2; In3 and In4 are a group of logic signals that control the group of output signals of OUT3 and OUT4. Control the output signal by controlling the logic signal, such as motor speed and forward and reverse.

(1) Output A and output B are used to connect two brushed DC motors, and the logic input should be connected to the control board. In1 and In2 control output A, and In3 and In4 control output B. The jumper cap of the channel enable is inserted on the top by default and remains unchanged. If unplugged, it is considered disabled.

(2) For the jumper cap at the onboard 5V enabling position, if it is plugged in, the 5V power supply can be used to supply power to the control board; if it is unplugged, it can be used to provide 5V power supply to the driver board.

(3) Power supply problem: When the input power is 7~12V, the 5V power supply can be connected to the control board to supply power to the control board, or not; when the input power is greater than 12V, you need to unplug the jumper cap next to the power supply (Onboard 5V is enabled), the 5V power supply needs to be connected to 5V voltage.

4. Physical wiring diagram

Through the analysis of the principle of L298N, it is not difficult to find that the logic signal is connected to the control board, and the output signal is connected to the motor. Note: The control board and the driver board should achieve a common ground, the purpose is to make the voltage have a reference level.

Use two 3.7V batteries for power supply, the positive pole of the battery is connected to +12V for power supply, and the negative pole is connected to GND. The control board used is the explorer of punctual atom, and the main control chip is STM32F407ZGT6. Because one motor is used, only one set of control signals is needed to control one output. Therefore, In1 and In2 are connected to the PA0 and PA6 pins of the control board, and OUT1 is connected to the two wires of the motor. Because the control board has its own power supply, there is no need for the control board to provide 5V power supply, so it is only necessary to connect the GND of the control board and the driver board together to give it a reference level.

Control issues:

motor status

IN1

IN2

Forward

1

0

reverse

0

1

stop

wait

0

0

brake

1

1

5.CUBEMX configuration

(1) Enable external high-speed clock

(2) Complete the clock tree configuration

(3) Set PWM (you can also open two channels in TIM2)

(4) Generate code (GENERATE CODE)

6. Code

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {		
	__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,5000);
	HAL_Delay(2000);
	__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,0);
	HAL_Delay(1000);//慢速正转2秒,等待1秒
	__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,20000);
	HAL_Delay(2000);
	__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,0);
	HAL_Delay(1000);//快速反转2秒,等待1秒
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

7. Demonstration video

STM32 controls L298N to drive small motor

Guess you like

Origin blog.csdn.net/weixin_45015121/article/details/130663353