Read and Write Bytes in a Word

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Read and Write Bytes in a Word

6,446件の閲覧回数
DEMO908AP64
Contributor I
Unsuccessfully, I tried with .l and .h ...

Any idea ?
ラベル(1)
0 件の賞賛
返信
5 返答(返信)

1,049件の閲覧回数
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,049件の閲覧回数
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,049件の閲覧回数
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,049件の閲覧回数
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,049件の閲覧回数
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 件の賞賛
返信