Hallo,
habe den Code nun am laufen und es funktioniert :-)
Was im Eingangsposting auch falsch war:
NICHT
1 | if (!(PIND & (1<PIND0)))
|
SONDERN
1 | if (!(PIND & (1<<PIND0)))
|
(doppelte einfache eckige Klammer nach links) :-)
Hier der funktionierende Code:
1 | #include <avr/interrupt.h>
|
2 | #include <avr/io.h>
|
3 | #include <stdint.h>
|
4 | #include <util/delay.h>
|
5 |
|
6 | volatile uint8_t keystate = 0x3F;
|
7 | volatile uint16_t countTimer2 = 0; // Speichert den aktuellen Zählerwert
|
8 |
|
9 | volatile uint8_t keycounter_0;
|
10 | volatile uint8_t keycounter_1;
|
11 | volatile uint8_t keycounter_2;
|
12 | volatile uint8_t keycounter_3;
|
13 | volatile uint8_t keycounter_4;
|
14 | volatile uint8_t keycounter_5;
|
15 | volatile uint8_t keycounter_6;
|
16 | volatile uint8_t keycounter_7;
|
17 |
|
18 | void init_io()
|
19 | {
|
20 | // PORTB Ausgänge
|
21 | DDRB = 0xFF;
|
22 | PORTB = 0x3F;
|
23 | // PORTD Eingänge
|
24 | DDRD = 0x00;
|
25 | PORTD = 0x3F;
|
26 | }
|
27 |
|
28 | // ISR zum auffangen der Interrupts:
|
29 | ISR(TIMER2_COMP_vect)
|
30 | {
|
31 |
|
32 | if (countTimer2 < 100 )
|
33 | {
|
34 | countTimer2++;
|
35 | }
|
36 | else
|
37 |
|
38 | {
|
39 | if ( (keycounter_0 != 0) && (!(keystate & 0x01)) )
|
40 |
|
41 | {
|
42 | keycounter_0--;
|
43 | }
|
44 |
|
45 | if ( (keycounter_1 != 0) && (!(keystate & 0x02)) )
|
46 |
|
47 | {
|
48 | keycounter_1--;
|
49 | }
|
50 |
|
51 | if ( (keycounter_2 != 0) && (!(keystate & 0x04)) )
|
52 |
|
53 | {
|
54 | keycounter_2--;
|
55 | }
|
56 |
|
57 | if ( (keycounter_3 != 0) && (!(keystate & 0x08)) )
|
58 |
|
59 | {
|
60 | keycounter_3--;
|
61 | }
|
62 |
|
63 | if ( (keycounter_4 != 0) && (!(keystate & 0x10)) )
|
64 |
|
65 | {
|
66 | keycounter_4--;
|
67 | }
|
68 |
|
69 | if ( (keycounter_5 != 0) && (!(keystate & 0x20)) )
|
70 |
|
71 | {
|
72 | keycounter_5--;
|
73 | }
|
74 | /*
|
75 | if ( (keycounter_6 != 0) && (!(keystate & 0x40)) )
|
76 |
|
77 | {
|
78 | keycounter_6--;
|
79 | }
|
80 |
|
81 | if ( (keycounter_7 != 0) && (!(keystate & 0x80)) )
|
82 |
|
83 | {
|
84 | keycounter_7--;
|
85 | }
|
86 | */
|
87 | countTimer2 = 0;
|
88 | }
|
89 | }
|
90 |
|
91 |
|
92 | int main(void)
|
93 | {
|
94 | cli();
|
95 | init_io();
|
96 |
|
97 | // Initialisierung Timer2:
|
98 | TCCR2 = (1<<CS21) | (1<<WGM21); // Prescaler von 8 | CTC-Modus (siehe unten für Beschreibung)
|
99 | // TCNT2 = 0;
|
100 | OCR2 = 100; // Vergleichswert
|
101 | TIMSK |= (1<<OCIE2); // Interrupts aktivieren und damit Timer starten
|
102 | sei();
|
103 |
|
104 | while(1)
|
105 | {
|
106 | //Taste0
|
107 | if ((PIND & (1<<PIND0)))
|
108 | {
|
109 | if (keycounter_0 == 0)
|
110 | {
|
111 | keystate |= 0x01; //aus
|
112 | }
|
113 | }
|
114 | else
|
115 | {
|
116 | keystate &= ~0x01;
|
117 | keycounter_0 = 255;
|
118 | }
|
119 |
|
120 | //Taste 1
|
121 | if ((PIND & (1<<PIND1))) //Taste losgelassen
|
122 | {
|
123 | if (keycounter_1 == 0)
|
124 | {
|
125 | keystate |= 0x02; //aus
|
126 | }
|
127 | }
|
128 | else //Taste gedrückt
|
129 | {
|
130 | keystate &= ~0x02; //an
|
131 | keycounter_1 = 255;
|
132 | }
|
133 |
|
134 | //Taste 2
|
135 | if ((PIND & (1<<PIND2)))
|
136 | {
|
137 | if (keycounter_2 == 0)
|
138 | {
|
139 | keystate |= 0x04;
|
140 | }
|
141 | }
|
142 | else
|
143 | {
|
144 | keystate &= ~0x04;
|
145 | keycounter_2 = 255;
|
146 | }
|
147 |
|
148 | //Taste 3
|
149 | if ((PIND & (1<<PIND3)))
|
150 | {
|
151 | if (keycounter_3 == 0)
|
152 | {
|
153 | keystate |= 0x08;
|
154 | }
|
155 | }
|
156 | else
|
157 | {
|
158 | keystate &= ~0x08;
|
159 | keycounter_3 = 255;
|
160 | }
|
161 |
|
162 | //Taste 4
|
163 | if ((PIND & (1<<PIND4)))
|
164 | {
|
165 | if (keycounter_4 == 0)
|
166 | {
|
167 | keystate |= 0x10;
|
168 | }
|
169 | }
|
170 | else
|
171 | {
|
172 | keystate &= ~0x10;
|
173 | keycounter_4 = 255;
|
174 | }
|
175 | //Taste 5
|
176 | if ((PIND & (1<<PIND5)))
|
177 | {
|
178 | if (keycounter_5 == 0)
|
179 | {
|
180 | keystate |= 0x20;
|
181 | }
|
182 | }
|
183 | else
|
184 | {
|
185 | keystate &= ~0x20;
|
186 | keycounter_5 = 255;
|
187 | }
|
188 | /*
|
189 | //Taste 6
|
190 | if ((PIND & (1<<PIND6)))
|
191 | {
|
192 | if (keycounter_6 == 0)
|
193 | {
|
194 | keystate |= 0x40;
|
195 | }
|
196 | }
|
197 | else
|
198 | {
|
199 | keystate &= ~0x40;
|
200 | keycounter_6 = 255;
|
201 | }
|
202 |
|
203 | //Taster 7
|
204 | if ((PIND & (1<<PIND7)))
|
205 | {
|
206 | if (keycounter_7 == 0)
|
207 | {
|
208 | keystate |= 0x80;
|
209 | }
|
210 | }
|
211 | else
|
212 | {
|
213 | keystate &= ~0x80;
|
214 | keycounter_7 = 255;
|
215 | }
|
216 | */
|
217 | PORTB = keystate;
|
218 | }
|
219 | }
|
Euch vielen Dank für die Hinweise und Tipps,
Norbert