Forum: Compiler & IDEs Schleifenabruch bei Tasterdruck


von Jonas K. (nandus)


Lesenswert?

1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <stdio.h>
4
 #include "em4095.h" 
5
#define KEYS 2
6
7
8
9
10
 #define BAUD 9600UL
11
12
/*EEPROM schreiben*/
13
void EEPROM_write(unsigned int uiAddress, unsigned char ucData) 
14
{ 
15
/* Wait for completion of previous write */ 
16
while(EECR & (1<<EEPE)) 
17
; 
18
/* Set up address and data registers */ 
19
EEAR = uiAddress; 
20
EEDR = ucData; 
21
/* Write logical one to EEMPE */ 
22
EECR |= (1<<EEMPE); 
23
/* Start eeprom write by setting EEPE */ 
24
EECR |= (1<<EEPE); 
25
}
26
27
/*EEPROM lesen*/
28
unsigned char EEPROM_read(unsigned int uiAddress) 
29
{ 
30
/* Wait for completion of previous write */ 
31
while(EECR & (1<<EEPE)) 
32
; 
33
/* Set up address register */ 
34
EEAR = uiAddress; 
35
/* Start eeprom read by writing EERE */ 
36
EECR |= (1<<EERE); 
37
/* Return data from data register */ 
38
return EEDR; 
39
}
40
41
int uart_putc(char c)
42
{
43
  loop_until_bit_is_set(UCSRA, UDRE);
44
  UDR = c;
45
  return 0;
46
}
47
/*Funktion zum Bitweisen-Vergleichen der TAG IDs*/
48
int cmp_tag(uint8_t *a,uint8_t *b)
49
{
50
int i;
51
      for (i=0;i<5;i++)
52
          {
53
        if(a[i]!=b[i])
54
            return 0;
55
        }
56
return 1;
57
}
58
59
60
/*RS232-Ausgabe */
61
void print (char *text)
62
{
63
int i=0;
64
while (text[i])
65
    uart_putc(text[i++]);
66
  
67
}
68
void nibbleout(int wert)
69
{
70
if(wert < 10) uart_putc(wert +'0');
71
                else
72
              uart_putc(wert +'A'-10);
73
}
74
75
void hexout(int wert)
76
{
77
nibbleout((wert&0xf0)>>4);
78
nibbleout(wert&0xf);
79
80
}
81
82
void tagout(uint8_t *a)
83
{
84
int i;
85
      for (i=0;i<5;i++)
86
          {
87
        hexout(a[i]);
88
        }
89
print("\n\r");
90
} 
91
92
93
94
int main()
95
{
96
  DDRD  = 1 <<PD2;
97
  PORTD = 1<<PD2;
98
  //Leuchtdiodenport
99
    DDRB  =1<<PB6|1<<PB7;
100
  char  KeyNichtEingelesen=1; 
101
102
  UCSRB = 1<<TXEN;
103
  UCSRC = 1<<UCSZ1 | 1<<UCSZ0;
104
  
105
  UBRRH = (uint8_t) (F_CPU / (BAUD * 16) - 1) / 256;
106
  UBRRL = (uint8_t) (F_CPU / (BAUD * 16) - 1);
107
108
109
110
  TCCR0B = 1<<CS01 | 1<<CS00; //Timer/Counter0 prescaler 64
111
  MCUCR |= 1<<ISC10; //Any logical change on INT1 generates an interrupt request
112
  GIMSK |= 1<<INT1; //External interrupt request 1 enable (demod_out from RFID chip)
113
114
  sei();
115
116
  uint8_t tag[5];
117
    uint8_t key[5];//Schlüssel
118
  uint8_t abbruch=1;
119
  unsigned char EEPROM_Inhalt ;
120
  
121
122
  volatile uint32_t d;
123
  //ersten Tag als Key einlesen
124
125
126
127
128
while (1) /*Hauptprogramm*/
129
         
130
{
131
EEPROM_Inhalt = EEPROM_read(0);
132
if (EEPROM_Inhalt != 0x1F)
133
{
134
135
  while (KeyNichtEingelesen)
136
  {
137
    if (em4095_read_tag(key))
138
    {
139
  /*  print("Schluessel eingelesen: "); */
140
    tagout(key); 
141
  /*  print("\n\r"); */
142
      KeyNichtEingelesen=0;
143
    }
144
    }
145
146
   
147
EEPROM_write(0,key[0]);
148
EEPROM_write(1,key[1]);
149
EEPROM_write(2,key[2]);
150
EEPROM_write(3,key[3]);
151
EEPROM_write(4,key[4]);
152
}
153
else 
154
{
155
key[0]=EEPROM_read(0);
156
key[1]=EEPROM_read(1);
157
key[2]=EEPROM_read(2);
158
key[3]=EEPROM_read(3);
159
key[4]=EEPROM_read(4);
160
}
161
162
163
164
   /*HIER IST DAS HAUPTPROBLEM !!!*/
165
  while (abbruch==1)
166
  {
167
    if (em4095_read_tag(tag))
168
    {
169
    tagout(tag);
170
171
172
        if (cmp_tag(key,tag))
173
174
            PORTB=1<<PB6; //Led an Port PB6 aus und Led an PB7 an
175
          else
176
                    PORTB=1<<PB7; //Led an Port PB7 aus und Led an PB6 an
177
178
        }
179
        else
180
181
                   { PORTB=1<<PB7;} //Led an Port PB7 aus und Led an PB6 an
182
183
      for (d=0; d<50000; d++);
184
185
    
186
    
187
     
188
     if (PIND&(1<<PD4))
189
190
    {
191
    
192
    EEPROM_write(0,0x0);
193
    abbruch=0;
194
    } 
195
    }  
196
    }
197
}
Der Verwendete Controller ist ein ATTiny2313 die Anwendung ist das RFID 
Entwicklerboard von Pollin.

Meine Frage wo ist der DENKFEHLER meinerseits das er die Schleife nicht 
abbricht wenn ich den Taster (active High) der an PD4 liegt betätige.
bzw.
2. Frage nachdem ich den Taster der an PD4 anliegt betätige soll das 
Byte 0 im EEPROM mit  Nullen überschrieben werden,  ist dies richtig 
gemacht . Den Prototyp aus dem ATTiny2313 manuel

von Peter D. (peda)


Lesenswert?

Rücke mal ordentlich ein, damit man die einzelnen Blöcke {} erkennt.
Und poste als Dateianhang.

Was soll das Programm überhaupt machen?


Peter

von Stefan B. (stefan) Benutzerseite


Lesenswert?

Ich sehe das Problem in der Reaktion auf den Tastendruck. Vor dem 
Abprüfen ist eine (schlechte) Warteschleife und danach wird gerade in 
einem CPU-Takt auf die Taste geprüft.

Ich schlage vor, dass du in der Warteschleife kontinuierlich auf die 
Taste abprüftst und entsprechend reagierst.
1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <stdio.h>
4
5
#include <util/delay.h> // F_CPU und -Os nicht vergessen!
6
#define DEBUG 0         // steuert print Ausgabe
7
#define DUMMY 1         // weil em4095.h und em4095_read_tag() fehlen
8
9
#if DUMMY
10
uint8_t em4095_read_tag(uint8_t *k)
11
{
12
  return 1;
13
}
14
#else
15
#include "em4095.h" 
16
#endif
17
18
#define KEYS 2
19
#define BAUD 9600UL
20
21
/*EEPROM schreiben*/
22
void EEPROM_write(unsigned int uiAddress, uint8_t ucData) 
23
{ 
24
  /* Wait for completion of previous write */ 
25
  while(EECR & (1<<EEPE)) 
26
    ; 
27
28
  /* Set up address and data registers */ 
29
  EEAR = uiAddress; 
30
  EEDR = ucData; 
31
  /* Write logical one to EEMPE */ 
32
  EECR |= (1<<EEMPE); 
33
  /* Start eeprom write by setting EEPE */ 
34
  EECR |= (1<<EEPE); 
35
36
  /* Wait for completion of previous write */ 
37
  while(EECR & (1<<EEPE)) 
38
    ; 
39
}
40
41
42
/*EEPROM lesen*/
43
uint8_t EEPROM_read(unsigned int uiAddress) 
44
{ 
45
  /* Wait for completion of previous write */ 
46
  while(EECR & (1<<EEPE)) 
47
    ; 
48
49
  /* Set up address register */ 
50
  EEAR = uiAddress; 
51
  /* Start eeprom read by writing EERE */ 
52
  EECR |= (1<<EERE); 
53
  /* Return data from data register */ 
54
55
  return EEDR; 
56
}
57
58
59
void uart_putc(uint8_t c)
60
{
61
  loop_until_bit_is_set(UCSRA, UDRE);
62
  UDR = c;
63
}
64
65
66
/*Funktion zum Bitweisen-Vergleichen der TAG IDs*/
67
uint8_t cmp_tag(uint8_t *a, uint8_t *b)
68
{
69
  int i;
70
  for (i=0;i<5;i++)
71
  {
72
    if(*a++ != *b++)
73
      return 0;
74
  }
75
  return 1;
76
}
77
78
79
/*RS232-Ausgabe */
80
void print(char *text)
81
{
82
  while (*text)
83
    uart_putc(*text++);
84
}
85
86
void nibbleout(uint8_t wert)
87
{
88
  if(wert < 10) 
89
    uart_putc(wert+'0');
90
  else
91
    uart_putc(wert+'A'-10);
92
}
93
94
95
void hexout(uint8_t wert)
96
{
97
  nibbleout((wert&0xf0)>>4);
98
  nibbleout(wert&0xf);
99
}
100
101
102
void tagout(uint8_t *a)
103
{
104
  int i;
105
  for (i=0;i<5;i++)
106
  {
107
    hexout(*a++);
108
  }
109
  print("\n\r");
110
} 
111
112
113
int main(void)
114
{
115
  // Lokale Variablen
116
  uint8_t tag[5]; // eingelesener Tag
117
  uint8_t key[5]; // Schlüssel (= gespeicherter Tag)
118
  unsigned char EEPROM_Inhalt;
119
#if 0
120
  volatile uint32_t d;
121
#else
122
  uint8_t d;
123
#endif
124
125
  // Ports initialisieren
126
  DDRD  = 1<<PD2;
127
  PORTD = 1<<PD2;
128
  //Leuchtdiodenport
129
  DDRB  = 1<<PB6|1<<PB7;
130
131
  // UART initialisieren
132
  UCSRB = 1<<TXEN;
133
  UCSRC = 1<<UCSZ1 | 1<<UCSZ0;
134
  UBRRH = (uint8_t) (F_CPU / (BAUD * 16) - 1) / 256;
135
  UBRRL = (uint8_t) (F_CPU / (BAUD * 16) - 1);
136
137
  // Timer0 initialisieren
138
  TCCR0B = 1<<CS01 | 1<<CS00; //Timer/Counter0 prescaler 64
139
  MCUCR |= 1<<ISC10;          //Any logical change on INT1 generates an interrupt request
140
  GIMSK |= 1<<INT1;           //External interrupt request 1 enable (demod_out from RFID chip)
141
  sei();
142
143
  while (1) 
144
  {
145
    /*
146
        Erstes Byte aus dem EEPROM einlesen
147
        Wenn 1. byte aus EEPROM UNGLEICH 0x1F 
148
        ist, dann key mit em4095_read_tag() 
149
        einlesen und in EEPROM speichern
150
     */
151
    EEPROM_Inhalt = EEPROM_read(0);
152
    
153
    if (EEPROM_Inhalt != 0x1F)
154
    {
155
      uint8_t KeyEingelesen = 0; 
156
      while (!KeyEingelesen)
157
      {
158
        if (em4095_read_tag(key))
159
        {
160
#if DEBUG
161
          print("Schluessel eingelesen: ");
162
#endif
163
          tagout(key); 
164
          KeyEingelesen = 1;
165
#if DEBUG
166
          print("\n\r");
167
#endif
168
        }
169
      }
170
      EEPROM_write(0,key[0]);
171
      EEPROM_write(1,key[1]);
172
      EEPROM_write(2,key[2]);
173
      EEPROM_write(3,key[3]);
174
      EEPROM_write(4,key[4]);
175
    }
176
    else 
177
    {
178
      // key bereits im EEPROM vorhanden
179
      key[0]=EEPROM_read(0);
180
      key[1]=EEPROM_read(1);
181
      key[2]=EEPROM_read(2);
182
      key[3]=EEPROM_read(3);
183
      key[4]=EEPROM_read(4);
184
    }
185
186
    /*
187
       Weitere tags einlesen und mit key
188
       vergleichen und Ergebnis an LEDs anzeigen
189
       Abbruch durch Druck auf Taster
190
     */
191
    uint8_t abbruch = 0;
192
    while (!abbruch)
193
    {
194
      if (em4095_read_tag(tag))
195
      {
196
        tagout(tag);
197
        if (cmp_tag(key,tag))
198
          PORTB = 1<<PB6; // Led an Port PB6 aus und Led an PB7 an
199
        else
200
          PORTB = 1<<PB7; // Led an Port PB7 aus und Led an PB6 an
201
      }
202
      else
203
      { 
204
#if 0
205
        PORTB = 1<<PB7; //Led an Port PB7 aus und Led an PB6 an
206
#else
207
        PORTB = (1<<PB7) | (1<<PB7); //Led an Port PB7 und PB6 aus
208
#endif
209
      } 
210
211
#if 0
212
      for (d=0; d<50000; d++);
213
214
      if (PIND & (1<<PD4))
215
      {
216
        EEPROM_write(0,0x0);
217
        abbruch = 1;
218
      } 
219
#else
220
      /*
221
          User ca. 3s (120*25=3000) Zeit geben, 
222
          um Taster zu drücken. Tastendruck selbst
223
          wird alle 25ms gepollt
224
          
225
          Tastendruck löscht 1. Byte vom EEPROM
226
          (key ungültig machen)
227
       */
228
      for (d = 0; d < 120; d++)
229
      {
230
        // active-high Taster gedrückt?
231
        if (PIND & (1<<PD4))
232
        {
233
          EEPROM_write(0,0x0);
234
          abbruch = 1; // while(!abbruch) verlassen
235
          break;       // for sofort verlassen
236
        } 
237
        _delay_ms(25);      
238
      }
239
#endif 
240
    } // while(!abbruch) 
241
  }
242
}

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.