View previous topic :: View next topic |
Author |
Message |
Guest
|
How to convert two hex values into a 16 bit representation. |
Posted: Wed Apr 15, 2009 10:22 pm |
|
|
Hi,
I am working on a ultrasonic sensor which returns its measure distance in two bytes or 16 bit hex form.
My problem is how can I be able to combine the two hex values that I retrieved from the sensor into a 16 bit hex form and determine its decimal value representation?
The decimal value representation is needed in the computation of the real value of distance measured. I am using a PIC18F26K20 MCU.
e.g.
raw value returned by the sensor:
0x21 - (hex1)
0xE5 - (hex2)
converting the two hex values will make it:
--->0x21E5 (which has 8,677 decimal representation)
THANK YOU IN ADVANCE! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Wed Apr 15, 2009 11:50 pm |
|
|
Thank you for your reply.
I already figured it out by scanning the CCS reference manual for possible solutions before i read your post and it works great! |
|
|
sapy44
Joined: 01 Feb 2004 Posts: 19 Location: Cedar Rapids, IA
|
math solution |
Posted: Thu Apr 16, 2009 3:21 pm |
|
|
X = first hex value
Y = second hex value
Z= combined hex value (16 bit varaible)
Z = 256*Y + X
or Z = (Y<<8) + X |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 16, 2009 4:06 pm |
|
|
There are problems with your code. The test program shown below
runs both your versions, and also the make16() version. Here are
the results. Your 2nd version fails.
Here is the ASM code produced by the compiler (vs. 4.090) for each one:
Code: | // Your first version:
.... z = 256*y + x;
003E: CLRF @@25
003F: MOVF x,W
0040: ADDWF @@25,W
0041: MOVWF z
0042: MOVF y,W
0043: MOVWF z+1
0044: BTFSC STATUS.C
0045: INCF z+1,F
// Your 2nd version:
.... z = (y<<8) + x;
005C: MOVLW 00
005D: ADDWF x,W
005E: MOVWF z
005F: CLRF z+1
// The make16(), which works, and has little code:
.... z = make16(y, x);
0076: MOVF y,W
0077: MOVWF z+1
0078: MOVF x,W
0079: MOVWF z
|
The test program:
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//================================
void main()
{
int8 x, y;
int16 z;
y = 0x12;
x = 0x34;
z = 256*y + x;
printf("%lx \n\r", z);
z = (y<<8) + x;
printf("%lx \n\r", z);
z = make16(y, x);
printf("%lx \n\r", z);
while(1);
} |
|
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Fri Apr 17, 2009 7:22 am |
|
|
I believe you would have to cast the y to a in16 before you shifted it; perhaps the same for the multiplication. |
|
|
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
Union with a structure - elegant and efficient |
Posted: Mon Apr 20, 2009 2:38 am |
|
|
I responded to another poster with a similar question a while back.
After playing around with various methods in the compiler, I found if you define a union, holding a int16 and a structure with two int8's you get very efficient conversion from two separate 8 bit reads into a 16 bit value.
Certainly much more elegant than bit shifts and oring.
The post is here, and my proven solution is the 5th post:
http://www.ccsinfo.com/forum/viewtopic.php?t=35913&highlight=
Cheers, Ray |
|
|
|