Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdlib.h>
- #define FLOAT_VALUE 560.0
- #define TRUE 1
- #define FALSE 0
- void F2b(float UserFloat, int format);
- uint8_t Byte1, Byte2, Byte3, Byte4, be;
- FILE* destFile;
- void main (int argc, char **argv)
- {
- double val;
- val = atof(argv[2]);
- F2b(val,0);
- char buf[50];
- destFile = fopen(argv[1], "wb");
- sprintf (buf,"asm\(\"\\t psect eeprom_data,class=EEDATA\");\n");
- fwrite(buf, 1, strlen(buf), destFile);
- sprintf (buf,"asm\(\"\\t DB 0x%2.2X, 0x%2.2X, 0x%2.2X\");\n",Byte1,Byte2,Byte3);
- fwrite(buf, 1, strlen(buf), destFile);
- fclose(destFile);
- }
- //derived from http://www.piclist.com/techref/microchip/math/fpconvert.htm
- void F2b(float UserFloat, int format)
- {
- double x, z;
- unsigned long mantissa=0;
- int e, k;
- char *endstr, sign;
- endstr = "\0"; // used by strtod function
- // remember sign and keep positive
- if (UserFloat < 0)
- {
- sign = 1;
- UserFloat *= -1;
- }
- else sign = 0;
- // what format are we using?
- switch (format) {
- case 0 : // Hitech 24bit
- if (UserFloat == 0.0)
- {
- e = -127;
- Byte1 = 0;
- Byte2 = 0;
- Byte3 = 0;
- Byte4 = 0;
- mantissa = 1.0;
- }
- else
- {
- z = log(UserFloat) / log(2.0);
- e = (int)(z);
- if ((double)e > z)
- e --;
- be = e + 127;
- x = UserFloat / pow(2.0, (double)e);
- for (k=0; k>-15; k--)
- {
- if (x >= pow(2.0, k))
- {
- mantissa++; // set bit
- x -= pow(2.0, k); // update remainder
- }
- mantissa = mantissa << 1;
- }
- // fill byte values
- if (sign) Byte1 = 0x80;
- else Byte1 = 0;
- Byte1 += (be & 0xFE) >> 1;
- Byte2 = (be & 0x01) << 7;
- Byte2 += ((int)mantissa & 0x7F00) >> 8;
- Byte3 = (int)mantissa & 0xFF;
- Byte4 = 0;
- }
- break;
- case 1 : // IEEE754 32Bit
- if (UserFloat == 0.0)
- {
- e = -127;
- Byte1 = 0;
- Byte2 = 0;
- Byte3 = 0;
- Byte4 = 0;
- mantissa = 1.0;
- }
- else
- {
- z = log(UserFloat) / log(2.0);
- e = (int)(z);
- if ((double)e > z)
- e --;
- be = e + 127;
- x = UserFloat / pow(2.0, (double)e);
- for (k=0; k>-23; k--)
- {
- if (x >= pow(2.0, k))
- {
- mantissa++; // set bit
- x -= pow(2.0, k); // update remainder
- }
- mantissa = mantissa << 1;
- }
- // fill byte values
- if (sign) Byte1 = 0x80;
- else Byte1 = 0;
- Byte1 += (be & 0xFE) >> 1;
- Byte2 = (be & 0x01) << 7;
- Byte2 += ((int)mantissa & 0x7F0000) >> 16;
- Byte3 = ((int)mantissa & 0xFF00) >> 8;
- Byte4 = (int)mantissa & 0xFF;
- }
- break;
- case 2 : // Microchip 32Bit
- if (UserFloat == 0.0)
- {
- e = -127;
- Byte1 = 0;
- Byte2 = 0;
- Byte3 = 0;
- Byte4 = 0;
- mantissa = 1.0;
- }
- else
- {
- z = log(UserFloat) / log(2.0);
- e = (int)(z);
- if ((double)e > z)
- e --;
- be = e + 127;
- x = UserFloat / pow(2.0, (double)e);
- for (k=0; k>-23; k--)
- {
- if (x >= pow(2.0, k))
- {
- mantissa++; // set bit
- x -= pow(2.0, k); // update remainder
- }
- mantissa = mantissa << 1;
- }
- // fill byte values
- Byte1 = be;
- if (sign) Byte2 = 0x80;
- else Byte2 = 0;
- Byte2 += ((int)mantissa & 0x7F0000) >> 16;
- Byte3 = ((int)mantissa & 0xFF00) >> 8;
- Byte4 = (int)mantissa & 0xFF;
- }
- break;
- case 3 : // Microchip 24Bit
- if (UserFloat == 0.0)
- {
- e = -127;
- Byte1 = 0;
- Byte2 = 0;
- Byte3 = 0;
- Byte4 = 0;
- mantissa = 1.0;
- }
- else
- {
- z = log(UserFloat) / log(2.0);
- e = (int)(z);
- if ((double)e > z)
- e --;
- be = e + 127;
- x = UserFloat / pow(2.0, (double)e);
- for (k=0; k>-15; k--)
- {
- if (x >= pow(2.0, k))
- {
- mantissa++; // set bit
- x -= pow(2.0, k); // update remainder
- }
- mantissa = mantissa << 1;
- }
- // fill byte values
- Byte1 = be;
- if (sign) Byte2 = 0x80;
- else Byte2 = 0;
- Byte2 += ((int)mantissa & 0x7F00) >> 8;
- Byte3 = (int)mantissa & 0xFF;
- Byte4 = 0;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement