16 and 32 bit comnination

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

16 and 32 bit comnination

3,570 Views
ernestsnaith
Contributor I
Hi
 
I have the timer running as well as secondary counter which increments on timer overflow. I am storing values of both at regualr intervals. Is there away of storing both 16 bit values as a long integer type to be used in expressions at a later time? Also is the reverse possible? accessing high and low parts of long int.
 
Cheers
Labels (1)
0 Kudos
Reply
5 Replies

1,266 Views
ernestsnaith
Contributor I
Thanks
0 Kudos
Reply

1,266 Views
bigmac
Specialist III
Hello,
 
A possible method is to form a union between a long and an integer array.  You can then manipulate the value of the individual words forming the long.
 
#define dword unsigned long
 
typedef union {
  word wordval[2];
  dword val;
} long_val;
 
long_val timer;
dword temp;
 
timer.wordval[0] = highreg;
timer.wordval[1] = lowreg;
 
temp = timer.val;  // 32-bit value
 
Regards,
Mac
 
0 Kudos
Reply

1,266 Views
Obetz
Contributor III
bigmac wrote:

typedef union {
  word wordval[2];
  dword val;
} long_val;
 
long_val timer;
dword temp;
 
timer.wordval[0] = highreg;
timer.wordval[1] = lowreg;



I'm missing the big red warning in your suggestion. How can you be sure about the order of high and low word?

Oliver
0 Kudos
Reply

1,266 Views
bigmac
Specialist III
Hello Oliver,
 
It is my understanding that the MC9S12XDT512 is big endian, and the code was presented with this in mind.  For a little endian case, the allocation of individual word values would obviously need to be reversed.
 
Since the problem presented was to specifically deal with hardware registers, "portability" does not seem to be an issue in this instance.
 
Regards,
Mac
 
0 Kudos
Reply

1,266 Views
ernestsnaith
Contributor I
...using
 
Codewarrior 4.5
MC9S12XDT512
0 Kudos
Reply