Read and Write Bytes in a Word

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

Read and Write Bytes in a Word

6,453 Views
DEMO908AP64
Contributor I
Unsuccessfully, I tried with .l and .h ...

Any idea ?
Labels (1)
0 Kudos
Reply
5 Replies

1,056 Views
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 Kudos
Reply

1,056 Views
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 Kudos
Reply

1,056 Views
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 Kudos
Reply

1,056 Views
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 Kudos
Reply

1,056 Views
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 Kudos
Reply