感谢Digikey和EEPW联合举办的Let's do 2026年第3期基于边缘AI的电机状态检测活动。本次活动我选用的器件包括NUCLEO-H533RE、LIS3DH 加速计传感器评估板、电机驱动模块,如下图所示。

本次活动需要实现的功能包括电机驱动、加速计传感器获取三轴的加速度参数,以及最终通过NanoEdgeAI判断电机运行状态。本文将实现电机驱动的相关功能。
根据电机驱动板接线图,可以看到需要有4个输入信号用于确定电机状态,这4个信号可以组成8种通电线序,实现电机旋转

在STM32CubeMX中选取开发板NUCLEO-H533RE,并选取了开发板上连续的4个引脚作为向电机的信号输出


设计电机控制代码如下,其核心原理为定时器提供时间基准,IN1-IN4依次导通控制电机旋转
/**
******************************************************************************
* @file step_motor.c
* @brief Step motor state machine implementation.
******************************************************************************
* Non-blocking design:
* - StepMotor_Tick() runs every iteration of the main loop. It compares
* HAL_GetTick() against last_step_tick to decide whether to advance the
* phase index, and against last_led_tick to decide whether to toggle the
* status LED.
* - StepMotor_ButtonHandler() records debounced button events in EXTI13;
* the main loop processes them in StepMotor_Tick().
******************************************************************************
*/
#include "step_motor.h"
/* Private constants ---------------------------------------------------------*/
/* 8-step half-step sequence, HIGH-active. Column order: IN1, IN2, IN3, IN4. */
static const uint8_t halfstep_seq[8][4] = {
{1, 0, 0, 0}, /* step 0: A */
{1, 1, 0, 0}, /* step 1: A + B */
{0, 1, 0, 0}, /* step 2: B */
{0, 1, 1, 0}, /* step 3: B + C */
{0, 0, 1, 0}, /* step 4: C */
{0, 0, 1, 1}, /* step 5: C + D */
{0, 0, 0, 1}, /* step 6: D */
{1, 0, 0, 1} /* step 7: D + A */
};
/* Step interval (ms) per state. Index 0 (STOP) is unused. */
static const uint16_t step_interval_ms[4] = {0, 3, 2, 1};
/* LED blink half-period (ms) per state. 0 means "solid on"; STOP forces off
* inside Tick regardless of table value. */
static const uint16_t led_blink_period_ms[4] = {0, 0, 500, 150};
/* Debounce window for the user button (ms). */
#define BUTTON_DEBOUNCE_MS 200U
/* Private variables ---------------------------------------------------------*/
static StepMotor_State_t motor_state = STATE_STOP;
static uint8_t phase_index = 0;
static uint32_t last_step_tick = 0;
static uint32_t last_led_tick = 0;
static volatile uint8_t button_pending = 0;
static uint32_t last_btn_tick = 0;
/* Private function prototypes -----------------------------------------------*/
static void apply_phase(uint8_t i);
static void coils_off(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Write IN1..IN4 according to the half-step sequence row i.
* @param i Phase index in [0, 7].
*/
static void apply_phase(uint8_t i)
{
GPIO_PinState s;
s = (halfstep_seq[i][0] != 0) ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(IN1_GPIO_Port, IN1_Pin, s);
s = (halfstep_seq[i][1] != 0) ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(IN2_GPIO_Port, IN2_Pin, s);
s = (halfstep_seq[i][2] != 0) ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(IN3_GPIO_Port, IN3_Pin, s);
s = (halfstep_seq[i][3] != 0) ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(IN4_GPIO_Port, IN4_Pin, s);
}
/**
* @brief De-energize all four coils.
*/
static void coils_off(void)
{
HAL_GPIO_WritePin(IN1_GPIO_Port, IN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(IN2_GPIO_Port, IN2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(IN3_GPIO_Port, IN3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(IN4_GPIO_Port, IN4_Pin, GPIO_PIN_RESET);
}
/* Exported functions --------------------------------------------------------*/
/**
* @brief Initialize the controller. Coils off, LED off, state = STOP.
*/
void StepMotor_Init(void)
{
motor_state = STATE_STOP;
phase_index = 0;
last_step_tick = HAL_GetTick();
last_btn_tick = HAL_GetTick() - BUTTON_DEBOUNCE_MS;
last_led_tick = HAL_GetTick();
button_pending = 0;
coils_off();
BSP_LED_Off(LED_GREEN);
}
/**
* @brief Periodic update — advances the phase and toggles the LED.
* Designed to be called from the main while(1) loop on every pass.
*/
void StepMotor_Tick(void)
{
uint32_t now = HAL_GetTick();
uint32_t primask = __get_PRIMASK();
uint8_t pressed;
StepMotor_State_t st;
__disable_irq();
pressed = button_pending;
button_pending = 0;
__set_PRIMASK(primask);
if (pressed != 0U) {
motor_state = (StepMotor_State_t)(((uint32_t)motor_state + 1U) & 0x03U);
if (motor_state == STATE_STOP) {
coils_off();
} else {
phase_index = 0;
last_step_tick = now;
last_led_tick = now;
}
BSP_LED_Off(LED_GREEN);
}
st = motor_state;
/* --- LED heartbeat --------------------------------------------------- */
if (st == STATE_STOP) {
BSP_LED_Off(LED_GREEN);
} else if (st == STATE_RUN_SLOW) {
BSP_LED_On(LED_GREEN);
} else {
/* RUN_MED / RUN_FAST: blink at led_blink_period_ms[st] half-period. */
uint16_t period = led_blink_period_ms[st];
if (period > 0 && (uint32_t)(now - last_led_tick) >= period) {
last_led_tick = now;
BSP_LED_Toggle(LED_GREEN);
}
}
/* --- Stepping (only in RUN states) ---------------------------------- */
if (st == STATE_STOP) {
return;
}
uint16_t interval = step_interval_ms[st];
if ((uint32_t)(now - last_step_tick) >= interval) {
last_step_tick = now;
apply_phase(phase_index);
phase_index = (uint8_t)((phase_index + 1U) & 0x07U);
}
}
/**
* @brief Record a debounced button press from BSP_PB_Callback.
*/
void StepMotor_ButtonHandler(void)
{
uint32_t now = HAL_GetTick();
/* Debounce: ignore presses within BUTTON_DEBOUNCE_MS of the last one. */
if ((uint32_t)(now - last_btn_tick) < BUTTON_DEBOUNCE_MS) {
return;
}
last_btn_tick = now;
button_pending = 1U;
}其中我还设置了电机转速控制和灯光控制,按下开发板上的用户按键后电机每一相之间的切换时间变化,从而调整电机转动速度
/* Step interval (ms) per state. Index 0 (STOP) is unused. */
static const uint16_t step_interval_ms[4] = {0, 3, 2, 1};
/* LED blink half-period (ms) per state. 0 means "solid on"; STOP forces off
* inside Tick regardless of table value. */
static const uint16_t led_blink_period_ms[4] = {0, 0, 500, 150};在main函数中,调用电机控制函数实现电机转动以及按键调速
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
StepMotor_Tick();
}实现效果如下

完整工程代码如下
我要赚赏金
