31-05-2021, 20:01
Witam,
robię sobie sterownik temperatury. Będzie obsługiwany enkoderem. Skorzystałem z przykładu i modyfikuję go na swoje potrzeby. Ale stanąłem w miejscu próbując rozwiązać problem już przez kilka godzin i straciłem cierpliwość Pewnie ktoś z Was mi pomoże
Chciałbym zgodnie z przykładem wykonywać operację na zmiennej "item" która ma być cyfrą z zakresu 0-3. Jednak nie mogę jej zmienić - cały czas jej wartość to 0. Co jest nie tak? Zależy mi na zrozumieniu tego konkretnego przykładu, a nie pisanie innego kodu sterującego enkoderem, bo reszta programu jest już od niego zależna i działa poprawnie.
robię sobie sterownik temperatury. Będzie obsługiwany enkoderem. Skorzystałem z przykładu i modyfikuję go na swoje potrzeby. Ale stanąłem w miejscu próbując rozwiązać problem już przez kilka godzin i straciłem cierpliwość Pewnie ktoś z Was mi pomoże
Chciałbym zgodnie z przykładem wykonywać operację na zmiennej "item" która ma być cyfrą z zakresu 0-3. Jednak nie mogę jej zmienić - cały czas jej wartość to 0. Co jest nie tak? Zależy mi na zrozumieniu tego konkretnego przykładu, a nie pisanie innego kodu sterującego enkoderem, bo reszta programu jest już od niego zależna i działa poprawnie.
Kod:
// Type of rotary encoder
#define ROTARY_TYPE 1 // 0: 2 increments/step; 1: 4 increments/step (default)
#define ROTARY_1_PIN 7 // rotary encoder 1
#define ROTARY_2_PIN 8 // rotary encoder 2
// Variables for pin change interrupt
volatile uint8_t a0, b0, c0, d0;
volatile bool ab0;
volatile int count, countMin, countMax, countStep;
int item = 0;
void setup() {
pinMode(ROTARY_1_PIN, INPUT_PULLUP);
pinMode(ROTARY_2_PIN, INPUT_PULLUP);
// setup pin change interrupt for rotary encoder
PCMSK0 = bit (PCINT0); // Configure pin change interrupt on Pin8
PCICR = bit (PCIE0); // Enable pin change interrupt
PCIFR = bit (PCIF0); // Clear interrupt flag
}
void loop() {
setRotary(0, 3, 1, item);
item = getRotary();
}
// sets start values for rotary encoder
void setRotary(int rmin, int rmax, int rstep, int rvalue) {
countMin = rmin << ROTARY_TYPE;
countMax = rmax << ROTARY_TYPE;
countStep = rstep;
count = rvalue << ROTARY_TYPE;
}
// reads current rotary encoder value
int getRotary() {
return (count >> ROTARY_TYPE);
}
// Pin change interrupt service routine for rotary encoder
ISR (PCINT0_vect) {
uint8_t a = PINB & 1;
uint8_t b = PIND >> 7 & 1;
if (a != a0) { // A changed
a0 = a;
if (b != b0) { // B changed
b0 = b;
count = constrain(count + ((a == b) ? countStep : -countStep), countMin, countMax);
if (ROTARY_TYPE && ((a == b) != ab0)) {
count = constrain(count + ((a == b) ? countStep : -countStep), countMin, countMax);;
}
ab0 = (a == b);
}
}
}