|
|
View previous topic :: View next topic |
Author |
Message |
dms Guest
|
Data conversion |
Posted: Wed Feb 05, 2003 1:38 am |
|
|
Hello to everyone I need some help here.
I've got a 2 byte data in this form:
bit15: Sign
bit14: 2^6
bit13: 2^5
bit12: 2^4
bit11: 2^3
bit10: 2^2
bit09: 2^1
bit08: 2^0
bit07: 2^-1
bit06: 2^-2
bit05: 2^-3
bit04: 2^-4
bit03: 0
bit02: 0
bit01: 0
bit00: 0
I want to convert a 2 byte data in this form to a float point data and save it in a variable. I need a calculation, if you want to know, this is related to the digital sensor DS1631.
Help me help me
thank you
___________________________
This message was ported from CCS's old forum
Original Post ID: 11287 |
|
|
R.J.Hamlett Guest
|
Re: Data conversion |
Posted: Wed Feb 05, 2003 2:57 am |
|
|
:=Hello to everyone I need some help here.
:=
:=I've got a 2 byte data in this form:
:=
:=bit15: Sign
:=bit14: 2^6
:=bit13: 2^5
:=bit12: 2^4
:=bit11: 2^3
:=bit10: 2^2
:=bit09: 2^1
:=bit08: 2^0
:=bit07: 2^-1
:=bit06: 2^-2
:=bit05: 2^-3
:=bit04: 2^-4
:=bit03: 0
:=bit02: 0
:=bit01: 0
:=bit00: 0
:=
:=I want to convert a 2 byte data in this form to a float point data and save it in a variable. I need a calculation, if you want to know, this is related to the digital sensor DS1631.
:=
:=Help me help me
This is just an integer, scaled /256.
Assuming your two bytes are held in a variable declared as a signed integer - if not, use a union, and have a signed int16 as one of the methods of accessing the data like :
union data {
int8 b[2];
signed int16 w;
} value;
Then you can access the individual characters as:
value.b[0], and value.b[1], and the whole 16bit word as value.w
Then just code as:
result=(float)value.w/256.0;
This 'casts' the integer into a float, and then divides this by 256 to give the correct result. However note that I am _forcing_ the operation to be done on a float, by using a float constant, and doing the conversion before the arithmetic (CCS C, has a tendency to try to save time, by converting latter...).
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 11289 |
|
|
dms Guest
|
Re: Data conversion |
Posted: Wed Feb 05, 2003 3:27 am |
|
|
:=:=Hello to everyone I need some help here.
:=:=
:=:=I've got a 2 byte data in this form:
:=:=
:=:=bit15: Sign
:=:=bit14: 2^6
:=:=bit13: 2^5
:=:=bit12: 2^4
:=:=bit11: 2^3
:=:=bit10: 2^2
:=:=bit09: 2^1
:=:=bit08: 2^0
:=:=bit07: 2^-1
:=:=bit06: 2^-2
:=:=bit05: 2^-3
:=:=bit04: 2^-4
:=:=bit03: 0
:=:=bit02: 0
:=:=bit01: 0
:=:=bit00: 0
:=:=
:=:=I want to convert a 2 byte data in this form to a float point data and save it in a variable. I need a calculation, if you want to know, this is related to the digital sensor DS1631.
:=:=
:=:=Help me help me
:=This is just an integer, scaled /256.
:=Assuming your two bytes are held in a variable declared as a signed integer - if not, use a union, and have a signed int16 as one of the methods of accessing the data like :
:=union data {
:= int8 b[2];
:= signed int16 w;
:=} value;
:=
:=Then you can access the individual characters as:
:=value.b[0], and value.b[1], and the whole 16bit word as value.w
:=
:=Then just code as:
:=result=(float)value.w/256.0;
:=
:=This 'casts' the integer into a float, and then divides this by 256 to give the correct result. However note that I am _forcing_ the operation to be done on a float, by using a float constant, and doing the conversion before the arithmetic (CCS C, has a tendency to try to save time, by converting latter...).
:=
:=Best Wishes
Works fine!
THANK YOU THANK YOU
___________________________
This message was ported from CCS's old forum
Original Post ID: 11292 |
|
|
R.J.Hamlett Guest
|
Re: Data conversion |
Posted: Wed Feb 05, 2003 4:50 am |
|
|
:=:=:=Hello to everyone I need some help here.
:=:=:=
:=:=:=I've got a 2 byte data in this form:
:=:=:=
:=:=:=bit15: Sign
:=:=:=bit14: 2^6
:=:=:=bit13: 2^5
:=:=:=bit12: 2^4
:=:=:=bit11: 2^3
:=:=:=bit10: 2^2
:=:=:=bit09: 2^1
:=:=:=bit08: 2^0
:=:=:=bit07: 2^-1
:=:=:=bit06: 2^-2
:=:=:=bit05: 2^-3
:=:=:=bit04: 2^-4
:=:=:=bit03: 0
:=:=:=bit02: 0
:=:=:=bit01: 0
:=:=:=bit00: 0
:=:=:=
:=:=:=I want to convert a 2 byte data in this form to a float point data and save it in a variable. I need a calculation, if you want to know, this is related to the digital sensor DS1631.
:=:=:=
:=:=:=Help me help me
:=:=This is just an integer, scaled /256.
:=:=Assuming your two bytes are held in a variable declared as a signed integer - if not, use a union, and have a signed int16 as one of the methods of accessing the data like :
:=:=union data {
:=:= int8 b[2];
:=:= signed int16 w;
:=:=} value;
:=:=
:=:=Then you can access the individual characters as:
:=:=value.b[0], and value.b[1], and the whole 16bit word as value.w
:=:=
:=:=Then just code as:
:=:=result=(float)value.w/256.0;
:=:=
:=:=This 'casts' the integer into a float, and then divides this by 256 to give the correct result. However note that I am _forcing_ the operation to be done on a float, by using a float constant, and doing the conversion before the arithmetic (CCS C, has a tendency to try to save time, by converting latter...).
:=:=
:=:=Best Wishes
:=
:=
:=Works fine!
:=
:=
:=THANK YOU THANK YOU
Glad to help.
You phrased your question clearly, and by thanking well, have made it a pleasure to help you.
Good luck on the rest of the project.
___________________________
This message was ported from CCS's old forum
Original Post ID: 11296 |
|
|
|
|
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
|