Skip to content

Instantly share code, notes, and snippets.

@d2rk
Last active August 29, 2015 14:10
Show Gist options
  • Save d2rk/6f591184276fab6c4ec2 to your computer and use it in GitHub Desktop.
Save d2rk/6f591184276fab6c4ec2 to your computer and use it in GitHub Desktop.
Test four-wire stepper motor with Propeller QuickStart board and L298N stepper motor driver

Test four-wire stepper motor with Propeller QuickStart board and L298N stepper motor driver controller board.

.PHONY: all clean
TARGET = test_stepper_motor
WRITE_TO_EEPROM ?= true
# See https://sites.google.com/site/propellergcc/documentation for details.
MEMORY_MODEL ?= lmm
BOARD = QUICKSTART
CC = /opt/parallax/bin/propeller-elf-gcc
CFLAGS = -m$(MEMORY_MODEL) -std=c99 -m32bit-doubles -O2
LOADER = /opt/parallax/bin/propeller-load
LFLAGS = -b $(BOARD)
all: $(TARGET)
$(TARGET):
$(CC) $(CFLAGS) test_stepper_motor.c -o $@
load: $(TARGET)
ifeq ($(WRITE_TO_EEPROM),true)
$(LOADER) $(LFLAGS) -e -r $<
else
$(LOADER) $(LFLAGS) -r $<
endif
clean:
-rm -f $(TARGET)
#include <propeller.h>
#define FORWARD_BUTTON_ID 7
#define BACKWARD_BUTTON_ID 5
/* 4-wire stepper motor. */
#define MOTOR_COIL1_A_PIN 10 /* RGBY */
#define MOTOR_COIL1_B_PIN 11
#define MOTOR_COIL2_C_PIN 12
#define MOTOR_COIL2_D_PIN 13
#define NUM_STEPS_PER_REVOLUTION 200 /* 360 / 1.8 */
#define STEP_DELAY 3 /* 60 * 1000 / NUM_STEPS_PER_REVOLUTION / speed */
#define FORWARD 1
#define BACKWARD 0
#define PAUSE(time) waitcnt((time * (CLKFREQ / 1000)) + CNT)
#define SET_OUTPUT(pin, state) \
do { \
if ((state) == 0) { \
_OUTA &= ~(1 << (pin)); \
} else { \
_OUTA |= 1 << (pin); \
} \
} while (0)
static volatile int step_id = 0;
void encode_step(char step) {
switch (step) {
case 0: /* 1010 */
OUTA |= 1 << (MOTOR_COIL1_A_PIN);
OUTA &= ~(1 << (MOTOR_COIL1_B_PIN));
OUTA |= 1 << (MOTOR_COIL2_C_PIN);
OUTA &= ~(1 << (MOTOR_COIL2_D_PIN));
break;
case 1: /* 0110 */
OUTA &= ~(1 << (MOTOR_COIL1_A_PIN));
OUTA |= 1 << (MOTOR_COIL1_B_PIN);
OUTA |= 1 << (MOTOR_COIL2_C_PIN);
OUTA &= ~(1 << (MOTOR_COIL2_D_PIN));
break;
case 2: /* 0101 */
OUTA &= ~(1 << (MOTOR_COIL1_A_PIN));
OUTA |= 1 << (MOTOR_COIL1_B_PIN);
OUTA &= ~(1 << (MOTOR_COIL2_C_PIN));
OUTA |= 1 << (MOTOR_COIL2_D_PIN);
break;
case 3: /* 1001 */
OUTA |= 1 << (MOTOR_COIL1_A_PIN);
OUTA &= ~(1 << (MOTOR_COIL1_B_PIN));
OUTA &= ~(1 << (MOTOR_COIL2_C_PIN));
OUTA |= 1 << (MOTOR_COIL2_D_PIN);
break;
}
}
void drive(char direction) {
if (direction == FORWARD) {
PAUSE(STEP_DELAY);
if (step_id == NUM_STEPS_PER_REVOLUTION) {
step_id = 0;
}
encode_step(++step_id & 3);
} else {
PAUSE(STEP_DELAY);
if (step_id == 0) {
step_id = NUM_STEPS_PER_REVOLUTION;
}
encode_step(--step_id & 3);
}
}
int main() {
int result;
char pin;
DIRA &= ~0xFF; /* setup buttons */
DIRA |= 1 << (MOTOR_COIL1_A_PIN); /* setup motor */
DIRA |= 1 << (MOTOR_COIL1_B_PIN);
DIRA |= 1 << (MOTOR_COIL2_C_PIN);
DIRA |= 1 << (MOTOR_COIL2_D_PIN);
OUTA &= ~0xFF0000; /* setup LEDs */
DIRA |= 0xFF0000;
OUTA |= 0xFF0000; /* flash LEDs */
PAUSE(1000);
OUTA &= ~0xFF0000;
while (1) {
result = INA & (unsigned)(0xFF);
for (pin = 0; pin < 16; pin++, result >>= 1) {
SET_OUTPUT(pin + 16, result & 1UL);
if (result & 1UL) {
switch (pin) {
case FORWARD_BUTTON_ID:
drive(FORWARD);
break;
case BACKWARD_BUTTON_ID:
drive(BACKWARD);
break;
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment