How to read low, and high byte from constant in AVR GCC

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have the following constant

#define test16bit  0xffaa

how to read the low byte, and high byte from test16bit, and do something like this:

varA8bit = low(test16bit);
varB8bit = high(test16bit);

Learn as long as you live

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
varA8bit = test16bit & 0xFF;
varB8bit = test16bit >> 8;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define low(x)   ((x) & 0xFF)
#define high(x)   (((x)>>8) & 0xFF)

Note that since you are assigning to an 8-bit variable, there is no need to mask with 0xFF.

Since you are using the macros on a constant, the Compiler will do the calculation at compile-time. So there will be no calculations done at run-time at all.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you guys

Learn as long as you live