View previous topic :: View next topic |
Author |
Message |
Guest
|
Highbyte/Lowbyte |
Posted: Thu Feb 05, 2009 10:50 am |
|
|
I have a simple question........How do you take a 16 bit number and split it into its high and low bytes?
ex.
16bitnumber=(16bitnumberhighbyte, 16bitnumberlowbyte)
I have searched the forums and cant find anyting. I have done it for the basic stamp long time ago.
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Feb 05, 2009 11:22 am |
|
|
make16
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Feb 05, 2009 12:57 pm |
|
|
I saw that, but that takes two 8 bit numbers and adds them to make a 16 bit number.
I need to take a 16 bit number and make two 8bits out of it.
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Feb 05, 2009 1:40 pm |
|
|
Code: | int8 var1, var2;
int16 number;
number = 0xAA88;
var1 = number & 0xFF;// stuff lower byte into var1
var2 = number >> 8 & 0xFF;// shift upper byte to lower position and store it into var2 |
var1 should equal 0x88 and var2 should equal 0xAA.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
|
Posted: Thu Feb 05, 2009 1:43 pm |
|
|
From help file:
make8(var, offset)
var is a 16 or 32 bit integer.
offset is a byte offset of 0,1,2 or 3.
EX:
Code: | int32 x;
int y;
y = make8(x,3); // Gets MSB of x |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 05, 2009 1:46 pm |
|
|
Quote: | I need to take a 16 bit number and make two 8bits out of it. |
Download the manual and look in the make8, make16 and make32
section:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look at this one:
Quote: | make8( )
Syntax: i8 = MAKE8(var, offset)
Parameters: var is a 16 or 32 bit integer.
offset is a byte offset of 0,1,2 or 3.
Returns: An 8 bit integer
Function: Extracts the byte at offset from var. |
For most simple things that you want to do, there will be a function.
You have to look in the manual. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Feb 05, 2009 4:09 pm |
|
|
Your 'text' question, and your program example, are for the opposite things.
Your text question asks how to get the individual bytes from a 16bit nmber. Make8 is the answer for this. But the line of code you post, is the code to assemble two 8bit values to make a 16bit value. Make16 is the function for this....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Feb 06, 2009 12:08 pm |
|
|
Interesting thing....
I just tried my solution and make8(). They both create the exact same ASM code.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 06, 2009 12:12 pm |
|
|
The CCS manual says:
Quote: |
make8( ) --
Extracts the byte at offset from var.
Same as: i8 = (((var >> (offset*8)) & 0xff) except it is done with a single byte move. |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Feb 07, 2009 1:07 pm |
|
|
Thanks for the help!! Works beautifully. |
|
![](templates/subSilver/images/spacer.gif) |
|