Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/23/10 02:52
Read: times


 
#174411 - Unions and position of bytes
Hi there

I know that this is not 8051 related but I have it on an C8051F040 so its sort of related.

I have an array that I read from Flash of 6 bytes. and it is read into calendar_reg. this all works fine.

Then when I want to access an individual byte within the array I wanted to be clever and define a union so that I could access a single element within the array.

However The bytes are all mixed up when I access them, I assume that I have defined it incorrectly somehow but would like to know how.


my array has [01 02 03 04 05 06] stored in it, and I wanted to access c.control which should have 06 in it but it has 01.


typedef union CALENDAR{
unsigned char calendar_reg[CALENDAR_REGS];
unsigned char minutes;
unsigned char hours;
unsigned char date;
unsigned char month;
unsigned char year;
unsigned char control;
} CALENDAR;


thoughts are appreciated.

Regards

Marshall

List of 23 messages in thread
TopicAuthorDate
Unions and position of bytes            01/01/70 00:00      
   Syntax and strategy problem.            01/01/70 00:00      
      wow that was quick and excellent thanks            01/01/70 00:00      
      Works a treat            01/01/70 00:00      
   Note that this is heavily compiler-reliant            01/01/70 00:00      
      Code for transparency            01/01/70 00:00      
         Agreed - after bitter experience            01/01/70 00:00      
            Compiler Specific            01/01/70 00:00      
               How many compler brands/versions to test for?            01/01/70 00:00      
                  Create a Compiler header            01/01/70 00:00      
                     Didn't we talk about unions - your examples doesn't...            01/01/70 00:00      
                        Someone already wrote up a good way....            01/01/70 00:00      
                           Incomplete byte order. But pad is still dangerous            01/01/70 00:00      
                              why bother?            01/01/70 00:00      
                                 how would you know?            01/01/70 00:00      
                                    Exactly my point... you don't            01/01/70 00:00      
                                       Never give up on portability - just decide the amount            01/01/70 00:00      
               Yes, but            01/01/70 00:00      
               why bother?            01/01/70 00:00      
      unions and portability            01/01/70 00:00      
         OT: use TR0 = 0; TMR0 -= offset; TR0 = 1;            01/01/70 00:00      
         you're not?            01/01/70 00:00      
            It was just a warning that union type casts are dangerous            01/01/70 00:00      

Back to Subject List