ugly interrupt-based stepper control
parent
50a70f5725
commit
0c3dd769bc
|
@ -30,6 +30,6 @@
|
|||
|
||||
// --- Z axis motor
|
||||
|
||||
#define STEPPER_ENABLE 10
|
||||
#define STEPPER_DIR 8
|
||||
#define STEPPER_STEP 9
|
||||
#define STEPPER_ENABLE A0
|
||||
#define STEPPER_STEP A1
|
||||
#define STEPPER_DIR A2
|
||||
|
|
|
@ -52,6 +52,10 @@ void loop() {
|
|||
sadBuzz();
|
||||
} else if (key == KEY_C) {
|
||||
beep();
|
||||
} else if (digit == 4) {
|
||||
demo_moveBy(-8000);
|
||||
} else if (digit == 5) {
|
||||
demo_moveBy(8000);
|
||||
} else {
|
||||
tick();
|
||||
}
|
||||
|
@ -65,6 +69,6 @@ void loop() {
|
|||
if (auto event = handleEncoder()) {
|
||||
tick();
|
||||
Serial.println(event == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
|
||||
demoStepper(event == ENCODER_CLOCKWISE ? LOW : HIGH);
|
||||
demo_moveBy(event == ENCODER_CLOCKWISE ? 800 : -800);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
#include <Arduino.h>
|
||||
#include "constants.h"
|
||||
|
||||
/*
|
||||
|
||||
A4988 Stepper Motor Driver.
|
||||
|
||||
NOTE: minimum pulse width 1us high, 1us low
|
||||
|
||||
*/
|
||||
|
||||
void setupStepper() {
|
||||
pinMode(STEPPER_ENABLE, OUTPUT);
|
||||
pinMode(STEPPER_DIR, OUTPUT);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <Arduino.h>
|
||||
#include "constants.h"
|
||||
|
||||
#define DEMO_REPORT_SECONDS 2
|
||||
#define DEMO_TRIGGER_MS DEMO_REPORT_SECONDS * 1000
|
||||
|
@ -18,7 +19,7 @@ void setupTimer() {
|
|||
// CTC mode (Clear Timer on Compare Match)
|
||||
TCCR1B |= (1 << WGM12);
|
||||
// interrupt at 62.5kHz / (OCR1A + 1)
|
||||
OCR1A = 0;
|
||||
OCR1A = 19;
|
||||
// enable timer
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
interrupts();
|
||||
|
@ -47,20 +48,27 @@ void demoTimer() {
|
|||
}
|
||||
}
|
||||
|
||||
volatile int16_t stepsLeft = 0;
|
||||
|
||||
void demo_moveBy(int16_t steps) {
|
||||
bool dir;
|
||||
|
||||
noInterrupts();
|
||||
stepsLeft += steps;
|
||||
dir = steps > 0 ? LOW : HIGH;
|
||||
digitalWrite(STEPPER_DIR, dir);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
volatile uint16_t timerTicks = 0;
|
||||
volatile uint16_t timerMillis = 0;
|
||||
|
||||
// triggers at 62.5kHz
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
timerTicks++;
|
||||
if (timerTicks == 625) {
|
||||
timerTicks = 0;
|
||||
timerMillis += 10;
|
||||
}
|
||||
if (timerMillis >= DEMO_TRIGGER_MS) {
|
||||
timerMillis -= DEMO_TRIGGER_MS;
|
||||
timerEvents++;
|
||||
}
|
||||
if (stepsLeft != 0) {
|
||||
stepsLeft += stepsLeft > 0 ? -1 : 1;
|
||||
|
||||
// TODO: Add motor encoder reading, calculate required carriage offset, control stepper
|
||||
// pulse STEP
|
||||
PORTC |= 1<<1;
|
||||
PORTC &= ~(1<<1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
void setupTimer();
|
||||
void demoTimer();
|
||||
|
||||
void demo_moveBy(int16_t steps);
|
||||
|
|
Loading…
Reference in New Issue