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
10
11
12
13 void Float2ASCIIBCD(float number, char* output)
14 {
15 if (number < 0) number = -number;
16
17 uint8_t int_part = (uint8_t)number;
18 float frac = number - int_part;
19
20 output[0] = '0' + int_part;
21 output[1] = '.';
22 output[2] = '0' + (uint8_t)(frac * 10);
23 }
24
25
26
27
28
29
30
31 uint8_t d0, d1, d2, d3, d4;
32
33 void Binary2ASCIIBCD(int bcd)
34 {
35 unsigned char q;
36
37
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
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
60 d0 = d0 + 48;
61 d1 = d1 + 48;
62 d2 = d2 + 48;
63 d3 = d3 + 48;
64 d4 = d4 + 48;
65 }
66
67
68
69
70
71
72
73 int d_hex[8];
74
75 void Binary2ASCIIHex(int i_hex)
76 {
77 unsigned t_hex;
78
79
80 t_hex = i_hex;
81 d_hex[0] = t_hex & 0xf;
82
83
84 i_hex = i_hex>>4;
85 t_hex = i_hex;
86 d_hex[1] = t_hex & 0xf;
87
88
89 i_hex = i_hex>>4;
90 t_hex = i_hex;
91 d_hex[2] = t_hex & 0xf;
92
93
94 i_hex = i_hex>>4;
95 t_hex = i_hex;
96 d_hex[3] = t_hex & 0xf;
97
98
99 i_hex = i_hex>>4;
100 t_hex = i_hex;
101 d_hex[4] = t_hex & 0xf;
102
103
104 i_hex = i_hex>>4;
105 t_hex = i_hex;
106 d_hex[5] = t_hex & 0xf;
107
108
109 i_hex = i_hex>>4;
110 t_hex = i_hex;
111 d_hex[6] = t_hex & 0xf;
112
113
114 i_hex = i_hex>>4;
115 t_hex = i_hex;
116 d_hex[7] = t_hex & 0xf;
117
118
119
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