clean up stepper/timer
parent
7955846d86
commit
0938fe70b1
|
@ -7,14 +7,12 @@
|
|||
#include "keyboard.h"
|
||||
#include "encoder.h"
|
||||
#include "debug.h"
|
||||
#include "timer.h"
|
||||
#include "stepper.h"
|
||||
#include "memes.h"
|
||||
|
||||
void setup() {
|
||||
setupDebug();
|
||||
setupEncoder();
|
||||
setupTimer();
|
||||
if (!setupStepper()) {
|
||||
fatal();
|
||||
}
|
||||
|
@ -59,10 +57,6 @@ void loop() {
|
|||
sadBuzz();
|
||||
} else if (key == KEY_C) {
|
||||
beep();
|
||||
} else if (digit == 4) {
|
||||
demo_moveBy(-8000);
|
||||
} else if (digit == 5) {
|
||||
demo_moveBy(8000);
|
||||
} else if (digit == 7) {
|
||||
auto micros1 = micros();
|
||||
// 0.1ms
|
||||
|
@ -94,7 +88,5 @@ void loop() {
|
|||
if (encoderEvent) {
|
||||
tick();
|
||||
Serial.println(encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
|
||||
changeFreq(encoderEvent == ENCODER_CLOCKWISE ? 1 : -1);
|
||||
Serial.println(OCR1A);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,10 @@
|
|||
|
||||
*/
|
||||
|
||||
#if F_CPU != 16000000
|
||||
#error "Unsupported F_CPU"
|
||||
#endif
|
||||
|
||||
bool setupStepper() {
|
||||
pinMode(STEPPER_ENABLE, OUTPUT);
|
||||
pinMode(STEPPER_DIR, OUTPUT);
|
||||
|
@ -24,9 +28,47 @@ bool setupStepper() {
|
|||
return false;
|
||||
}
|
||||
|
||||
// set up the timer
|
||||
noInterrupts();
|
||||
// reset timer settings
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
TCNT1 = 0;
|
||||
TIMSK1 &= ~(1 << OCIE1A);
|
||||
// 256 prescaler (16MHz / 256 = 62.5kHz)
|
||||
TCCR1B |= (1 << CS12);
|
||||
// CTC mode (Clear Timer on Compare Match)
|
||||
TCCR1B |= (1 << WGM12);
|
||||
// interrupt at 62.5kHz / (OCR1A + 1)
|
||||
OCR1A = 39;
|
||||
interrupts();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isTimerEnabled() {
|
||||
return TIMSK1 & (1 << OCIE1A);
|
||||
}
|
||||
|
||||
void setTimerEnabled(bool enabled) {
|
||||
if (enabled) {
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
} else {
|
||||
TIMSK1 &= ~(1 << OCIE1A);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t getTimerCadence() {
|
||||
return OCR1A;
|
||||
}
|
||||
|
||||
void setTimerCadence(uint16_t cadence) {
|
||||
noInterrupts();
|
||||
// interrupt at 62.5kHz / (OCR1A + 1)
|
||||
OCR1A = cadence;
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void setStepperEnabled(bool enabled) {
|
||||
digitalWrite(STEPPER_ENABLE, !enabled);
|
||||
}
|
||||
|
@ -41,3 +83,7 @@ inline void step() {
|
|||
delayMicroseconds(1);
|
||||
STEPPER_STEP_PORT &= ~STEPPER_STEP_BIT;
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
step();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,12 @@
|
|||
#define LEFT 0
|
||||
|
||||
bool setupStepper();
|
||||
|
||||
bool isTimerEnabled();
|
||||
void setTimerEnabled(bool enabled);
|
||||
uint16_t getTimerCadence();
|
||||
void setTimerCadence(uint16_t cadence);
|
||||
|
||||
void setStepperEnabled(bool enabled);
|
||||
void setStepperDirection(bool dir);
|
||||
inline void step();
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
#include <Arduino.h>
|
||||
#include "constants.h"
|
||||
|
||||
#define DEMO_REPORT_SECONDS 2
|
||||
#define DEMO_TRIGGER_MS DEMO_REPORT_SECONDS * 1000
|
||||
|
||||
#if F_CPU != 16000000
|
||||
#error "Unsupported F_CPU"
|
||||
#endif
|
||||
|
||||
void setupTimer() {
|
||||
noInterrupts();
|
||||
// reset timer settings
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
TCNT1 = 0;
|
||||
// 256 prescaler (16MHz / 256 = 62.5kHz)
|
||||
TCCR1B |= (1 << CS12);
|
||||
// CTC mode (Clear Timer on Compare Match)
|
||||
TCCR1B |= (1 << WGM12);
|
||||
// interrupt at 62.5kHz / (OCR1A + 1)
|
||||
OCR1A = 39;
|
||||
// OCR1B = 39;
|
||||
// enable timer
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void changeFreq(int16_t delta) {
|
||||
OCR1A += delta;
|
||||
}
|
||||
|
||||
// incremented every DEMO_TRIGGER_MS by interrupt,
|
||||
// read and decremented by main loop
|
||||
// volatile uint8_t timerEvents = 0;
|
||||
// uint8_t demoCounter = 0;
|
||||
|
||||
volatile uint16_t timerTicks = 0;
|
||||
|
||||
void demoTimer() {
|
||||
// if (timerTicks % 1000 == 0) {
|
||||
// Serial.println(timerTicks);
|
||||
// }
|
||||
// bool shouldTrigger = false;
|
||||
|
||||
// noInterrupts();
|
||||
// if (timerEvents > 0) {
|
||||
// timerEvents--;
|
||||
// shouldTrigger = true;
|
||||
// }
|
||||
// interrupts();
|
||||
|
||||
// if (shouldTrigger) {
|
||||
// demoCounter += DEMO_REPORT_SECONDS;
|
||||
// Serial.print("Uptime: ");
|
||||
// Serial.print(demoCounter);
|
||||
// Serial.println("s");
|
||||
// }
|
||||
}
|
||||
|
||||
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 timerMillis = 0;
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
if (stepsLeft != 0) {
|
||||
stepsLeft += stepsLeft > 0 ? -1 : 1;
|
||||
|
||||
// pulse STEP
|
||||
PORTC |= 1<<1;
|
||||
PORTC &= ~(1<<1);
|
||||
}
|
||||
}
|
||||
|
||||
// ISR(TIMER1_COMPA_vect) {
|
||||
// if (OCR1B < 34) {
|
||||
// OCR1B = 40;
|
||||
// } else {
|
||||
// OCR1B--;
|
||||
// }
|
||||
// }
|
|
@ -1,5 +0,0 @@
|
|||
void setupTimer();
|
||||
void demoTimer();
|
||||
|
||||
void changeFreq(int16_t delta);
|
||||
void demo_moveBy(int16_t steps);
|
Loading…
Reference in New Issue