1 | /*******************************************************************
|
2 | * Drehencoder aus dem DDS-Projekt in Assembler auf c übersetzt
|
3 | * vorzugsweise für Anwendung in Arduino Jan 2022 R. Drabek
|
4 | * Basis jedenfalls von peda und Hannes Lux
|
5 | * Mit Timer2 Interrupt ca. 1 ms. Timer0 funktioniert nicht,
|
6 | * wenn man die millis() Funktion von Arduino benutzt
|
7 | * Encoder an PortD in Arduino fragt, wichtig, "gleichzeitig"
|
8 | * beide Phasen des Encoders ab.
|
9 | * Die LUT kann ggfs an den verwendeten Encoder angepasst werden,
|
10 | * bzw auch gespiegelt werden wen li, re vertauscht ist
|
11 | * Mit Arduino ist keine spezielle library zu includieren
|
12 | *******************************************************************/
|
13 | static uint8_t result; //Wert aus LUT ins Hauptprogramm bringen
|
14 | static uint8_t flag; //ist doppel von result
|
15 |
|
16 | void setup() //ohne 328mega datasheet nichts zu machen
|
17 | { TCCR2A |= (1 << WGM21)|(0 << WGM20); //CTC mode
|
18 | TCCR2B |= (1 << CS22)| ( 1<<CS21)|(1<<CS20); //XTAL / 64
|
19 | TIMSK2 |= 1 << TOIE2; //enable Interrupt
|
20 | OCR2A = 250; //für 1 ms Interrupt
|
21 | sei();
|
22 | pinMode(2, INPUT_PULLUP); //Phase A PD2
|
23 | pinMode(3, INPUT_PULLUP); //Phase B PD3
|
24 | }
|
25 |
|
26 | ISR(Timer2_0x0012) //funkt nur mit Zahl, obwohl Timer2 überbestimmt ist
|
27 | { const uint8_t enc_lut[16] = {0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0};
|
28 | uint8_t index;
|
29 | static uint8_t last; //alte Encoderphasen
|
30 | static uint8_t aktuell; //aktuelle -"-
|
31 |
|
32 | aktuell = PIND & 0b00001100; //input PORTD PINS 2,3 und maskiere Phasen
|
33 | // Vorsicht bei anders gewählten Portpins
|
34 | index = aktuell | last >>2; //erzeuge index für enc_lut mit rechtssch.
|
35 | last = aktuell; //so schiebt sich altes last ins Nirvana
|
36 | result = enc_lut[index]; //result ist globale
|
37 | }
|
38 | void loop()
|
39 | { flag = result;
|
40 | }
|