16 and 32 bit comnination

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

16 and 32 bit comnination

3,584 次查看
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
标签 (1)
0 项奖励
回复
5 回复数

1,280 次查看
ernestsnaith
Contributor I
Thanks
0 项奖励
回复

1,280 次查看
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 项奖励
回复

1,280 次查看
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 项奖励
回复

1,280 次查看
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 项奖励
回复

1,280 次查看
ernestsnaith
Contributor I
...using
 
Codewarrior 4.5
MC9S12XDT512
0 项奖励
回复