I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
example
input 3.14159
output 80490FD0

just need the conversion routine (in C). thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 10837

PCM programmer

Joined: 06 Sep 2003Posts: 20477

Re: Convert float number to PIC 4 byte hex

Posted: Mon Jan 20, 2003 4:39 pm

:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=example
:=input 3.14159
:=output 80490FD0
:=
:=just need the conversion routine (in C). thanks
-------------------------------------------------------
I found this by doing a search for "float" and "convert".
<a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10838

Neutone

Joined: 08 Sep 2003Posts: 839Location: Houston

Re: Convert float number to PIC 4 byte hex

Posted: Mon Jan 20, 2003 5:06 pm

:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=example
:=:=input 3.14159
:=:=output 80490FD0
:=:=
:=:=just need the conversion routine (in C). thanks
:=-------------------------------------------------------
:=I found this by doing a search for "float" and "convert".
:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a>

This one helps as well
<a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10841

This is not exactly what you are looking for but in some of my old archives I have these notes. I am not sure if I tested these but here is how you can very easily convert from the IEEE to PIC floating point format. This is PIC assembly;

IEEE to PIC floating point conversion
RLF MSB ;move most significant bit to carry
RLF EXP ;put bit at top of EXP and get sign bit in carry
RRF MSB ;move sign bit into MSB

IEEE to PIC floating point conversion
RLF MSB ;move sign bit to carry
RRF EXP ;put sign at top of EXP and get most significant bit in carry
RRF MSB ;move most significant bit into MSB

If you change your concept and convert in the PIC after receive from the PC this would work.

If you want I can also provide tested and working functions done in Visual Basic.

Also keep in mind there are some differences in the byte ordering, little endian vs big endian, depending on how you transfer the bytes between the PC and the PIC.

-Troy
___________________________
This message was ported from CCS's old forum
Original Post ID: 10908

Function tovb(inval As Single) As Single
'Here I have a 'single' comprising four bytes, that are from the remote system
'This needs to be reorganised to convert to the VB format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) / 2&
'Now retrieve the sign bit
sign = bytes(2) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127)
If (val And 128) Then
bytes(2) = bytes(2) Or 128
End If
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) Or sign
CopyMemory rval, bytes(0), 4
tovb = rval
End Function

Function toccs(inval As Single) As Single
'This is the reverse conversion to CCS format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) * 2&
'Now retrieve the sign bit
sign = bytes(3) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127) Or sign
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) And 255
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
CopyMemory rval, bytes(0), 4
toccs = rval
End Function
___________________________
This message was ported from CCS's old forum
Original Post ID: 10940

Function tovb(inval As Single) As Single
'Here I have a 'single' comprising four bytes, that are from the remote system
'This needs to be reorganised to convert to the VB format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) / 2&
'Now retrieve the sign bit
sign = bytes(2) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127)
If (val And 128) Then
bytes(2) = bytes(2) Or 128
End If
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) Or sign
CopyMemory rval, bytes(0), 4
tovb = rval
End Function

Function toccs(inval As Single) As Single
'This is the reverse conversion to CCS format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) * 2&
'Now retrieve the sign bit
sign = bytes(3) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127) Or sign
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) And 255
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
CopyMemory rval, bytes(0), 4
toccs = rval
End Function
___________________________
This message was ported from CCS's old forum
Original Post ID: 10941