Guten Tag,
ich habe mal ne Frage, und zwar habe ich nachfolgenden Code hier
getestet. Jetzt will ich mir im Debugg Mode mit dem AVR Dragon in der
Watchlist die Variable
anschauen, geht das Überhaupt ?
ich meine, wenn ich wärend ich im Debbug Modus bin und am Encoder drehe
?
Denn, wenn ich die Variable zur Watchlist hinzufüge, steht optimized
away, muss ich da den Compiler noch anders mit den Optimirungsgrad
einstellen ?
oder ist mein Vorhaben von Haus aus nicht möglich ?
vielleicht kann mir bitte jemnand weiterhelfen.
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: ATmega88
|
13 | //------------------------------------------------------------------------
|
14 |
|
15 | #define F_CPU 16000000UL
|
16 |
|
17 | #define PHASE_A (PIND & 1<<PD2)
|
18 | #define PHASE_B (PIND & 1<<PD3)
|
19 |
|
20 | #define LEDS_DDR DDRC
|
21 | #define LEDS PORTC // 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<<WGM12) | (1<<CS11) | (1<<CS10); // CTC, prescaler 64
|
38 | OCR1A = (uint8_t)(F_CPU / 64.0 * 1e-3 - 0.5); // 1ms
|
39 | TIMSK1 |= 1<<OCIE1A;
|
40 | }
|
41 |
|
42 |
|
43 | ISR( TIMER1_COMPA_vect ) // 1ms for manual movement
|
44 | {
|
45 | int8_t new, diff;
|
46 |
|
47 | new = 0;
|
48 | if( PHASE_A ) new = 3;
|
49 | if( PHASE_B ) new ^= 1; // convert gray to binary
|
50 | diff = last - new; // difference last - new
|
51 | if( diff & 1 ) { // bit 0 = value (1)
|
52 | last = new; // store new as next last
|
53 | enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | int8_t encode_read1( void ) // read single step encoders
|
59 | {
|
60 | int8_t val;
|
61 |
|
62 | cli();
|
63 | val = enc_delta;
|
64 | enc_delta = 0;
|
65 | sei();
|
66 | return val; // counts since last call
|
67 | }
|
68 |
|
69 |
|
70 | int8_t encode_read2( void ) // read two step encoders
|
71 | {
|
72 | int8_t val;
|
73 |
|
74 | cli();
|
75 | val = enc_delta;
|
76 | enc_delta = val & 1;
|
77 | sei();
|
78 | return val >> 1;
|
79 | }
|
80 |
|
81 |
|
82 | int8_t encode_read4( void ) // read four step encoders
|
83 | {
|
84 | int8_t val;
|
85 |
|
86 | cli();
|
87 | val = enc_delta;
|
88 | enc_delta = val & 3;
|
89 | sei();
|
90 | return val >> 2;
|
91 | }
|
92 |
|
93 |
|
94 | int main( void )
|
95 | {
|
96 |
|
97 | PORTD |= (1<<PD2)|(1<<PD3); // PULL UP
|
98 |
|
99 | int32_t val = 0;
|
100 |
|
101 | LEDS_DDR = 0xFF;
|
102 | encode_init();
|
103 | sei();
|
104 |
|
105 | for(;;)
|
106 |
|
107 | {
|
108 | val += encode_read2();
|
109 | LEDS = val;
|
110 | }
|
111 | }
|