rotary stuff

This commit is contained in:
Wolfgang Hottgenroth 2015-02-14 17:30:37 +01:00
parent 3c26c92329
commit 655f9021e6
4 changed files with 118 additions and 3 deletions

View File

@ -6,10 +6,11 @@
#include "control.h" #include "control.h"
#include "hardware.h" #include "hardware.h"
#include "ctrlParams.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); float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom; 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; newPwm = (uint16_t) newPwm_f;
analogWrite(PWM_PIN, newPwm); analogWrite(PWM_PIN, newPwm);
@ -78,7 +79,12 @@ void loop() {
maxCycleDelay = 0; maxCycleDelay = 0;
} }
int rotaryCount = getAndResetRotaryCount();
noInterrupts(); noInterrupts();
u_des += rotaryCount;
float my_u_curr = u_curr; float my_u_curr = u_curr;
uint16_t my_newPwm = newPwm; uint16_t my_newPwm = newPwm;
uint32_t my_cycleDelay = cycleDelay; uint32_t my_cycleDelay = cycleDelay;
@ -86,7 +92,7 @@ void loop() {
lcd.clear(); lcd.clear();
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print(U_des); lcd.print(u_des);
lcd.setCursor(8, 0); lcd.setCursor(8, 0);
lcd.print(my_u_curr); lcd.print(my_u_curr);
lcd.setCursor(0, 1); lcd.setCursor(0, 1);

View File

@ -35,6 +35,9 @@ const uint8_t LCD_D7 = 7;
const uint8_t LCD_ROWS = 2; const uint8_t LCD_ROWS = 2;
const uint8_t LCD_COLS = 16; const uint8_t LCD_COLS = 16;
const uint8_t ROTARY_A = ;
const uint8_t ROTARY_B = ;
const uint8_t SWITCH = ;

90
rotary.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <stdint.h>
#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;
}

16
rotary.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef ROTARY_H_
#define ROTARY_H_
const uint32_t DEBOUNCING_DEAD_TIME = 100;
void rotaryInit();
int getAndResetRotaryCount();
bool getAndResetSwitchState();
#endif