ErniW / Stepper-motor-bare-metal-STM32

A non-blocking bare-metal firmware to control stepper motor with A4988 driver, made on STM32F446RE board using timers and interrupts.
2 stars 0 forks source link
a4988 arm bare-metal cortex-m embedded interrupts stepper-motor stm32 timer

Stepper-motor-bare-metal-STM32

A non-blocking, bare-metal firmware to control stepper motor with A4988 driver, made on STM32F446RE board using timers and interrupts. Code uses queue data structure to sequence the movement command but its usage is not mandatory.

Circuit

Connect the A4988 driver:

Remember to correctly set the current limit on the driver!

Compile and Upload

Library

Initialize the stepper.

Stepper(int stepsPerRevolution);

Initialize the stepper timer.

void timerInit(TIM_TypeDef* timer, int timerChannel, IRQn_Type timerInt, unsigned long APBClockFreq);

Important: alternate function of step pin must be set manually to corresponding timer.

Set the dir pin

void setDirPin(GPIO_TypeDef* dirPort, int dirPin);

Set the port and pin number.

Set the sleep pin

void setSleepPin(GPIO_TypeDef* sleepPort, int sleepPin);

Set the port and pin number.

Set the speed

void setSpeed(int speed);

Set speed in RPM. Be aware that speed over 150 can cause stepper to block. You can play with WAIT_TIME to see how it works.

Trigger next command and move by X steps

void nextCommand(long long steps);

The sign determines the direction. If command has the same as previous direction it will move, when it has to change the direction it will go to stabilize state before moving.

For other utility methods please refer to stepper.h file.

States

The motor can be in 4 states: