Forum: Mikrocontroller und Digitale Elektronik Problem mit Drehencoder


von Matthias (Gast)


Lesenswert?

Hallo Leute,

ich habe ein Problem mit meinem Drehencoder. Ich benutze einen 
Atmega32-16PU und habe da einen Drehencoder ALPS STEC12E von Reichelt 
angeschlossen. Ich benutze (Dankbarerweise) den Code von Peter Dannegger 
mit einer kleinen Erweiterung die nur jeden 4. Schritt zählt (auch aus 
der Codesammlung) da der Drehencoder nur nach einem Zyklus (4 Zustände) 
einrastet. Mein Problem ist jetzt, dass der uC nur vorwärts zählt, egal 
in welche Richtung ich drehe! Ich hab mir schon Stundenlang den kopf 
zerbrochen woran dass liegen mag. Vielleicht hat einer von euch ja noch 
eine Idee. hier noch mal mein vollständiger Code:

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

#define PHASE_A  (PINC & 1<<PINC0)  // PINC.0 Anschluss Drehencoder A
#define PHASE_B (PINC & 1<<PINC2)  // PINC.2 Anschluss Drecencoder B

volatile char  enc_delta, count=0;  // -128 ... 127

int main( void )
{
  TCCR0 = 1<<CS01;      //divide by 8 * 256
  TIMSK = 1<<TOIE0;      //enable timer interrupt
  PORTC = (1 << DDC0) | (1 << DDC2);
  DDRC = (1 << DDC1) | (1 << DDC3) | (1 << DDC4) | (1 << DDC5) | (1 << 
DDC6) | (1 << DDC7);
  DDRA = 0xFF;
  DDRB = 0xFF;
  sei();
  for(;;)        // main loop
    PORTA = count;
}

SIGNAL (SIG_OVERFLOW0)
{
  static char enc_last = 0x01;
  char i = 0;

  if( PHASE_A )
      i = 1;
  if( PHASE_B )
      i ^= 3;                  // convert gray to binary

  i -= enc_last;               // difference new - last
  if( i & 1 )                  // bit 0 = value (1)
    {
    enc_last += i;             // store new as next last
    enc_delta += (i & 2) - 1;  // bit 1 = direction (+/-)
    if(!(enc_delta % 4))       // nur jeden 4. Schritt zählen
      {
      if((i & 2) - 1)          // prüfen ob auf oder ab
        count--;               // 0-255
      else
  count++;               // 255-0
      }
    }
}


Ich sag schon mal Danke im Voraus !

MfG Matthias

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.