Problem with Array of structs

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with Array of structs

927 Views
protoease
Contributor II

I have all my global variabls in a header file, global.h.  In it, I declare the following:

 

 

// SWITCH debounce counterstypedef struct {    unsigned char debounce_active;  unsigned int debounce_timer;  int held_timer;  unsigned char state0;  unsigned char state1;} SWITCH;extern SWITCH Switch[6];

 

 

As you can see, I'm trying to declare an array of structs called Switch, of type SWITCH.

 

In my main.c file, I initialize all of the structs:

 

 

    for(key_num = 0; key_num <= 6; key_num++)    {      Switch[key_num].debounce_active = FALSE;      Switch[key_num].debounce_timer = 0;      Switch[key_num].held_timer = 0;      switch(key_num)      {        case 0:          Switch[0].state0 = SWITCH0;          Switch[0].state1 = SWITCH0;          break;        case 1:          Switch[1].state0 = SWITCH1;          Switch[1].state1 = SWITCH1;          break;        case 2:          Switch[2].state0 = SWITCH2;          Switch[2].state1 = SWITCH2;          break;        case 3:          Switch[3].state0 = SWITCH3;          Switch[3].state1 = SWITCH3;          break;        case 4:          Switch[4].state0 = SWITCH4;          Switch[4].state1 = SWITCH4;          break;        case 5:          Switch[5].state0 = SWITCH5;          Switch[5].state1 = SWITCH5;          break;        case 6:          Switch[6].state0 = SWITCH1;  // state 6 is a combo of up & down keys          Switch[6].state1 = SWITCH4;          break;        default:          break;      };    }

 

No compiler warnings, errors, but when I debug the thing none of the member variables are getting set correctly.

 

What I can see when I do this is just garbage setting in the member variables, such as Switch[3].state0 will read some large int value that doesn't make any sense.

 

 

I would appreciate any help on this, it is driving me crazy.  This was just supposed to be a simple debounce routine. 

 

Thanks

Labels (1)
0 Kudos
3 Replies

498 Views
CrasyCat
Specialist III

Hello

 

First question is

  - Which CPU are you targeting (HC08, HC12, Coldfire, ..)
  - Which version of CodeWarrior are you using?
    To retrieve that info:
    - Start CodeWarrior
    - Select Help -> About Freescale CodeWarrior
    - Click on "Install Products"
    - CodeWarrior version used is displayed on top in the Installed Products dialog.

 

Also keep in mind that if you declare a table of 6 elements SWITCH Switch[6];

the table includes 6 elements indexed from 0 to 5.

 

Attempting to access Switch{6} is not valid.

 

So either define a table with 7 elements or remove the access to element with index 6.

 

 

CrasyCat

0 Kudos

498 Views
protoease
Contributor II

Sorry, this is on a MC9S08JM60 processor and I'm using Codewarrior IDE version 6.3.

 

Yes you are right, I should have declared it as SWITCH Switch[7];

 

Should I have declared it as extern Struct SWITCH Switch[7]; ??

 

Thanks

protoease

 

 

0 Kudos

498 Views
protoease
Contributor II

Disregard...It was something completely different.

 

BTW, the debugger (True-Time simulator) gave weird address for these structs.  When you drag and drop one of the array'd structs (such as Switch[0]) to the memory window, it said the struct was located at 0x00 which is where the ports are at.  What is the reason for this behavior??

 

Thanks

 

 

0 Kudos