|
|
View previous topic :: View next topic |
Author |
Message |
muhibraza1
Joined: 26 Jul 2013 Posts: 23
|
IEEE float to Microchip Float Conversion in CCS C |
Posted: Fri Sep 19, 2014 11:38 pm |
|
|
Hi Everyone !
I have got a C routine that converts IEEE float into Microchip float. Can someone explain me about how it works ? I am facing trouble in understanding it since I haven't written this routine by myself. Basically it is supposed to receive four bytes that have been concatenated together with the make32() built in CCS function so it actually takes the int32 as input. these four bytes are to be received through MODBUS protocol but thats another topic. I just need to know about the conversion. And if someone could suggest any other routine for the same purpose or just optimize this one that would also be appreciated. Thanks !
Here is the code !
Code: | float f_IEEEtoPIC(int32 f)
{
float ret;
int16 temp;
memcpy(&temp, ((int8*)&f)+2, 2);
temp = ((temp << 1) & 0xFF00) + (temp & 0xFF);
if(bit_test(f, 31)) // Test the sign bit
temp |= 0x0080;
else
temp &= 0xFF7F;
memcpy(((int8*)&ret)+3, ((int8*)&f) , 1);
memcpy(((int8*)&ret)+2, ((int8*)&f)+1 , 1);
memcpy(((int8*)&ret)+1, ((int8*)&temp) , 1);
memcpy(((int8*)&ret) , ((int8*)&temp)+1, 1);
return ret;
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sat Sep 20, 2014 12:36 am |
|
|
A far more optimised version, comes with the compiler.....
Look at the file ieeefloat.c in the drivers directory.
This has versions optimised for each different chip family.
The version you have will be very slow. Pointer maths is quite laborious on the PIC, and the code you have fiddles with all four bytes, when only two actually have to be moved.
Some years ago (before CCS wrote their version), I wrote a version, and the comments in this may well answer your question:
Code: |
void toccs(int *Value) //pointer to 32bit value
{
//program to convert a 4 bytes 'word' from IEEE to CCS format
//difference is where the 'sign' bit is stored. CCS's layout is based on
//maths routines published by Microchip, when the PIC was first launched
//and uses byte 1 as the 8bit exponent (offset by 0x7F), followed by a sign
//and 23bit mantissa. This layout was optimised to suit the maths operations
//available in the PIC. The IEEE format instead has the sign as the first bit
//then the 8bit exponent, then the 23bit mantissa. So to convert one has to
//extract the sign bit 'out' of the second byte, move the exponent left one
//bit into this space, and then put the sign back into the first bit.
int1 sign;
sign = shift_left(&Value[1],1,0); //extract sign
sign = shift_left(&Value[0],1,sign); //move to new location, and extract
//bottom bit of exponent
shift_right(&Value[1],1,sign); //put this back in place of the sign bit
}
|
The CCS version is much more optimised, but the basic operation is the same:
Code: |
xxxxxxxx smmmmmmm mmmmmmmm mmmmmmmm
sxxxxxxx xmmmmmmm mmmmmmmm mmmmmmmm
|
The first line is the CCS format, the second is IEEE. |
|
|
muhibraza1
Joined: 26 Jul 2013 Posts: 23
|
|
Posted: Sat Sep 20, 2014 4:51 am |
|
|
Thank you very much Ttelmah ! |
|
|
|
|
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
|