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.
Connect the A4988 driver:
PA5
which is D13
. Thanks to build-in LED you can see it's running without plugging the motor. This example uses Timer 2 channel 1. If you want to use other timer, remember to set the corrsponding pin alternate function (the library won't switch it manually).PA9
which is D8
. PA8
which is D7
. Optional.Remember to correctly set the current limit on the driver!
makefile
.c_cpp_properties.json
.launch.json
.make compile upload
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.
The motor can be in 4 states:
nextCommand
sets the movement.