Well this took a while. As so often the answer was to simplify the code.
Still, see if the result is what Microchip wants!
#include <string.h>
// The IEEE 754 format is seeeeeee-efffffff-ffffffff-ffffffff and Microchip format eeeeeeee-sfffffff-ffffffff-ffffffff .
byte *toCCS( byte *buf, float value )
{
//This is a routine to convert from IEEE to CCS. The inverse of the above.
byte signBit, switchBit;
memcpy( buf, &value, 4 );
signBit = bitRead( buf[ 3 ], 7 );
switchBit = bitRead( buf[ 2 ], 7 );
buf[ 3 ] = ( buf[ 3 ] << 1 ) | switchBit;
bitWrite( buf[ 2 ], 7, signBit );
return buf;
}
void setup( void )
{
float test = 12.5;
byte buffer[ 5 ];
byte *fptr = (byte *) &test;
byte idx;
Serial.begin( 9600 );
Serial.println();
Serial.println( test );
Serial.println( "\n float bytes in high to low order\n" );
for ( idx = 0; idx < 4; idx++ )
{
if ( *( fptr + 3 - idx ) < 0x10 )
{
Serial.print( "0" );
}
Serial.print((byte) *( fptr + 3 - idx ), HEX );
}
Serial.println( "\n *** \n" );
toCCS( buffer, test );
Serial.println( "\n converted bytes in high to low order\n" );
for ( idx = 0; idx < 4; idx++ )
{
if ( buffer[ 3 - idx ] < 0x10 )
{
Serial.print( "0" );
}
Serial.print( buffer[ 3 - idx ], HEX );
}
}
void loop( void )
{
}