Last active
August 29, 2015 14:07
-
-
Save Rhomboid/ac80f00a047e3bc2c89b to your computer and use it in GitHub Desktop.
Scaling 0-255 to 0.00-5.00 for a microcontroller with only 8 bit operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
static const unsigned char lookup[][5] = { { 0, 0, 1, 9, 6 }, | |
{ 0, 0, 3, 9, 2 }, | |
{ 0, 0, 7, 8, 4 }, | |
{ 0, 1, 5, 6, 9 }, | |
{ 0, 3, 1, 3, 7 }, | |
{ 0, 6, 2, 7, 5 }, | |
{ 1, 2, 5, 4, 9 }, | |
{ 2, 5, 0, 9, 8 } }, | |
rounding_helper[5] = { 0, 0, 1, 0, 0 }; | |
void add_bcd5(unsigned char *dest, const unsigned char *src) | |
{ | |
unsigned char digit = 4, sum, carry = 0; | |
for(;;) { | |
sum = src[digit] + dest[digit] + carry; | |
if(sum > 9) { | |
dest[digit] = sum - 10; | |
carry = 1; | |
} else { | |
dest[digit] = sum; | |
carry = 0; | |
} | |
if(!digit) | |
break; | |
--digit; | |
} | |
} | |
void scale_it(unsigned char val, unsigned char *output) | |
{ | |
memset(output, 0, 5); | |
for(unsigned char bit = 0, mask = 1; bit < 8; bit++, mask <<= 1) | |
if(val & mask) | |
add_bcd5(output, lookup[bit]); | |
/* round to three places */ | |
if(output[3] >= 5) | |
add_bcd5(output, rounding_helper); | |
} | |
int main(void) | |
{ | |
unsigned char output[6]; | |
for(unsigned char val = 0; ; val++) { | |
scale_it(val, output); | |
printf("%3d %c.%c%c\n", val, output[0] + '0', output[1] + '0', output[2] + '0'); | |
if(val == 255) | |
break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 0.00 | |
1 0.02 | |
2 0.04 | |
3 0.06 | |
4 0.08 | |
5 0.10 | |
6 0.12 | |
7 0.14 | |
8 0.16 | |
9 0.18 | |
10 0.20 | |
11 0.22 | |
12 0.24 | |
13 0.25 | |
14 0.27 | |
15 0.29 | |
16 0.31 | |
17 0.33 | |
18 0.35 | |
19 0.37 | |
20 0.39 | |
21 0.41 | |
22 0.43 | |
23 0.45 | |
24 0.47 | |
25 0.49 | |
26 0.51 | |
27 0.53 | |
28 0.55 | |
29 0.57 | |
30 0.59 | |
31 0.61 | |
32 0.63 | |
33 0.65 | |
34 0.67 | |
35 0.69 | |
36 0.71 | |
37 0.73 | |
38 0.75 | |
39 0.76 | |
40 0.78 | |
41 0.80 | |
42 0.82 | |
43 0.84 | |
44 0.86 | |
45 0.88 | |
46 0.90 | |
47 0.92 | |
48 0.94 | |
49 0.96 | |
50 0.98 | |
51 1.00 | |
52 1.02 | |
53 1.04 | |
54 1.06 | |
55 1.08 | |
56 1.10 | |
57 1.12 | |
58 1.14 | |
59 1.16 | |
60 1.18 | |
61 1.20 | |
62 1.22 | |
63 1.24 | |
64 1.25 | |
65 1.27 | |
66 1.29 | |
67 1.31 | |
68 1.33 | |
69 1.35 | |
70 1.37 | |
71 1.39 | |
72 1.41 | |
73 1.43 | |
74 1.45 | |
75 1.47 | |
76 1.49 | |
77 1.51 | |
78 1.53 | |
79 1.55 | |
80 1.57 | |
81 1.59 | |
82 1.61 | |
83 1.63 | |
84 1.65 | |
85 1.67 | |
86 1.69 | |
87 1.71 | |
88 1.73 | |
89 1.75 | |
90 1.76 | |
91 1.78 | |
92 1.80 | |
93 1.82 | |
94 1.84 | |
95 1.86 | |
96 1.88 | |
97 1.90 | |
98 1.92 | |
99 1.94 | |
100 1.96 | |
101 1.98 | |
102 2.00 | |
103 2.02 | |
104 2.04 | |
105 2.06 | |
106 2.08 | |
107 2.10 | |
108 2.12 | |
109 2.14 | |
110 2.16 | |
111 2.18 | |
112 2.20 | |
113 2.22 | |
114 2.24 | |
115 2.25 | |
116 2.27 | |
117 2.29 | |
118 2.31 | |
119 2.33 | |
120 2.35 | |
121 2.37 | |
122 2.39 | |
123 2.41 | |
124 2.43 | |
125 2.45 | |
126 2.47 | |
127 2.49 | |
128 2.51 | |
129 2.53 | |
130 2.55 | |
131 2.57 | |
132 2.59 | |
133 2.61 | |
134 2.63 | |
135 2.65 | |
136 2.67 | |
137 2.69 | |
138 2.71 | |
139 2.73 | |
140 2.75 | |
141 2.76 | |
142 2.78 | |
143 2.80 | |
144 2.82 | |
145 2.84 | |
146 2.86 | |
147 2.88 | |
148 2.90 | |
149 2.92 | |
150 2.94 | |
151 2.96 | |
152 2.98 | |
153 3.00 | |
154 3.02 | |
155 3.04 | |
156 3.06 | |
157 3.08 | |
158 3.10 | |
159 3.12 | |
160 3.14 | |
161 3.16 | |
162 3.18 | |
163 3.20 | |
164 3.22 | |
165 3.24 | |
166 3.25 | |
167 3.27 | |
168 3.29 | |
169 3.31 | |
170 3.33 | |
171 3.35 | |
172 3.37 | |
173 3.39 | |
174 3.41 | |
175 3.43 | |
176 3.45 | |
177 3.47 | |
178 3.49 | |
179 3.51 | |
180 3.53 | |
181 3.55 | |
182 3.57 | |
183 3.59 | |
184 3.61 | |
185 3.63 | |
186 3.65 | |
187 3.67 | |
188 3.69 | |
189 3.71 | |
190 3.73 | |
191 3.75 | |
192 3.76 | |
193 3.78 | |
194 3.80 | |
195 3.82 | |
196 3.84 | |
197 3.86 | |
198 3.88 | |
199 3.90 | |
200 3.92 | |
201 3.94 | |
202 3.96 | |
203 3.98 | |
204 4.00 | |
205 4.02 | |
206 4.04 | |
207 4.06 | |
208 4.08 | |
209 4.10 | |
210 4.12 | |
211 4.14 | |
212 4.16 | |
213 4.18 | |
214 4.20 | |
215 4.22 | |
216 4.24 | |
217 4.25 | |
218 4.27 | |
219 4.29 | |
220 4.31 | |
221 4.33 | |
222 4.35 | |
223 4.37 | |
224 4.39 | |
225 4.41 | |
226 4.43 | |
227 4.45 | |
228 4.47 | |
229 4.49 | |
230 4.51 | |
231 4.53 | |
232 4.55 | |
233 4.57 | |
234 4.59 | |
235 4.61 | |
236 4.63 | |
237 4.65 | |
238 4.67 | |
239 4.69 | |
240 4.71 | |
241 4.73 | |
242 4.75 | |
243 4.76 | |
244 4.78 | |
245 4.80 | |
246 4.82 | |
247 4.84 | |
248 4.86 | |
249 4.88 | |
250 4.90 | |
251 4.92 | |
252 4.94 | |
253 4.96 | |
254 4.98 | |
255 5.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment