92 lines
1.7 KiB
C++
92 lines
1.7 KiB
C++
#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--;
|
|
// }
|
|
// }
|