1 #include "xc.h"
  2 #include <p32xxxx.h>
  3 #include "MainBrain.h"
  4 #include <stdlib.h>
  5 #include <string.h>
  6 #include <math.h>
  7 
  8 /********************************************/
  9 /*float to ASCII BCD Conversion Code        */
 10 /*The fast and compact method by Cypress    */
 11 /*PIC32                                     */
 12 /********************************************/
 13 void Float2ASCIIBCD(float number, char* output)
 14 {
 15     if (number < 0) number = -number;           // handle negative numbers if needed
 16 
 17     uint8_t int_part = (uint8_t)number;         // get integer part (e.g., 3)
 18     float frac = number - int_part;             // get fractional part (e.g., 0.3)
 19 
 20     output[0] = '0' + int_part;                 // ASCII for integer digit
 21     output[1] = '.';                            // decimal point
 22     output[2] = '0' + (uint8_t)(frac * 10);     // one decimal digit as ASCII
 23 }
 24 
 25 /********************************************/
 26 /*Binary to ASCII BCD Conversion Code       */
 27 /*The fast and compact method by Cypress    */
 28 /*PIC32                                     */
 29 /********************************************/
 30 
 31 uint8_t d0, d1, d2, d3, d4;
 32 
 33 void Binary2ASCIIBCD(int bcd)
 34 {
 35     unsigned char q;
 36         
 37     //Find n0-n3 numbers
 38     d0 = bcd & 0xF;
 39     d1 = (bcd>>4) & 0xF;
 40     d2 = (bcd>>8) & 0xF;
 41     d3 = (bcd>>12) & 0xF;
 42     d4 = (bcd>>16) & 0xF;
 43     
 44     //Calculate d0-d4 numbers
 45     d0 = 6*(d3 + d2 + d1) + d0; 
 46     q = d0 / 10; 
 47     d0 = d0 % 10;     
 48     d1 = q + 9*d3 + 5*d2 + d1;
 49     q = d1 / 10;
 50     d1 = d1 % 10;
 51     d2 = q + 2*d2;
 52     q = d2 / 10;
 53     d2 = d2 % 10;
 54     d3 = q + 4*d3;
 55     q = d3 / 10;
 56     d3 = d3 % 10;
 57     d4 = q;
 58     
 59     //ASCII
 60     d0 = d0 + 48;
 61     d1 = d1 + 48;
 62     d2 = d2 + 48;
 63     d3 = d3 + 48;
 64     d4 = d4 + 48;
 65 }
 66 
 67 /****************************************************/
 68 /*32-bit Binary to ASCII Hexadecimal Conversion Code*/
 69 /*The fast and compact method by Cypress            */
 70 /*PIC32                                             */
 71 /****************************************************/
 72 
 73 int d_hex[8];
 74 
 75 void Binary2ASCIIHex(int i_hex)
 76 {
 77     unsigned t_hex;
 78     
 79     //digit 0
 80     t_hex = i_hex;
 81     d_hex[0] = t_hex & 0xf;
 82     
 83     //digit 1
 84     i_hex = i_hex>>4;
 85     t_hex = i_hex;
 86     d_hex[1] = t_hex & 0xf;
 87 
 88     //digit 2
 89     i_hex = i_hex>>4;
 90     t_hex = i_hex;
 91     d_hex[2] = t_hex & 0xf;
 92 
 93     //digit 3
 94     i_hex = i_hex>>4;
 95     t_hex = i_hex;
 96     d_hex[3] = t_hex & 0xf;
 97 
 98     //digit 4
 99     i_hex = i_hex>>4;
100     t_hex = i_hex;
101     d_hex[4] = t_hex & 0xf;
102 
103     //digit 5
104     i_hex = i_hex>>4;
105     t_hex = i_hex;
106     d_hex[5] = t_hex & 0xf;
107 
108     //digit 6
109     i_hex = i_hex>>4;
110     t_hex = i_hex;
111     d_hex[6] = t_hex & 0xf;
112 
113     //digit 7
114     i_hex = i_hex>>4;
115     t_hex = i_hex;
116     d_hex[7] = t_hex & 0xf;
117 
118     //hex digits are separated
119     //now convert to ascii
120     int h;
121     
122     for(h=0;h<=7;h++)
123     {
124         if(d_hex[h] > 9)
125         {
126             d_hex[h] = d_hex[h] + 87;
127         }
128         else
129         {
130             d_hex[h] = d_hex[h] + 48;
131         }
132     }
133     
134     return;
135 }
136