跳转至

rocketpi_pwm_motor

效果展示

motor

功能说明

  • TIM4 通道 1/2 输出 PWM 驱动 L9110:CH1→INB(PB6),CH2→INA(PB7),默认 20 kHz,可在 tim.c 调整。
  • 刹车式 PWM:低电平阶段 IA=0、IB=0,低速力矩大、响应快。
  • 驱动接口:motor_l9110_initmotor_l9110_drive(方向+占空比 0-100%)、motor_l9110_drive_signed(有符号占空比 ±100%)、motor_l9110_brake(双低刹车)。
  • 演示状态机:motor_l9110_test_init/motor_l9110_test_task 循环慢速→加速→刹车→反转→加速→刹车,可调 hold_ms 改各阶段时长。

硬件连接

image-20251216235933477

image-20251217000100719

原理

IA / IB 的逻辑关系(数据手册真值表)

  • IA=H, IB=L → OA=H, OB=L(一个方向)
  • IA=L, IB=H → OA=L, OB=H(反方向)
  • IA=L, IB=L → OA=L, OB=L(刹车到地)
  • IA=H, IB=H → OA=L, OB=L(同样刹车)

没有“输出高电平滑行(coast)”,停时两端被拉低,更像动态刹车。

推荐的 PWM 调速接法(最简单、最安全)

  • 正转调速IB = 0 固定,IA = PWM(duty)
  • 反转调速IA = 0 固定,IB = PWM(duty)
  • 停/刹车IA = 0, IB = 0(或都 1 也会停,但推荐双低)

特点:PWM 的低电平阶段落在 IA=0、IB=0 → OA/OB 都为 0,即 PWM 刹车式调速(非滑行式)。

PWM 频率

  • 默认配置约 20 kHz,可根据电机啸叫与发热情况在 10–20 kHz 之间调整。

驱动以及测试代码

Core/Src/main.c
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "driver_motor_l9110_test.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_USART2_UART_Init();
  MX_TIM4_Init();
  /* USER CODE BEGIN 2 */
  motor_l9110_test_init();
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
    motor_l9110_test_task();
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 84;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Core/Src/stm32f4xx_it.c
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
   while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f4xx.s).                    */
/******************************************************************************/

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
bsp/motor_l9110/driver_motor_l9110.c
/**
 * @file driver_motor_l9110.c
 * @brief PWM control helpers for the L9110 motor bridge.
 *
 * Recommended PWM (brake style):
 * - Forward: INB low (TIM4 CH1), INA = PWM (TIM4 CH2)
 * - Reverse: INA low, INB = PWM
 * - Brake:   INA = 0, INB = 0
 * 正转 INB=0、INA=PWM;反转 INA=0、INB=PWM;刹车双低。
 */

#include "driver_motor_l9110.h"

#define MOTOR_L9110_TIMER       (&htim4)
#define MOTOR_L9110_CHANNEL_IB  TIM_CHANNEL_1
#define MOTOR_L9110_CHANNEL_IA  TIM_CHANNEL_2

/* 将 0-100% 占空比换算为 CCR 脉宽 */
static uint32_t motor_l9110_calc_pulse(uint8_t duty_percent)
{
    uint32_t period = MOTOR_L9110_TIMER->Init.Period;
    uint32_t pulse = (period + 1U) * duty_percent / 100U;

    if (pulse > period)
    {
        pulse = period;
    }

    return pulse;
}

/* 同步更新两路,避免瞬间交叉导通 */
static void motor_l9110_apply(uint32_t ia_pulse, uint32_t ib_pulse)
{
    __HAL_TIM_SET_COMPARE(MOTOR_L9110_TIMER, MOTOR_L9110_CHANNEL_IA, ia_pulse);
    __HAL_TIM_SET_COMPARE(MOTOR_L9110_TIMER, MOTOR_L9110_CHANNEL_IB, ib_pulse);
}

/* 初始化 PWM 输出并默认刹车,防止上电误转 */
void motor_l9110_init(void)
{
    HAL_TIM_PWM_Start(MOTOR_L9110_TIMER, MOTOR_L9110_CHANNEL_IA);
    HAL_TIM_PWM_Start(MOTOR_L9110_TIMER, MOTOR_L9110_CHANNEL_IB);
    motor_l9110_brake();
}

/* 方向 + 占空比控制(0-100%),低电平阶段为刹车
 * @param direction 正转/反转/刹车
 * @param duty_percent 占空比,数值越大转速越高
 */
void motor_l9110_drive(motor_l9110_direction_t direction, uint8_t duty_percent)
{
    if (duty_percent > 100U)
    {
        duty_percent = 100U;
    }

    uint32_t pulse = motor_l9110_calc_pulse(duty_percent);

    switch (direction)
    {
    case MOTOR_L9110_DIR_FORWARD:
        /* INA = PWM, INB = 0 */
        motor_l9110_apply(pulse, 0U);
        break;
    case MOTOR_L9110_DIR_REVERSE:
        /* INA = 0, INB = PWM */
        motor_l9110_apply(0U, pulse);
        break;
    case MOTOR_L9110_DIR_BRAKE:
    default:
        motor_l9110_apply(0U, 0U);
        break;
    }
}

/* 有符号占空比控制:正为正转,负为反转
 * @param duty_percent -100~100,占空比正负代表方向
 */
void motor_l9110_drive_signed(int8_t duty_percent)
{
    int16_t duty = duty_percent;

    if (duty > 100)
    {
        duty = 100;
    }
    else if (duty < -100)
    {
        duty = -100;
    }

    if (duty >= 0)
    {
        motor_l9110_drive(MOTOR_L9110_DIR_FORWARD, (uint8_t)duty);
    }
    else
    {
        motor_l9110_drive(MOTOR_L9110_DIR_REVERSE, (uint8_t)(-duty));
    }
}

/* 双低刹车,快速停转 */
void motor_l9110_brake(void)
{
    motor_l9110_apply(0U, 0U);
}
bsp/motor_l9110/driver_motor_l9110.h
/**
 * @file driver_motor_l9110.h
 * @brief Simple PWM-based driver for the L9110 dual-channel motor bridge.
 *
 * TIM4 CH1 -> INB, TIM4 CH2 -> INA (10 kHz PWM as configured in CubeMX).
 * 使用 TIM4 通道 1/2 分别驱动 INB/INA,10 kHz PWM 刹车式调速。
 */

#ifndef DRIVER_MOTOR_L9110_H
#define DRIVER_MOTOR_L9110_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include "tim.h"

typedef enum
{
    MOTOR_L9110_DIR_BRAKE = 0,
    MOTOR_L9110_DIR_FORWARD,
    MOTOR_L9110_DIR_REVERSE
} motor_l9110_direction_t;

/* 初始化 PWM 输出并默认刹车 */
void motor_l9110_init(void);

/* 指定方向和占空比(0-100%),低电平阶段等效刹车
 * @param direction 运动方向(正/反/刹车)
 * @param duty_percent 占空比 0-100%,数值越大越快
 */
void motor_l9110_drive(motor_l9110_direction_t direction, uint8_t duty_percent);

/* 使用有符号占空比:正为正转,负为反转
 * @param duty_percent -100~100,占空比正负代表方向
 */
void motor_l9110_drive_signed(int8_t duty_percent);

/* 立即刹车(双低) */
void motor_l9110_brake(void);

#ifdef __cplusplus
}
#endif

#endif /* DRIVER_MOTOR_L9110_H */
bsp/motor_l9110/driver_motor_l9110_test.c
/**
 * @file driver_motor_l9110_test.c
 * @brief Simple demo sequence that exercises forward, brake, and reverse.
 * 演示正转->加速->刹车->反转->加速->刹车的循环。
 */

#include "driver_motor_l9110_test.h"
#include "driver_motor_l9110.h"
#include "main.h"

typedef enum
{
    MOTOR_L9110_TEST_FORWARD_SOFT = 0,  /* 正转慢速 */
    MOTOR_L9110_TEST_FORWARD_FAST,      /* 正转加速 */
    MOTOR_L9110_TEST_BRAKE_FROM_FWD,    /* 正转后刹车 */
    MOTOR_L9110_TEST_REVERSE_SOFT,      /* 反转慢速 */
    MOTOR_L9110_TEST_REVERSE_FAST,      /* 反转加速 */
    MOTOR_L9110_TEST_BRAKE_FROM_REV     /* 反转后刹车 */
} motor_l9110_test_state_t;

static motor_l9110_test_state_t s_state = MOTOR_L9110_TEST_FORWARD_SOFT;
static uint32_t s_state_tick = 0;
static uint32_t s_state_hold_ms = 0;

/* 切换到新状态
 * @param next_state 目标状态
 * @param hold_ms    该状态保持的时间(毫秒)
 */
static void motor_l9110_test_enter(motor_l9110_test_state_t next_state, uint32_t hold_ms)
{
    /* 进入新状态并记录起始时间 */
    s_state = next_state;
    s_state_hold_ms = hold_ms;
    s_state_tick = HAL_GetTick();

    switch (next_state)
    {
    case MOTOR_L9110_TEST_FORWARD_SOFT:
        motor_l9110_drive(MOTOR_L9110_DIR_FORWARD, 50);
        break;
    case MOTOR_L9110_TEST_FORWARD_FAST:
        motor_l9110_drive(MOTOR_L9110_DIR_FORWARD, 100);
        break;
    case MOTOR_L9110_TEST_BRAKE_FROM_FWD:
        motor_l9110_brake();
        break;
    case MOTOR_L9110_TEST_REVERSE_SOFT:
        motor_l9110_drive(MOTOR_L9110_DIR_REVERSE, 50);
        break;
    case MOTOR_L9110_TEST_REVERSE_FAST:
        motor_l9110_drive(MOTOR_L9110_DIR_REVERSE, 100);
        break;
    case MOTOR_L9110_TEST_BRAKE_FROM_REV:
    default:
        motor_l9110_brake();
        break;
    }
}

void motor_l9110_test_init(void)
{
    /* 初始化驱动后进入初始状态 */
    motor_l9110_init();
    motor_l9110_test_enter(MOTOR_L9110_TEST_FORWARD_SOFT, 2000);
}

void motor_l9110_test_task(void)
{
    uint32_t now = HAL_GetTick();

    /* 未到驻留时间则直接返回(非阻塞) */
    if ((now - s_state_tick) < s_state_hold_ms)
    {
        return;
    }

    switch (s_state)
    {
    case MOTOR_L9110_TEST_FORWARD_SOFT:
        motor_l9110_test_enter(MOTOR_L9110_TEST_FORWARD_FAST, 2000);
        break;
    case MOTOR_L9110_TEST_FORWARD_FAST:
        motor_l9110_test_enter(MOTOR_L9110_TEST_BRAKE_FROM_FWD, 1000);
        break;
    case MOTOR_L9110_TEST_BRAKE_FROM_FWD:
        motor_l9110_test_enter(MOTOR_L9110_TEST_REVERSE_SOFT, 2000);
        break;
    case MOTOR_L9110_TEST_REVERSE_SOFT:
        motor_l9110_test_enter(MOTOR_L9110_TEST_REVERSE_FAST, 2000);
        break;
    case MOTOR_L9110_TEST_REVERSE_FAST:
        motor_l9110_test_enter(MOTOR_L9110_TEST_BRAKE_FROM_REV, 1000);
        break;
    case MOTOR_L9110_TEST_BRAKE_FROM_REV:
    default:
        motor_l9110_test_enter(MOTOR_L9110_TEST_FORWARD_SOFT, 2000);
        break;
    }
}
bsp/motor_l9110/driver_motor_l9110_test.h
/**
 * @file driver_motor_l9110_test.h
 * @brief Non-blocking demo sequence for the L9110 driver.
 *  L9110 驱动的简单演示状态机(非阻塞)。
 */

#ifndef DRIVER_MOTOR_L9110_TEST_H
#define DRIVER_MOTOR_L9110_TEST_H

#ifdef __cplusplus
extern "C" {
#endif

void motor_l9110_test_init(void);
/* 放在主循环中定期调用,驱动状态机前进 */
void motor_l9110_test_task(void);

#ifdef __cplusplus
}
#endif

#endif /* DRIVER_MOTOR_L9110_TEST_H */