Forum: Mikrocontroller und Digitale Elektronik unerklärliche Änderung von Variablenwerten


von hans (Gast)


Lesenswert?

1
MENU_MACChange
2
(
3
)
4
/*****************************************************************************/
5
{
6
   EEPROM_Read(pu8Data,EEPROM_MAC_ADDRESS_LENGTH, EEPROM_MAC_ADDRESS_OFFSET );
7
   sprintf(cMAC,"%02x%02x.%02x.%02x.%02x.%02x",pu8Data[0],pu8Data[1],pu8Data[2]
8
           ,pu8Data[3],pu8Data[4],pu8Data[5]);
9
  LCD_Clear();
10
  LCD_Goto(0,0);
11
  LCD_PutS(cMAC);
12
  LCD_Goto(0,0);
13
  u8Cursor=0;
14
  
15
  while(u8ExitMAC!=1)
16
  {  
17
    xil_printf("%02x%02x.%02x.%02x.%02x.%02x\n\n",pu8Data[0],pu8Data[1],pu8Data[2]
18
           ,pu8Data[3],pu8Data[4],pu8Data[5]);
19
    KBD_Read(pu8Key);
20
    if(*pu8Key!=0x01) LCD_DelayMS(KBD_POLL_CYCLE);
21
    
22
    switch(*pu8Key){
23
      case 'R': 
24
      {  
25
        if(u8Cursor==15) LCD_Goto(0,0);
26
        u8Cursor++;
27
        LCD_Goto(0,u8Cursor);  
28
        break;
29
      }
30
      case 'L': 
31
      {
32
        if(u8Cursor==0) LCD_Goto(0,15);
33
        u8Cursor--;
34
        LCD_Goto(0,u8Cursor);        
35
        break;
36
      }
37
      case 'U':
38
      {
39
        switch(u8Cursor)
40
        {
41
        case 0:
42
        {        
43
          pu8Data[0] = pu8Data[0] + 0x10;
44
          LCD_Goto(0,0);
45
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
46
          LCD_PutS(&cMACbyte);
47
          LCD_Goto(0,u8Cursor);
48
          break;
49
        }
50
        case 1:
51
        {        
52
          pu8Data[0] = pu8Data[0] + 0x01;
53
          LCD_Goto(0,0);
54
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
55
          LCD_PutS(&cMACbyte);
56
          LCD_Goto(0,u8Cursor);
57
          break;
58
        }
59
        case 2:
60
        {        
61
          pu8Data[1] = pu8Data[1] + 0x10;
62
          LCD_Goto(0,2);
63
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
64
          LCD_PutS(&cMACbyte);
65
          LCD_Goto(0,u8Cursor);
66
          break;
67
        }
68
        case 3:
69
        {        
70
          pu8Data[1] = pu8Data[1] + 0x01;
71
          LCD_Goto(0,2);
72
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
73
          LCD_PutS(&cMACbyte);
74
          LCD_Goto(0,u8Cursor);
75
          break;
76
        }
77
        case 5:
78
        {        
79
          pu8Data[2] = pu8Data[2] + 0x10;
80
          LCD_Goto(0,5);
81
          sprintf(&cMACbyte,"%02x",pu8Data[2]);
82
          LCD_PutS(&cMACbyte);
83
          LCD_Goto(0,u8Cursor);
84
          break;
85
        }
86
        case 6:
87
        {        
88
          pu8Data[2] = pu8Data[2] + 0x01;
89
          LCD_Goto(0,5);
90
          sprintf(&cMACbyte,"%02x",pu8Data[2]);
91
          LCD_PutS(&cMACbyte);
92
          LCD_Goto(0,u8Cursor);
93
          break;
94
        }
95
        case 8:
96
        {        
97
          pu8Data[3] = pu8Data[3] + 0x10;
98
          LCD_Goto(0,8);
99
          sprintf(&cMACbyte,"%02x",pu8Data[3]);
100
          LCD_PutS(&cMACbyte);
101
          LCD_Goto(0,u8Cursor);
102
          break;
103
        }
104
        case 9:
105
        {        
106
          pu8Data[3] = pu8Data[3] + 0x01;
107
          LCD_Goto(0,8);
108
          sprintf(&cMACbyte,"%02x",pu8Data[3]);
109
          LCD_PutS(&cMACbyte);
110
          LCD_Goto(0,u8Cursor);
111
          break;
112
        }
113
        case 11:
114
        {        
115
          pu8Data[4] = pu8Data[4] + 0x10;
116
          LCD_Goto(0,11);
117
          sprintf(&cMACbyte,"%02x",pu8Data[4]);
118
          LCD_PutS(&cMACbyte);
119
          LCD_Goto(0,u8Cursor);
120
          break;
121
        }
122
        case 12:
123
        {        
124
          pu8Data[4] = pu8Data[4] + 0x01;
125
          LCD_Goto(0,11);
126
          sprintf(&cMACbyte,"%02x",pu8Data[4]);
127
          LCD_PutS(&cMACbyte);
128
          LCD_Goto(0,u8Cursor);
129
          break;
130
        }
131
        case 14:
132
        {        
133
          pu8Data[5] = pu8Data[5] + 0x10;
134
          LCD_Goto(0,14);
135
          sprintf(&cMACbyte,"%02x",pu8Data[5]);
136
          LCD_PutS(&cMACbyte);
137
          LCD_Goto(0,u8Cursor);
138
          break;
139
        }
140
        case 15:
141
        {        
142
          pu8Data[5] = pu8Data[5] + 0x01;
143
          LCD_Goto(0,14);
144
          sprintf(&cMACbyte,"%02x",pu8Data[5]);
145
          LCD_PutS(&cMACbyte);
146
          LCD_Goto(0,u8Cursor);
147
          break;
148
        }
149
        default: break;
150
        }/*switch Cursor*/
151
        break;
152
      }
153
      case 'D':
154
      {
155
        switch(u8Cursor)
156
        {
157
        case 0:
158
        {        
159
          pu8Data[0] = pu8Data[0] - 0x10;
160
          LCD_Goto(0,0);
161
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
162
          LCD_PutS(&cMACbyte);
163
          LCD_Goto(0,u8Cursor);
164
          break;
165
        }
166
        case 1:
167
        {        
168
          pu8Data[0] = pu8Data[0] - 0x01;
169
          LCD_Goto(0,0);
170
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
171
          LCD_PutS(&cMACbyte);
172
          LCD_Goto(0,u8Cursor);
173
          break;
174
        }
175
        case 2:
176
        {        
177
          pu8Data[1] = pu8Data[1] - 0x10;
178
          LCD_Goto(0,2);
179
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
180
          LCD_PutS(&cMACbyte);
181
          LCD_Goto(0,u8Cursor);
182
          break;
183
        }
184
        case 3:
185
        {        
186
          pu8Data[1] = pu8Data[1] - 0x01;
187
          LCD_Goto(0,2);
188
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
189
          LCD_PutS(&cMACbyte);
190
          LCD_Goto(0,u8Cursor);
191
          break;
192
        }
193
        case 5:
194
        {        
195
          pu8Data[2] = pu8Data[2] - 0x10;
196
          LCD_Goto(0,5);
197
          sprintf(&cMACbyte,"%02x",pu8Data[2]);
198
          LCD_PutS(&cMACbyte);
199
          LCD_Goto(0,u8Cursor);
200
          break;
201
        }
202
        case 6:
203
        {        
204
          pu8Data[2] = pu8Data[2] - 0x01;
205
          LCD_Goto(0,5);
206
          sprintf(&cMACbyte,"%02x",pu8Data[2]);
207
          LCD_PutS(&cMACbyte);
208
          LCD_Goto(0,u8Cursor);
209
          break;
210
        }
211
        case 8:
212
        {        
213
          pu8Data[3] = pu8Data[3] - 0x10;
214
          LCD_Goto(0,8);
215
          sprintf(&cMACbyte,"%02x",pu8Data[3]);
216
          LCD_PutS(&cMACbyte);
217
          LCD_Goto(0,u8Cursor);
218
          break;
219
        }
220
        case 9:
221
        {        
222
          pu8Data[3] = pu8Data[3] - 0x01;
223
          LCD_Goto(0,8);
224
          sprintf(&cMACbyte,"%02x",pu8Data[3]);
225
          LCD_PutS(&cMACbyte);
226
          LCD_Goto(0,u8Cursor);
227
          break;
228
        }
229
        case 11:
230
        {        
231
          pu8Data[4] = pu8Data[4] - 0x10;
232
          LCD_Goto(0,11);
233
          sprintf(&cMACbyte,"%02x",pu8Data[4]);
234
          LCD_PutS(&cMACbyte);
235
          LCD_Goto(0,u8Cursor);
236
          break;
237
        }
238
        case 12:
239
        {        
240
          pu8Data[4] = pu8Data[4] + 0x01;
241
          LCD_Goto(0,11);
242
          sprintf(&cMACbyte,"%02x",pu8Data[4]);
243
          LCD_PutS(&cMACbyte);
244
          LCD_Goto(0,u8Cursor);
245
          break;
246
        }
247
        case 14:
248
        {        
249
          pu8Data[5] = pu8Data[5] - 0x10;
250
          LCD_Goto(0,14);
251
          sprintf(&cMACbyte,"%02x",pu8Data[5]);
252
          LCD_PutS(&cMACbyte);
253
          LCD_Goto(0,u8Cursor);
254
          break;
255
        }
256
        case 15:
257
        {        
258
          pu8Data[5] = pu8Data[5] - 0x01;
259
          LCD_Goto(0,14);
260
          sprintf(&cMACbyte,"%02x",pu8Data[5]);
261
          LCD_PutS(&cMACbyte);
262
          LCD_Goto(0,u8Cursor);
263
          break;
264
        }
265
        default: break;
266
        }/*switch Cursor*/
267
        break;
268
      }
269
      case 'O':
270
      {
271
        EEPROM_Write( pu8Data,EEPROM_MAC_ADDRESS_LENGTH,EEPROM_MAC_ADDRESS_OFFSET );
272
        EEPROM_Read(pu8Data,EEPROM_MAC_ADDRESS_LENGTH, EEPROM_MAC_ADDRESS_OFFSET );
273
         xil_printf("%02x%02x.%02x.%02x.%02x.%02x",pu8Data[0],pu8Data[1],pu8Data[2]
274
           ,pu8Data[3],pu8Data[4],pu8Data[5]);
275
        
276
        u8ExitMAC=1;
277
        break;
278
      }
279
      case 'C':
280
      {
281
        u8ExitMAC=1;
282
        break;
283
      }
284
      default: break;
285
    }
286
    
287
  }
288
  
289
}



Sobald die while schleife augerufen wird ändenr sich die werte des 
pu8Data Feldes auf unerklärliche Weise. Hat jemand eien Idee hierzu?

danke

von Lehrmann M. (ubimbo)


Lesenswert?

hans schrieb:
> Sobald die while schleife augerufen wird ändenr sich die werte des
> pu8Data Feldes auf unerklärliche Weise. Hat jemand eien Idee hierzu?

Du machst ja permanent was mit dem pu8Data - warum wundert es dich, dass 
sich das Array ändert. Die switch wird ja mit jedem Durchlauf des 
Main-Programms bearbeitet.

von Stefan B. (stefan) Benutzerseite


Lesenswert?

Ja, *pu8Key enthält ein 'U' ein 'O' oder ein 'D'.
In diesen Fällen wird pu8Data potentiell geändert.

von hans (Gast)


Lesenswert?

ja das problem ist das sich pu8Data auch ändert ohen das die Fälle 
'U''O' oder 'D' eintreten.

von Karl H. (kbuchegg)


Lesenswert?

Kannst du deinen Code nicht erst mal ein wenig bereinigen?
Diese Copy&Paste Programmierung ist doch unüberschaubar!

ZB
vereinfacht sich das hier
1
      case 'D':
2
      {
3
        switch(u8Cursor)
4
        {
5
        case 0:
6
        {        
7
          pu8Data[0] = pu8Data[0] - 0x10;
8
          LCD_Goto(0,0);
9
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
10
          LCD_PutS(&cMACbyte);
11
          LCD_Goto(0,u8Cursor);
12
          break;
13
        }
14
        case 1:
15
        {        
16
          pu8Data[0] = pu8Data[0] - 0x01;
17
          LCD_Goto(0,0);
18
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
19
          LCD_PutS(&cMACbyte);
20
          LCD_Goto(0,u8Cursor);
21
          break;
22
        }
23
        case 2:
24
        {        
25
          pu8Data[1] = pu8Data[1] - 0x10;
26
          LCD_Goto(0,2);
27
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
28
          LCD_PutS(&cMACbyte);
29
          LCD_Goto(0,u8Cursor);
30
          break;
31
        }
32
        case 3:
33
        {        
34
          pu8Data[1] = pu8Data[1] - 0x01;
35
          LCD_Goto(0,2);
36
          sprintf(&cMACbyte,"%02x",pu8Data[1]);
37
          LCD_PutS(&cMACbyte);
38
          LCD_Goto(0,u8Cursor);
39
          break;
40
        }

zu dem hier
1
      case 'D':
2
      {
3
        if( u8Cursor % 2 == 0 )
4
          offset = 0x10;
5
        else
6
          offset = 0x01;
7
8
        index = u8Cursor / 2;
9
10
        pu8Data[index] = pu8Data[index] - offset;
11
        LCD_Goto( 0, 0 );
12
        sprintf( cMACbyte, "%02x", pu8Data[index] );
13
        LCD_PutS( cMACbyte );
14
        LCD_Goto( 0, u8Cursor );
15
      }

Ich hab jetzt nur die Fälle bis 3 durchgesehen, die restlichen hab ich 
nicht weiter genauer untersucht, aber beim schnellen hinschauen sind 
auch sie alle mit den paar Zeilen Code abgedeckt und wenn nicht müsste 
man sich ein kleines Array machen, welches zum Umsetzen des u8Cursor in 
die richtige Indexnummer benutzt wird.

Auf jeden Fall: Deine 25-tausend Seiten Code kann man auf ein paar 
wenige Zeilen eindampfen. Und dann verliert man auch nicht so schnell 
die Übersicht.

von Karl H. (kbuchegg)


Lesenswert?

Stutzig hat mich beim Umarbeiten auf den kleinen Code gemacht, dass du 
hier
1
          sprintf(&cMACbyte,"%02x",pu8Data[0]);
2
          LCD_PutS(&cMACbyte);

offenbar ein & brauchst:

Wie ist cMACbyte definiert?

Wenn hier ein & notwendig ist, dann ist da was faul.

von Stefan B. (stefan) Benutzerseite


Lesenswert?

Dann das Programm auf Stackoverflow abprüfen. Dein Code ruft viele 
"fette" Unterfunktionen auf, die den Stack in den Keller treiben können, 
d.h. in die Nähe von DATA und BSS Variablen.

Ein Hinweis ist die Anzeige des statischen SRAM-Bedarfs nach dem 
Kompilieren. Wenn das in Richtung 75% des vorhandenen SRAM geht, sollte 
eine Alarmglocke läuten.

Du könntest z.B. eine Kanarienvogel-Variable hinter deinen regulären 
Variablen platzieren, diese zyklisch prüfen und Alarm geben, wenn deren 
Inhalt verändert wird.

ADD: Geh dem Hinweis von Karl Heinz zuerst nach. Sieht 
vielversprechender aus!

von hans (Gast)


Lesenswert?

@karl heiz danke für den tipp mit dem verkleinern des codes.

die variabel ist so deklariert:  char cMACbyte;

irgendwie ändert sich pu8Data schon bei wenn ich die anderen fälle 
rauslasse.
1
void MENU_MACChange
2
(
3
)
4
/*****************************************************************************/
5
{
6
   EEPROM_Read(pu8Data,EEPROM_MAC_ADDRESS_LENGTH, EEPROM_MAC_ADDRESS_OFFSET );
7
   sprintf(cMAC,"%02x%02x.%02x.%02x.%02x.%02x",pu8Data[0],pu8Data[1],pu8Data[2]
8
           ,pu8Data[3],pu8Data[4],pu8Data[5]);
9
  LCD_Clear();
10
  LCD_Goto(0,0);
11
  LCD_PutS(cMAC);
12
  LCD_Goto(0,0);
13
  u8Cursor=0;
14
  
15
  while(u8ExitMAC!=1)
16
  {  
17
    xil_printf("%02x%02x.%02x.%02x.%02x.%02x\n\n",pu8Data[0],pu8Data[1],pu8Data[2]
18
           ,pu8Data[3],pu8Data[4],pu8Data[5]);
19
    KBD_Read(pu8Key);
20
    if(*pu8Key!=0x01) LCD_DelayMS(KBD_POLL_CYCLE);
21
    switch(*pu8Key){
22
23
      case 'C':
24
      {
25
        u8ExitMAC=1;
26
        break;
27
    }
28
      default: break;
29
    }
30
    
31
  }
32
  
33
}

von Karl H. (kbuchegg)


Lesenswert?

hans schrieb:
> @karl heiz danke für den tipp mit dem verkleinern des codes.
>
> die variabel ist so deklariert:  char cMACbyte;

Dachte ich mirs doch.

Und wie stellst du dir vor, dass zb ein String mit 3 Zeichen in so eine 
einsame klitzekleine char Variable mit gerade mal 8 Bit passt?

Zum Speichern von Strings benötigt man Arrays!

http://www.mikrocontroller.net/articles/FAQ#Wie_funktioniert_String-Verarbeitung_in_C.3F


> irgendwie ändert sich pu8Data schon bei wenn ich die anderen
> fälle rauslasse.

Wer weiß, wo du sonst noch überall Arrays überläufst.

von hans (Gast)


Lesenswert?

ok aber was ändert das daran das sich pu8Data ändert?

von Karl H. (kbuchegg)


Lesenswert?

hans schrieb:
> ok aber was ändert das daran das sich pu8Data ändert?

Du jubelst dem System eine einsame char Variabel als Array unter.
zb versucht sprintf dort 3 oder mehr Zeichen abzulegen. Die Variable hat 
aber nicht den Platz dafür. sprintf schreibt aber trotzdem. Es schreibt 
dann einfach beginnend mit der Startadresse die du angegeben hast im 
Speicher weiter. Und was immer dort im Speicher war .... wird 
überschrieben. Ohne Rücksicht auf Verluste.

Sei froh dass du jetzt drauf gekommen bist. Das hätte alles auch noch 
viel schlimmer ausgehen können, wenn du den Stack niedergebügelt 
hättest.

http://www.mikrocontroller.net/articles/FAQ#Wie_funktioniert_String-Verarbeitung_in_C.3F

von hans (Gast)


Lesenswert?

ja cool jetzt hab ich verstanden danke für die schnelle kompetente 
hilfe!


hans

von Karl H. (kbuchegg)


Lesenswert?

hans schrieb:
> ja cool jetzt hab ich verstanden danke für die schnelle kompetente
> hilfe!

Das muss allerdings nicht das einzige Problem gewesen sein.
Daher ist es meistens nicht sehr schlau wenn man Code schreibt, und Code 
schreibt und Code schreibt ... und nie zwischendurch mal testet.

Denn dann steht man am Ende mit einem Haufen Code da der nicht 
funktioniert. Und das ist so sicher wie das Amen im Gebet.

von Karl H. (kbuchegg)


Lesenswert?

hans schrieb:

> irgendwie ändert sich pu8Data schon bei wenn ich die anderen fälle
> rauslasse.
>
>
1
> 
2
> void MENU_MACChange
3
> (
4
> )
5
> /*****************************************************************************/
6
> {
7
>    EEPROM_Read(pu8Data,EEPROM_MAC_ADDRESS_LENGTH,
8
> EEPROM_MAC_ADDRESS_OFFSET );
9
>    sprintf(cMAC,"%02x%02x.%02x.%02x.%02x.%02x",pu8Data[0],pu8Data[1],pu8Data[2]
10
>            ,pu8Data[3],pu8Data[4],pu8Data[5]);
11
>   LCD_Clear();

Wie gross ist cMAC?


Nur damit das klar ist:
Es ist dein Bier dafür zu sorgen, dass die Arrays groß genug sind. Da 
gibt es kein Netz und keinen doppelten Boden, der dich hier schützt.

von hans (Gast)


Lesenswert?

ja eigentlich hab ich es zwischendurch getestet und hatte schon so 
komische phänomene aber als anfänger kommt man schwer darauf das 
irgendwo speicher überschrieben wird.

von hans (Gast)


Lesenswert?

cMAC ist jetzt 16 byte groß das war das problem vorher hatte ich es auf 
6 byte

von Karl H. (kbuchegg)


Lesenswert?

hans schrieb:
> cMAC ist jetzt 16 byte groß das war das problem vorher hatte ich es auf
> 6 byte

OK.
Lektion:
Solche Arrays definiert man nie zu knapp und niemals auf Knirsch!

Hier gilt die Devise: Nicht kleckern, klotzen!

Mach es 80 Byte groß

von Karl H. (kbuchegg)


Lesenswert?

Karl heinz Buchegger schrieb:
> hans schrieb:
>> cMAC ist jetzt 16 byte groß das war das problem vorher hatte ich es auf
>> 6 byte
>
> OK.
> Lektion:
> Solche Arrays definiert man nie zu knapp und niemals auf Knirsch!
>
> Hier gilt die Devise: Nicht kleckern, klotzen!
>
> Mach es 80 Byte groß


ps: 16 ist schon wieder zu klein
%02x%02x.%02x.%02x.%02x.%02x
braucht 17 Bytes. Du hast auf das abschliessende \0 Byte vergessen.

Aber wie gesagt: Ordentlich überdimensionieren, damit dir bei der 
nächsten Formatänderung nicht schon wieder alles um die Ohren fliegt.

Der nächste macht daraus:
1
  sprintf( cMAC, "MAC: %02x%02x.%02x.%02x.%02x.%02x",  ...
2
3
  ...
4
5
  LCD_PutS( cMAC );
6
  ...

weil ihm das besser gefällt, und schon sind deine 17 Bytes schon wieder 
zu klein.

Und nein: Man braucht nicht für jeden String ein neues Array. SO wie du 
das hast, kannst du alle sprintf über denselben Buffer (dasselbe char 
Array) abwickeln. Und das dann dafür reichlich groß dimensionieren.

Und auch mal über den Einsatz von snprintf nachdenken.

von hans (Gast)


Lesenswert?

alles kalr habs jetzt auf 80 hoffe jetzt gibs nie wieder probleme damit 
:D

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.