CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to convert two hex values into a 16 bit representation.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








How to convert two hex values into a 16 bit representation.
PostPosted: Wed Apr 15, 2009 10:22 pm     Reply with quote

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! Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 15, 2009 11:14 pm     Reply with quote

CCS has "make" functions which combine or extract bytes from larger
data types. To combine two bytes into a 16-bit value, use make16().
http://www.ccsinfo.com/forum/viewtopic.php?t=33689
Guest








PostPosted: Wed Apr 15, 2009 11:50 pm     Reply with quote

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! Very Happy
sapy44



Joined: 01 Feb 2004
Posts: 19
Location: Cedar Rapids, IA

View user's profile Send private message MSN Messenger

math solution
PostPosted: Thu Apr 16, 2009 3:21 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 16, 2009 4:06 pm     Reply with quote

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.
Quote:
1234

0034

1234


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

View user's profile Send private message

PostPosted: Fri Apr 17, 2009 7:22 am     Reply with quote

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

View user's profile Send private message Visit poster's website

Union with a structure - elegant and efficient
PostPosted: Mon Apr 20, 2009 2:38 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group