Guten Tag,
ich habe einen Dregeber mit spur A und B TTL Ausgang und einen
Atmega328p.
Und versuche mich gerade an dem Drehgeber Tutorial hier aus dem Forum
und versuche ihn passend auf meinen 328P umzuschreiben, aber ich bekomme
keine Led am PortD zu leuchten/PortD zu zählen.
habe ich den Timer1 verkehrt eingestellt, so müsste doch 1ms raukommen ?
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 |
|
12 | // target: ATmega328P
|
13 | //------------------------------------------------------------------------
|
14 |
|
15 | #define XTAL 16e6 // 8MHz original code 8e6
|
16 |
|
17 | #define PHASE_A (PINB & 1<<PB0)
|
18 | #define PHASE_B (PINB & 1<<PB1)
|
19 |
|
20 | #define LEDS_DDR DDRD
|
21 | #define LEDS PORTD // LEDs against VCC
|
22 |
|
23 |
|
24 | volatile int8_t enc_delta; // -128 ... 127
|
25 | static int8_t last;
|
26 |
|
27 |
|
28 | void encode_init( void )
|
29 | {
|
30 | int8_t new;
|
31 |
|
32 | new = 0;
|
33 | if( PHASE_A ) new = 3;
|
34 | if( PHASE_B ) new ^= 1; // convert gray to binary
|
35 | last = new; // power on state
|
36 | enc_delta = 0;
|
37 | TCCR1B = (1<<CS11) | (1<<CS10); // prescaler 64
|
38 | TCCR1B = (1<<WGM12) ; // CTC
|
39 | OCR1A = (uint8_t)(XTAL / 64.0 * 1e-3 - 0.5); // 1ms
|
40 | TIMSK1 |= (1<<OCIE1A);
|
41 | }
|
42 |
|
43 |
|
44 | ISR( TIMER1_COMPA_vect ) // 1ms for manual movement
|
45 | {
|
46 | int8_t new, diff;
|
47 |
|
48 | new = 0;
|
49 | if( PHASE_A ) new = 3;
|
50 | if( PHASE_B ) new ^= 1; // convert gray to binary
|
51 | diff = last - new; // difference last - new
|
52 | if( diff & 1 ) { // bit 0 = value (1)
|
53 | last = new; // store new as next last
|
54 | enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 | int8_t encode_read1( void ) // read single step encoders
|
60 | {
|
61 | int8_t val;
|
62 |
|
63 | cli();
|
64 | val = enc_delta;
|
65 | enc_delta = 0;
|
66 | sei();
|
67 | return val; // counts since last call
|
68 | }
|
69 |
|
70 |
|
71 | int8_t encode_read2( void ) // read two step encoders
|
72 | {
|
73 | int8_t val;
|
74 |
|
75 | cli();
|
76 | val = enc_delta;
|
77 | enc_delta = val & 1;
|
78 | sei();
|
79 | return val >> 1;
|
80 | }
|
81 |
|
82 |
|
83 | int8_t encode_read4( void ) // read four step encoders
|
84 | {
|
85 | int8_t val;
|
86 |
|
87 | cli();
|
88 | val = enc_delta;
|
89 | enc_delta = val & 3;
|
90 | sei();
|
91 | return val >> 2;
|
92 | }
|
93 |
|
94 |
|
95 | int main( void )
|
96 | {
|
97 | int32_t val = 0;
|
98 |
|
99 | LEDS_DDR = 0xFF;
|
100 | encode_init();
|
101 | sei();
|
102 |
|
103 | for(;;)
|
104 | {
|
105 | val += encode_read1(); // read a single step encoder
|
106 | LEDS = val;
|
107 | }
|
108 | }
|