Read and Write Bytes in a Word

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

Read and Write Bytes in a Word

6,449 次查看
DEMO908AP64
Contributor I
Unsuccessfully, I tried with .l and .h ...

Any idea ?
标签 (1)
0 项奖励
回复
5 回复数

1,052 次查看
eexpress
Contributor I
I thought maybe this way.

#define HB(x) x>>8
#define LB(x) x&0xff

the CodeWarrior' complier can do rest step well.
0 项奖励
回复

1,052 次查看
bigmac
Specialist III

Hello,

The original problem was to combine individual bytes into a word, rather than split a word into individual bytes.  However, the macro approach seems a good one.

#define conv2word(hi,lo) (word)hi*256+(word)lo

e.g.
word wordval;
wordval = conv2word(ATD1RH,ATD1RL);

The compiler seems to optimise the code better using multiplication rather than a left-shift process.

Regards,
Mac

 

0 项奖励
回复

1,052 次查看
lupin
Contributor I
Try this:
union {
struct {
byte hi_;
byte lo_;
}part_;
word sum_;
}ful_;

#define hi_byte ful_.part_.hi_
#define lo_byte ful_.part_.lo_
#define full_word ful_.sum_

hi_byte=55;
lo_byte=22;
if(full_word>3200)

Is it OK?
0 项奖励
回复

1,052 次查看
DEMO908AP64
Contributor I
Thank you Lupin.

I knew that redefining the Word type was a solution.

I would like to avoid to do so and stick to the word type provided in Metrowerks.

I found no trace of the definition of that word type in the Project File.

Anybody, an other idea ?
0 项奖励
回复

1,052 次查看
alex_spotw
Contributor III
Hi:

If you're using Processor Expert, you can use one of the methods provided by the ATD Bean to read the value as 16 bits.

If you're not using PE, you can use code like:
(From PE)
/* 16-bit register (big endian) */
typedef union {
word w;
struct {
byte high,low;
} b;
} TWREG;


word Read_Sample(void)
{
TWREG Sample

Sample.b.high = ATD1RH;
Sample.b.low = ATD1RL;
return Sample.w; // Return as word (16 bit)
}


Regards,

Alex
0 项奖励
回复