• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Enkoder i operacje na zmiennych
#1
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ść Dodgy Pewnie ktoś z Was mi pomoże Smile

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);
    }
  }
}
 
Odpowiedź
  


Wiadomości w tym wątku
Enkoder i operacje na zmiennych - przez leoj - 31-05-2021, 20:01
RE: Enkoder i operacje na zmiennych - przez leoj - 01-06-2021, 19:56
RE: Enkoder i operacje na zmiennych - przez leoj - 01-06-2021, 21:11
RE: Enkoder i operacje na zmiennych - przez leoj - 02-06-2021, 22:32
RE: Enkoder i operacje na zmiennych - przez leoj - 05-06-2021, 21:50

Skocz do:


Przeglądający: 1 gości