Forum: Compiler & IDEs AVR: Variable einem Ausgangsport zuweisen


von Norbert M. (schnorpser)


Lesenswert?

Hallo,

stehe eben auf dem Schlauch..
Versuche den Inhalt einer Variablen, einem Port zuzuweisen.
Das Programm (ein Testprogramm), soll den Zustand des Ports D einlesen 
(Taster) und auf Port B ausgeben. Das ganze teste ich via STK500-Board, 
AVR: ATmega8

Problem: Die LEDs leuchten immer, Tastendrücker werden ignoriert.

Kann mir bitte jemand auf die Sprünge helfen?
1
#include <avr/interrupt.h>
2
#include <avr/io.h>
3
#include <stdint.h>
4
5
static volatile uint8_t tastenzustand = 0x00;
6
7
int main(void)
8
{
9
    // PORTB Ausgänge
10
  DDRB  = 0xFF;
11
    PORTB = 0xFF;
12
    // PORTC Eingänge
13
    DDRD  = 0x00;
14
    PORTD = 0xFF;
15
  
16
  while(1)
17
  {
18
    if (!(PIND & (1<PIND0)))
19
      tastenzustand = tastenzustand | 0x01;
20
    else
21
      tastenzustand = tastenzustand & 0x01;
22
    
23
    if ((PIND & (1<PIND1)))
24
      tastenzustand = tastenzustand | 0x02;
25
    else
26
      tastenzustand = tastenzustand & 0x02;
27
    
28
    if ((PIND & (1<PIND2)))
29
      tastenzustand = tastenzustand | 0x04;
30
    else
31
      tastenzustand = tastenzustand & 0x04;  
32
      
33
    if (!(PIND & (1<PIND3)))
34
      tastenzustand = tastenzustand | 0x08;
35
    else
36
      tastenzustand = tastenzustand & 0x08;
37
    
38
    if (!(PIND & (1<PIND4)))
39
      tastenzustand = tastenzustand | 0x10;
40
    else
41
      tastenzustand = tastenzustand & 0x10;
42
    
43
    if (!(PIND & (1<PIND5)))
44
      tastenzustand = tastenzustand | 0x20;
45
    else
46
      tastenzustand = tastenzustand & 0x20;
47
    
48
    if (!(PIND & (1<PIND6)))
49
      tastenzustand = tastenzustand | 0x40;
50
    else
51
      tastenzustand = tastenzustand & 0x40;
52
    
53
    if (!(PIND & (1<PIND7)))
54
      tastenzustand = tastenzustand | 0x80;
55
    else
56
      tastenzustand = tastenzustand & 0x80;
57
    
58
    PORTB = tastenzustand; 
59
  }
60
}

Dankende Grüße
Norbert

von Ralf (Gast)


Lesenswert?

Denk' noch mal nach:
Deine dir selbst gestellte Aufgabe hat nichts mit dem zu tun, was du 
programmiert hast. Das geht viel einfacher! ...

Norbert Martsch schrieb:
> Das Programm (ein Testprogramm), soll den Zustand des Ports D einlesen

von Markus (Gast)


Lesenswert?

Vielleicht hilft's:

tastenzustand = tastenzustand & 0xFE;
   statt
tastenzustand = tastenzustand & 0x01;

etc.

P. S. Das Programm läßt sich stark vereinfachen.

von Klaus (Gast)


Lesenswert?

Hallo,


Du musst die Bits auch wieder mal auf 0 setzen

nicht so,
Norbert Martsch schrieb:
> tastenzustand = tastenzustand & 0x01;

sondern so
  tastenzustand = tastenzustand & ~0x01;

von Norbert M. (schnorpser)


Lesenswert?

Hallo zusammen,

noch ein Nachtrag: im weiteren Verlauf soll später die Variable 
tastenzustand verzögert und danach erst die LED aktiviert/deaktiviert 
werden, und zwar jede Taste unabhängig voneinander. Insofern mag der 
jetzige Code etwas sinnentleert aussehen, dieser wird aber noch wachsen 
und ist nur ein "Modul" des Vorhabens, welches ich separat testen 
möchte.

Viele Grüße,
Norbert

von Ralf (Gast)


Lesenswert?

Norbert Martsch schrieb:
> Das Programm (ein Testprogramm), soll den Zustand des Ports D einlesen
> (Taster) und auf Port B ausgeben.

!!!

von Ralf (Gast)


Lesenswert?

Ralf schrieb:
> Norbert Martsch schrieb:
>> Das Programm (ein Testprogramm), soll den Zustand des Ports D einlesen
>> (Taster) und auf Port B ausgeben.
>
> !!!

Na gut, etwas spät. Ändert aber an dem möglichen Code nicht viel.
1
Tastenzustand=PORTD;
2
/* Mach was mit Tastenzustand, aber nicht so umständlich */
3
PORTB=Tastenzustand;

von Norbert M. (schnorpser)


Lesenswert?

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

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.