Structs within Structs

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

Structs within Structs

3,168 Views
blinari
Contributor I

I am having a problem getting this to work.  I am Trying out this new thing ":1;"  It seems to be pretty cool when it works.
I find that sometimes it compiles just fine and breaks in simulator (not all the time). Other times the compiler gives me a warring and works just fine it also sometime doesn't work when it get a warring.

When is simulation mode I get this Error:
Error: Attempt to use uninitialized (uu) Accumulator.

This is the relivant code:


typedef struct status
{
    byte Mode0              :1;        /* b0 */
    byte Mode1              :1;        /* b1 */
    byte Mode_Read     :1;        /* b2 */
   
    byte High                  :1;        /* b5 */
    byte Active               :1;        /* b6 */
//    byte Master            :1;        /* b7 */
} Status;

typedef struct Port
{
//

int Period3;
Status Stat;
char Mode_out;
char Mode_in;    
char Clk_in;
char Clk_out;
char ModeRead;

} Port ;

void main(void)
{
Port Port1;
Port1.Stat.High=0;

while(1)
{
    Port1.Stat.High++;
}
}

Labels (1)
Tags (1)
0 Kudos
Reply
5 Replies

780 Views
CrasyCat
Specialist III
Hello
 
I assume you are using an HC12 MCU and you are running the application on the simulator. Am I right?
 
The full chip simulator is able to detect read access to uninitialized memory and in that particular case it detects one of these access.
 
Basically when you write
  Port1.Stat.High=0;
The compiler generates a "BCLR  2,SP,#8" instruction.
 
BCLR instruction is performing a read set bit and write on the local variable So basically it reads an un-initialized memory address.
 
You have two solutions to prevent that from happening:
  1- Configure the debugger so that it does not stop on read access to undefined memory location
      This is done as follows:
         - Start the debugger
         - Select "HCS12FCS" -> "Configure ..."
         - uncheck the box "Stop on Read undefined"
         - Click on the "Save" button
         - Save the configuration in a file called default.mem located in your project directory.
         - Click OK to save the memory configuration
     Starting from now debugger will not stop anymore on read to undefined memory location.
 
  2- Make sure the whole bitfield is initialized before you attempt to set a bit in there.   
      If you want to continue to track read access to undefined memory location you need to explicitly
      initialize the whole structure in the application.
      If the variable is a global variable this is performed automatically by the startup code (as soon as 
      you are using the delivered ANSI C startup code in your application).
      If the structure is a local variable, I would suggest you to initialize it as a whole at the beginning
      of the function.
      In that case it is good practice to define a union allowing to access the variable wither as a
      whole byte of as single bits
 
     In your case define the variable as follows:
         typedef union {
             byte   StatusByte ;
             struct status
              {
                 byte Mode0         :1;        /* b0 */
                 byte Mode1         :1;        /* b1 */
                 byte Mode_Read     :1;        /* b2 */
  
                 byte High          :1;        /* b5 */
                 byte Active        :1;        /* b6 */
             //    byte Master        :1;        /* b7 */
            } StatusBits;
     }Status;
main function will then be implemented as follows.
 
void main(void)
{
  Port Port1;
  Port1.Stat.StatusByte = 0;
  while(1)
  {
      Port1.Stat.StatusBits.High++;
  }
}
I hope this helps a bit.
    
CrasyCat
0 Kudos
Reply

780 Views
blinari
Contributor I
I am using the 9S08QG8

Thank you for your help.  The way that I am now implementing this is make an initialize function that looks like this.  It seems to work well.
void Port_Int(Port *P)
{
    char * pointer_To_Char;

    pointer_To_Char = (byte *) &(*P).Stat;
    *pointer_To_Char = 0;

    (*P).Period3=0;
    (*P).ModeRead=0;
.....
}


Message Edited by blinari on 2007-07-16 06:00 PM
0 Kudos
Reply

780 Views
CompilerGuru
NXP Employee
NXP Employee
I guess the OP is using the HC08,
>Error: Attempt to use uninitialized (uu) Accumulator.
is a message typically seen on that simulator.
Is there a way to switch of those checks on the HC08?

Daniel
0 Kudos
Reply

780 Views
CrasyCat
Specialist III
Hello
 
Just tried on HC08GP32 simulator on CW HC08 V5.1.
The simulator does not report any issue around "Attempt to use uninitialized (uu) Accumulator".
 
CrasyCat
0 Kudos
Reply

780 Views
peg
Senior Contributor IV
Hi,
 
Yes, if you do a STA before you load the accumulator with something you will get a very similar meassage in the standalone P&E HC08 product (XX replaces uu). But Hiwave HC08 will not complain!
???
 
0 Kudos
Reply