diff --git a/TeensyPwm.cpp b/TeensyPwm.cpp index 9315c68..ef7516d 100644 --- a/TeensyPwm.cpp +++ b/TeensyPwm.cpp @@ -6,10 +6,11 @@ #include "control.h" #include "hardware.h" #include "ctrlParams.h" +#include "rotary.h" -volatile float U_des = 14.0; +volatile float u_des = 14.0; @@ -40,7 +41,7 @@ void cycle() { float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX); u_curr = u_adc * (R_top + R_bottom) / R_bottom; - float newPwm_f = ctrl.cycle(U_des, u_curr); + float newPwm_f = ctrl.cycle(u_des, u_curr); newPwm = (uint16_t) newPwm_f; analogWrite(PWM_PIN, newPwm); @@ -78,7 +79,12 @@ void loop() { maxCycleDelay = 0; } + + + int rotaryCount = getAndResetRotaryCount(); + noInterrupts(); + u_des += rotaryCount; float my_u_curr = u_curr; uint16_t my_newPwm = newPwm; uint32_t my_cycleDelay = cycleDelay; @@ -86,7 +92,7 @@ void loop() { lcd.clear(); lcd.setCursor(0, 0); - lcd.print(U_des); + lcd.print(u_des); lcd.setCursor(8, 0); lcd.print(my_u_curr); lcd.setCursor(0, 1); diff --git a/hardware.h b/hardware.h index 9c759d0..6fb2894 100644 --- a/hardware.h +++ b/hardware.h @@ -35,6 +35,9 @@ const uint8_t LCD_D7 = 7; const uint8_t LCD_ROWS = 2; const uint8_t LCD_COLS = 16; +const uint8_t ROTARY_A = ; +const uint8_t ROTARY_B = ; +const uint8_t SWITCH = ; diff --git a/rotary.cpp b/rotary.cpp new file mode 100644 index 0000000..57e8f21 --- /dev/null +++ b/rotary.cpp @@ -0,0 +1,90 @@ +#include +#include "hardware.h" +#include "rotary.h" + + + +volatile int rotaryCount = 0; +volatile bool switchState = false; +volatile unsigned long lastEvent = 0; + + +void rotary_a_interrupt() { + uint32_t currentEvent = millis(); + + if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { + lastEvent = currentEvent; + + int a = digitalRead(ROTARY_A); + int b = digitalRead(ROTARY_B); + + if (((a != -1) && (b != -1))) { + if (a == b) { + rotaryCount++; + } else { + rotaryCount--; + } + } + } +} + +void rotary_b_interrupt() { + uint32_t currentEvent = millis(); + + if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { + lastEvent = currentEvent; + + int a = digitalRead(ROTARY_A); + int b = digitalRead(ROTARY_B); + + if (((a != -1) && (b != -1))) { + if (a == b) { + rotaryCount--; + } else { + rotaryCount++; + } + } + } +} + +void switch_interrupt() { + unsigned long currentEvent = millis(); + + if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { + lastEvent = currentEvent; + + switchState = true; + } +} + +void rotaryInit() { + pinMode(SWITCH, INPUT_PULLUP); + pinMode(ROTARY_A, INPUT_PULLUP); + pinMode(ROTARY_B, INPUT_PULLUP); + attachInterrupt(SWITCH, switch_interrupt, FALLING); + attachInterrupt(ROTARY_A, rotary_a_interrupt, CHANGE); + attachInterrupt(ROTARY_B, rotary_b_interrupt, CHANGE); +} + + +int getAndResetRotaryCount() { + int res = 0; + + noInterrupts(); + res = rotaryCount; + rotaryCount = 0; + interrupts(); + + return res; +} + +bool getAndResetSwitchState() { + bool res = false; + + noInterrupts(); + res = switchState; + switchState = false; + interrupts(); + + return res; +} diff --git a/rotary.h b/rotary.h new file mode 100644 index 0000000..2722991 --- /dev/null +++ b/rotary.h @@ -0,0 +1,16 @@ +#ifndef ROTARY_H_ +#define ROTARY_H_ + + +const uint32_t DEBOUNCING_DEAD_TIME = 100; + + +void rotaryInit(); + +int getAndResetRotaryCount(); +bool getAndResetSwitchState(); + + + + +#endif \ No newline at end of file