Forum: Mikrocontroller und Digitale Elektronik Beispielcode (hier aus dem Artikel) für Drehgeber auf STK500 testen


von Max B. (theeye)


Lesenswert?

Hallo!

Ich versuche gerade einen Drehencoder zum laufen zu bringen. Basis ist 
der Beispielcode hier aus dem Artikel Drehgeber von Peter Dannegger. 
Klappt leider noch nicht.

Ich nutze ein STK500 und der Drehgeber steckt auf dem Breadboard. Es 
handelt sich um den STEC12E08 (Datenblatt: 
http://www.reichelt.de/index.html?&ACTION=7&LA=3&OPEN=0&INDEX=0&FILENAME=F100%252F402097STEC12E08.PDF).

Hier mein aktueller Code:
1
/************************************************************************/
2
/*                                                                      */
3
/*                      Reading rotary encoder                      */
4
/*                      one, two and four step encoders supported   */
5
/*                                                                      */
6
/*              Author: Peter Dannegger                                 */
7
/*                                                                      */
8
/************************************************************************/
9
#include <avr/io.h>
10
#include <avr/interrupt.h>
11
#include <inttypes.h>
12
13
                // target: ATmega8
14
//------------------------------------------------------------------------
15
16
//#define XTAL        8e6         // 8MHz
17
18
#define PHASE_A     (PIND & 1<<PD0)
19
#define PHASE_B     (PIND & 1<<PD1)
20
21
#define LEDS_DDR    DDRB
22
#define LEDS        PORTB           // LEDs against VCC
23
24
25
26
volatile int8_t enc_delta;          // -128 ... 127
27
static int8_t last;
28
29
30
void encode_init( void )
31
{
32
  int8_t new;
33
34
  new = 0;
35
  if( PHASE_A )
36
    new = 3;
37
  if( PHASE_B )
38
    new ^= 1;                   // convert gray to binary
39
  last = new;                   // power on state
40
  enc_delta = 0;
41
42
  TCCR0 = (1<<CS02)|(1<<CS00);         // divide by 1024
43
  TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5);  // preload for 10ms
44
  TIMSK |= 1<<TOIE0;                   // enable timer interrupt
45
}
46
47
48
ISR( TIMER0_COMP_vect )             // 1ms for manual movement
49
{
50
  int new, diff;
51
52
  new = 0;
53
  if( PHASE_A )
54
    new = 3;
55
  if( PHASE_B )
56
    new ^= 1;                   // convert gray to binary
57
  diff = last - new;                // difference last - new
58
  if( diff & 1 ){               // bit 0 = value (1)
59
    last = new;                 // store new as next last
60
    enc_delta += (diff & 2) - 1;        // bit 1 = direction (+/-)
61
  }
62
}
63
64
65
int8_t encode_read1( void )         // read single step encoders
66
{
67
  int8_t val;
68
69
  cli();
70
  val = enc_delta;
71
  enc_delta = 0;
72
  sei();
73
  return val;                   // counts since last call
74
}
75
76
77
int main( void )
78
{
79
  int32_t val = 0;
80
81
  LEDS_DDR = 0xFF;
82
  encode_init();
83
  sei();
84
85
  for(;;){
86
    val += encode_read1();          // read a single step encoder
87
    LEDS = val;
88
  }
89
}

Über tatkräftige Unterstützung würde ich mich sehr freuen! :)

Gruß Max

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Max B. schrieb:
> TIMSK |= 1<<TOIE0;                   // enable timer interrupt
> }
>
> ISR( TIMER0_COMP_vect )             // 1ms for manual movement
> {

Das passt nicht zusammen. Du gibst den Overflow Interrupt frei, 
behandelst aber den COMP Interrupt.

Entweder nimmst du den CTC Mode (so macht PeDa das m.W.) oder du lässt 
den Timer einfach überlaufen und taktest ihn so, das du so ca. 1-10ms 
Zyklus hast.
In jedem Fall muss aber die Interrupt Freigabe zur ISR passen.

: Bearbeitet durch User
von Max B. (theeye)


Lesenswert?

Ah okay, danke! Mache mich noch einmal dran :)

Gruß Max

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.