Advertisement
Guest User

Untitled

a guest
May 17th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8. #define FLOAT_VALUE 560.0
  9.  
  10.  
  11.  
  12.  
  13.  
  14. #define TRUE    1
  15. #define FALSE    0
  16.  
  17. void F2b(float UserFloat, int format);
  18. uint8_t Byte1, Byte2, Byte3, Byte4, be;
  19. FILE* destFile;
  20.  
  21.  
  22. void main (int argc, char **argv)
  23. {
  24. double val;
  25. val = atof(argv[2]);
  26. F2b(val,0);
  27. char buf[50];
  28. destFile = fopen(argv[1], "wb");
  29. sprintf (buf,"asm\(\"\\t psect eeprom_data,class=EEDATA\");\n");
  30. fwrite(buf, 1, strlen(buf), destFile);
  31. sprintf (buf,"asm\(\"\\t DB 0x%2.2X, 0x%2.2X, 0x%2.2X\");\n",Byte1,Byte2,Byte3);
  32. fwrite(buf, 1, strlen(buf), destFile);
  33. fclose(destFile);
  34. }
  35.  
  36.  
  37. //derived from http://www.piclist.com/techref/microchip/math/fpconvert.htm
  38.  
  39. void F2b(float UserFloat, int format)
  40. {
  41.  
  42.     double x, z;
  43.     unsigned long mantissa=0;
  44.     int e, k;
  45.     char *endstr, sign;
  46.  
  47.     endstr = "\0";      // used by strtod function
  48.  
  49.     // remember sign and keep positive
  50.     if (UserFloat < 0)
  51.     {
  52.         sign = 1;
  53.         UserFloat *= -1;
  54.     }
  55.     else sign = 0;
  56.  
  57.     // what format are we using?
  58.     switch (format) {
  59.  
  60.     case 0 :    // Hitech 24bit
  61.  
  62.         if (UserFloat == 0.0)
  63.         {
  64.             e = -127;
  65.             Byte1 = 0;
  66.             Byte2 = 0;
  67.             Byte3 = 0;
  68.             Byte4 = 0;
  69.             mantissa = 1.0;
  70.         }
  71.         else
  72.         {
  73.             z = log(UserFloat) / log(2.0);
  74.             e = (int)(z);
  75.             if ((double)e > z)
  76.                 e --;
  77.             be = e + 127;
  78.  
  79.             x = UserFloat / pow(2.0, (double)e);
  80.        
  81.             for (k=0; k>-15; k--)
  82.             {
  83.                 if (x >= pow(2.0, k))
  84.                 {
  85.                     mantissa++;     // set bit
  86.                     x -= pow(2.0, k);   // update remainder
  87.                 }
  88.                 mantissa = mantissa << 1;
  89.             }
  90.  
  91.             // fill byte values
  92.             if (sign) Byte1 = 0x80;
  93.             else Byte1 = 0;
  94.             Byte1 += (be & 0xFE) >> 1;
  95.             Byte2 = (be & 0x01) << 7;
  96.             Byte2 += ((int)mantissa & 0x7F00) >> 8;
  97.             Byte3 = (int)mantissa & 0xFF;
  98.             Byte4 = 0;
  99.         }
  100.  
  101.         break;
  102.  
  103.     case 1 :    // IEEE754 32Bit
  104.  
  105.         if (UserFloat == 0.0)
  106.         {
  107.             e = -127;
  108.             Byte1 = 0;
  109.             Byte2 = 0;
  110.             Byte3 = 0;
  111.             Byte4 = 0;
  112.             mantissa = 1.0;
  113.         }
  114.         else
  115.         {
  116.             z = log(UserFloat) / log(2.0);
  117.             e = (int)(z);
  118.             if ((double)e > z)
  119.             e --;
  120.             be = e + 127;
  121.  
  122.             x = UserFloat / pow(2.0, (double)e);
  123.        
  124.             for (k=0; k>-23; k--)
  125.             {
  126.                 if (x >= pow(2.0, k))
  127.                 {
  128.                     mantissa++;     // set bit
  129.                     x -= pow(2.0, k);   // update remainder
  130.                 }
  131.                 mantissa = mantissa << 1;
  132.             }
  133.  
  134.             // fill byte values
  135.             if (sign) Byte1 = 0x80;
  136.             else Byte1 = 0;
  137.             Byte1 += (be & 0xFE) >> 1;
  138.             Byte2 = (be & 0x01) << 7;
  139.             Byte2 += ((int)mantissa & 0x7F0000) >> 16;
  140.             Byte3 = ((int)mantissa & 0xFF00) >> 8;
  141.             Byte4 = (int)mantissa & 0xFF;
  142.         }
  143.  
  144.         break;
  145.  
  146.     case 2 :    // Microchip 32Bit
  147.  
  148.         if (UserFloat == 0.0)
  149.         {
  150.             e = -127;
  151.             Byte1 = 0;
  152.             Byte2 = 0;
  153.             Byte3 = 0;
  154.             Byte4 = 0;
  155.             mantissa = 1.0;
  156.         }
  157.         else
  158.         {
  159.             z = log(UserFloat) / log(2.0);
  160.             e = (int)(z);
  161.             if ((double)e > z)
  162.                 e --;
  163.             be = e + 127;
  164.  
  165.             x = UserFloat / pow(2.0, (double)e);
  166.        
  167.             for (k=0; k>-23; k--)
  168.             {
  169.                 if (x >= pow(2.0, k))
  170.                 {
  171.                     mantissa++;     // set bit
  172.                     x -= pow(2.0, k);   // update remainder
  173.                 }
  174.                 mantissa = mantissa << 1;
  175.             }
  176.  
  177.             // fill byte values
  178.             Byte1 = be;
  179.             if (sign) Byte2 = 0x80;
  180.             else Byte2 = 0;
  181.             Byte2 += ((int)mantissa & 0x7F0000) >> 16;
  182.             Byte3 = ((int)mantissa & 0xFF00) >> 8;
  183.             Byte4 = (int)mantissa & 0xFF;
  184.         }
  185.  
  186.         break;
  187.  
  188.     case 3 :    // Microchip 24Bit
  189.  
  190.         if (UserFloat == 0.0)
  191.         {
  192.             e = -127;
  193.             Byte1 = 0;
  194.             Byte2 = 0;
  195.             Byte3 = 0;
  196.             Byte4 = 0;
  197.             mantissa = 1.0;
  198.         }
  199.         else
  200.         {
  201.             z = log(UserFloat) / log(2.0);
  202.             e = (int)(z);
  203.             if ((double)e > z)
  204.                 e --;
  205.             be = e + 127;
  206.  
  207.             x = UserFloat / pow(2.0, (double)e);
  208.        
  209.             for (k=0; k>-15; k--)
  210.             {
  211.                 if (x >= pow(2.0, k))
  212.                 {
  213.                     mantissa++;     // set bit
  214.                     x -= pow(2.0, k);   // update remainder
  215.                 }
  216.                 mantissa = mantissa << 1;
  217.             }
  218.  
  219.             // fill byte values
  220.             Byte1 = be;
  221.             if (sign) Byte2 = 0x80;
  222.             else Byte2 = 0;
  223.             Byte2 += ((int)mantissa & 0x7F00) >> 8;
  224.             Byte3 = (int)mantissa & 0xFF;
  225.             Byte4 = 0;
  226.         }
  227.  
  228.         break;
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement