source: firmware/FSC/src/interpol.c@ 13217

Last change on this file since 13217 was 10753, checked in by neise, 14 years ago
working usb revision 10245 - modified for nice temperature printing command 'T'
File size: 11.0 KB
Line 
1//-----------------------------------------------------------------------------
2
3#include "interpol.h"
4#include "usart.h"
5#include "ad7719_adc.h"
6//#include "typedefs.h"
7//#include "application.h"
8#include "num_conversion.h"
9#include "math.h"
10//-----------------------------------------------------------------------------
11/*
12Temperatur Sensoren von IST nach DIN60751, Kl.B
13Messfehler +/- 0.30+0.005*|t|
14
15R(t) = R0(1 + At + Bt^2 + C(t-100)t^3) t = [-200, 0]°C
16R(t) = R0(1 + At + Bt^2) t = [0, 850]°C
17
18A = 3.9083*10^-3[°C^-1]
19B = -5.775*10^-7[°C^-2]
20C = -4.183*10^-12[°C^-4]
21
22Strom durch R ist konstant 400uA
23Intervallweise lineare Interpolation der Funktion R(t) mit MATLAB polyfit
24adcword = 13107200 * u(temperatur)
25*/
26 U08 space_STR[] = " |";
27 U08 OoR_STR[] = " OoR |";
28
29
30
31 float gettemp(void)
32 {
33
34
35 U8 temprange = 17;
36 U32 adcword=0;
37 float temp=-3.14; // should be initialized in a proper way, but I dont know how :-)
38 const U32 min = 0x404054;
39 const U32 max = 0x8CAF50;
40 const U32 s = 0x4C6F0; //(max-min)/16 = s(lice)
41
42 adcword = read_adc();
43
44 if (adcword >= (min + 8*s))
45 {
46 if (adcword >= (min + 12*s))
47 {
48 if (adcword >= (min + 14*s))
49 {
50 if (adcword >= (min + 15*s))
51 {
52 if (adcword < max)
53 {
54 temprange = 16;
55 }
56 }
57 else
58 {
59 temprange = 15;
60 }
61 }
62 else
63 {
64 if (adcword >= (min + 13*s))
65 {
66 temprange = 14;
67 }
68 else
69 {
70 temprange = 13;
71 }
72 }
73 }
74 else
75 {
76 if (adcword >= (min + 10*s))
77 {
78 if (adcword >= (min + 11*s))
79 {
80 temprange = 12;
81 }
82 else
83 {
84 temprange = 11;
85 }
86 }
87 else
88 {
89 if (adcword >= (min + 9*s))
90 {
91 temprange = 10;
92 }
93 else
94 {
95 temprange = 9;
96 }
97 }
98 }
99 }
100 else
101 {
102 if (adcword >= (min + 4*s))
103 {
104 if (adcword >= (min + 6*s))
105 {
106 if (adcword >= (min + 7*s))
107 {
108 temprange = 8;
109 }
110 else
111 {
112 temprange = 7;
113 }
114 }
115 else
116 {
117 if (adcword >= (min + 5*s))
118 {
119 temprange = 6;
120 }
121 else
122 {
123 temprange = 5;
124 }
125 }
126 }
127 else
128 {
129 if (adcword >= (min + 2*s))
130 {
131 if (adcword >= (min + 3*s))
132 {
133 temprange = 4;
134 }
135 else
136 {
137 temprange = 3;
138 }
139 }
140 else
141 {
142 if (adcword >= (min + s))
143 {
144 temprange = 2;
145 }
146 else
147 {
148 if (adcword > min)
149 {
150 temprange = 1;
151 }
152 }
153 }
154 }
155 }
156
157
158 switch (temprange)
159 {
160 case 1:{ // Temp. Range [-50°C , -34.375°C[
161 temp = ((float)adcword / 20764.727846 - 252.7721); //20764.727846 - 252.7721
162 }
163 break;
164
165 case 2:{ // Temp. Range [-34.375°C , -18.75°C[
166 temp = ((float)adcword / 20658.049789 - 253.8995); //20658.049789 - 253.8995
167 }
168 break;
169
170 case 3:{ // Temp. Range [-18.75°C , -3.125°C[
171 temp = ((float)adcword / 20557.997603 - 255.0436); //20557.997603 - 255.0436
172 }
173 break;
174
175 case 4:{ // Temp. Range [-3.125°C , 12.5°C]
176 temp = ((float)adcword / 20462.362624 - 256.2209); //20462.362624 - 256.2209
177 }
178 break;
179
180 case 5:{ // Temp. Range [12.5°C , 28.125°C]
181 temp = ((float)adcword / 20367.745024 - 257.4692); //20367.745024 - 257.4692
182 }
183 break;
184
185 case 6:{ // Temp. Range [28.125°C , 43.75°C]
186 temp = ((float)adcword / 20273.127424 - 258.8021); //20273.127424 - 258.8021
187 }
188 break;
189
190 case 7:{ // Temp. Range [43.75°C , 59.375°C]
191 temp = ((float)adcword / 20178.509824 - 260.2208); //20178.509824 - 260.2208
192 }
193 break;
194
195 case 8:{ // Temp. Range [59.375°C , 75°C]
196 temp = ((float)adcword / 20083.892224 - 261.7265); //20083.892224 - 261.7265
197 }
198 break;
199
200 case 9:{ // Temp. Range [75°C , 90.625°C]
201 temp = ((float)adcword / 19989.274624 - 263.3203); //19989.274624 - 263.3203
202 }
203 break;
204
205 case 10:{ // Temp. Range [90.625°C , 106.25°C]
206 temp = ((float)adcword / 19894.657024 - 265.0037); //19894.657024 - 265.0037
207 }
208 break;
209
210 case 11:{ // Temp. Range [106.25°C , 121.875°C]
211 temp = ((float)adcword / 19800.039424 - 266.7778); //19800.039424 - 266.7778
212 }
213 break;
214
215 case 12:{ // Temp. Range [121.875°C , 137.5°C]
216 temp = ((float)adcword / 19705.421824 - 268.6439); //19705.421824 - 268.6439
217 }
218 break;
219
220 case 13:{ // Temp. Range [137.5°C , 153.125°C]
221 temp = ((float)adcword / 19610.804224 - 270.6035); //19610.804224 - 270.6035
222 }
223 break;
224
225 case 14:{ // Temp. Range [153.125°C , 168.75°C]
226 temp = ((float)adcword / 19516.186624 - 272.6578); //19516.186624 - 272.6578
227 }
228 break;
229
230 case 15:{ // Temp. Range [168.75°C , 184.375°C]
231 temp = ((float)adcword / 19421.569024 - 274.8082); //19421.569024 - 274.8082
232 }
233 break;
234
235 case 16:{ // Temp. Range [184.375°C , 200°C]
236 temp = ((float)adcword / 19326.951424 - 277.0562); //19326.951424 - 277.0562
237 }
238 break;
239 default:{ // Temp. Range beyond [-50C , 200C]
240 PORTC = (PORTC | 0x04);
241 }
242 }// end of switch case statement
243 return temp;
244
245 }
246
247 void readandsendpress(void)
248 {
249 U32 adcword=0;
250 float press;
251
252 adcword = read_adc();
253
254 if ( (adcword > 0x253332) && (adcword < 0xF80000) ) // Press. Range is [0Bar , 250Bar]
255 {
256 press = ((float)adcword / (62 * 836.658790402)) - 63.0091; // 62 Ohm
257 usart_write_float(press,2,5);
258 usart_write_flash_str(space_STR);
259 }
260 else
261 {
262 usart_write_flash_str(OoR_STR); // Press. is beyond [0Bar , 250Bar]
263 PORTC = (PORTC | 0x04);
264 }
265
266 }
267
268
269/*
270Temperatur Sensoren von IST nach DIN60751, Kl.B
271Messfehler +/- 0.30+0.005*|t|
272
273R(t) = R0(1 + At + Bt^2 + C(t-100)t^3) t = [-200, 0]°C
274R(t) = R0(1 + At + Bt^2) t = [0, 850]°C
275
276A = 3.9083*10^-3[°C^-1]
277B = -5.775*10^-7[°C^-2]
278C = -4.183*10^-12[°C^-4]
279
280Strom durch R ist konstant 400uA
281Messung ist ratiometrisch bezogen auf Referenzwiderstand an ADC Pin5 (REFIN-) und Pin6 (REFIN+)
282Referenzwiderstand ist ca. 6.28kOhm.
283ADC hat internes gain von 2.
284
285*/
286
287float getresistance(void)
288{
289
290 U32 adcword=0;
291 float resistance;
292 const float R_REF=6.25; // kilo-ohms
293 adcword = read_adc();
294 U32 fullscale = 16777215L; //2^24 -1
295
296 resistance = ((float)adcword / (float)fullscale) /2.0 * R_REF; // divide through 2.0 because of PGA in ADC.
297
298 return resistance; // in kilo-ohms
299}
300
301U32 getadc(void)
302{
303return read_adc();
304}
305
306
307 float calc_temp(U32 adcword)
308 {
309 U8 temprange = 17;
310
311 float temp=NAN;
312 const U32 min = 0x404054;
313 const U32 max = 0x8CAF50;
314 const U32 s = 0x4C6F0; //(max-min)/16 = s(lice)
315
316 if (adcword >= (min + 8*s))
317 {
318 if (adcword >= (min + 12*s))
319 {
320 if (adcword >= (min + 14*s))
321 {
322 if (adcword >= (min + 15*s))
323 {
324 if (adcword < max)
325 {
326 temprange = 16;
327 }
328 }
329 else
330 {
331 temprange = 15;
332 }
333 }
334 else
335 {
336 if (adcword >= (min + 13*s))
337 {
338 temprange = 14;
339 }
340 else
341 {
342 temprange = 13;
343 }
344 }
345 }
346 else
347 {
348 if (adcword >= (min + 10*s))
349 {
350 if (adcword >= (min + 11*s))
351 {
352 temprange = 12;
353 }
354 else
355 {
356 temprange = 11;
357 }
358 }
359 else
360 {
361 if (adcword >= (min + 9*s))
362 {
363 temprange = 10;
364 }
365 else
366 {
367 temprange = 9;
368 }
369 }
370 }
371 }
372 else
373 {
374 if (adcword >= (min + 4*s))
375 {
376 if (adcword >= (min + 6*s))
377 {
378 if (adcword >= (min + 7*s))
379 {
380 temprange = 8;
381 }
382 else
383 {
384 temprange = 7;
385 }
386 }
387 else
388 {
389 if (adcword >= (min + 5*s))
390 {
391 temprange = 6;
392 }
393 else
394 {
395 temprange = 5;
396 }
397 }
398 }
399 else
400 {
401 if (adcword >= (min + 2*s))
402 {
403 if (adcword >= (min + 3*s))
404 {
405 temprange = 4;
406 }
407 else
408 {
409 temprange = 3;
410 }
411 }
412 else
413 {
414 if (adcword >= (min + s))
415 {
416 temprange = 2;
417 }
418 else
419 {
420 if (adcword > min)
421 {
422 temprange = 1;
423 }
424 }
425 }
426 }
427 }
428
429
430 switch (temprange)
431 {
432 case 1:{ // Temp. Range [-50°C , -34.375°C[
433 temp = ((float)adcword / 20764.727846 - 252.7721); //20764.727846 - 252.7721
434 }
435 break;
436
437 case 2:{ // Temp. Range [-34.375°C , -18.75°C[
438 temp = ((float)adcword / 20658.049789 - 253.8995); //20658.049789 - 253.8995
439 }
440 break;
441
442 case 3:{ // Temp. Range [-18.75°C , -3.125°C[
443 temp = ((float)adcword / 20557.997603 - 255.0436); //20557.997603 - 255.0436
444 }
445 break;
446
447 case 4:{ // Temp. Range [-3.125°C , 12.5°C]
448 temp = ((float)adcword / 20462.362624 - 256.2209); //20462.362624 - 256.2209
449 }
450 break;
451
452 case 5:{ // Temp. Range [12.5°C , 28.125°C]
453 temp = ((float)adcword / 20367.745024 - 257.4692); //20367.745024 - 257.4692
454 }
455 break;
456
457 case 6:{ // Temp. Range [28.125°C , 43.75°C]
458 temp = ((float)adcword / 20273.127424 - 258.8021); //20273.127424 - 258.8021
459 }
460 break;
461
462 case 7:{ // Temp. Range [43.75°C , 59.375°C]
463 temp = ((float)adcword / 20178.509824 - 260.2208); //20178.509824 - 260.2208
464 }
465 break;
466
467 case 8:{ // Temp. Range [59.375°C , 75°C]
468 temp = ((float)adcword / 20083.892224 - 261.7265); //20083.892224 - 261.7265
469 }
470 break;
471
472 case 9:{ // Temp. Range [75°C , 90.625°C]
473 temp = ((float)adcword / 19989.274624 - 263.3203); //19989.274624 - 263.3203
474 }
475 break;
476
477 case 10:{ // Temp. Range [90.625°C , 106.25°C]
478 temp = ((float)adcword / 19894.657024 - 265.0037); //19894.657024 - 265.0037
479 }
480 break;
481
482 case 11:{ // Temp. Range [106.25°C , 121.875°C]
483 temp = ((float)adcword / 19800.039424 - 266.7778); //19800.039424 - 266.7778
484 }
485 break;
486
487 case 12:{ // Temp. Range [121.875°C , 137.5°C]
488 temp = ((float)adcword / 19705.421824 - 268.6439); //19705.421824 - 268.6439
489 }
490 break;
491
492 case 13:{ // Temp. Range [137.5°C , 153.125°C]
493 temp = ((float)adcword / 19610.804224 - 270.6035); //19610.804224 - 270.6035
494 }
495 break;
496
497 case 14:{ // Temp. Range [153.125°C , 168.75°C]
498 temp = ((float)adcword / 19516.186624 - 272.6578); //19516.186624 - 272.6578
499 }
500 break;
501
502 case 15:{ // Temp. Range [168.75°C , 184.375°C]
503 temp = ((float)adcword / 19421.569024 - 274.8082); //19421.569024 - 274.8082
504 }
505 break;
506
507 case 16:{ // Temp. Range [184.375°C , 200°C]
508 temp = ((float)adcword / 19326.951424 - 277.0562); //19326.951424 - 277.0562
509 }
510 break;
511 default:
512 break;
513 }// end of switch case statement
514 return temp;
515
516 }
517
518
Note: See TracBrowser for help on using the repository browser.