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

Last change on this file since 10094 was 10094, checked in by neise, 14 years ago
initial commit of FSC firmware - use for FSC testing only -
File size: 6.6 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//-----------------------------------------------------------------------------
10/*
11Temperatur Sensoren von IST nach DIN60751, Kl.B
12Messfehler +/- 0.30+0.005*|t|
13
14R(t) = R0(1 + At + Bt^2 + C(t-100)t^3) t = [-200, 0]°C
15R(t) = R0(1 + At + Bt^2) t = [0, 850]°C
16
17A = 3.9083*10^-3[°C^-1]
18B = -5.775*10^-7[°C^-2]
19C = -4.183*10^-12[°C^-4]
20
21Strom durch R ist konstant 400uA
22Intervallweise lineare Interpolation der Funktion R(t) mit MATLAB polyfit
23adcword = 13107200 * u(temperatur)
24*/
25 U08 space_STR[] = " |";
26 U08 OoR_STR[] = " OoR |";
27
28
29
30 float gettemp(void)
31 {
32
33
34 U8 temprange = 17;
35 U32 adcword=0;
36 float temp=-3.14; // should be initialized in a proper way, but I dont know how :-)
37 const U32 min = 0x404054;
38 const U32 max = 0x8CAF50;
39 const U32 s = 0x4C6F0; //(max-min)/16 = s(lice)
40
41 adcword = read_adc();
42
43 if (adcword >= (min + 8*s))
44 {
45 if (adcword >= (min + 12*s))
46 {
47 if (adcword >= (min + 14*s))
48 {
49 if (adcword >= (min + 15*s))
50 {
51 if (adcword < max)
52 {
53 temprange = 16;
54 }
55 }
56 else
57 {
58 temprange = 15;
59 }
60 }
61 else
62 {
63 if (adcword >= (min + 13*s))
64 {
65 temprange = 14;
66 }
67 else
68 {
69 temprange = 13;
70 }
71 }
72 }
73 else
74 {
75 if (adcword >= (min + 10*s))
76 {
77 if (adcword >= (min + 11*s))
78 {
79 temprange = 12;
80 }
81 else
82 {
83 temprange = 11;
84 }
85 }
86 else
87 {
88 if (adcword >= (min + 9*s))
89 {
90 temprange = 10;
91 }
92 else
93 {
94 temprange = 9;
95 }
96 }
97 }
98 }
99 else
100 {
101 if (adcword >= (min + 4*s))
102 {
103 if (adcword >= (min + 6*s))
104 {
105 if (adcword >= (min + 7*s))
106 {
107 temprange = 8;
108 }
109 else
110 {
111 temprange = 7;
112 }
113 }
114 else
115 {
116 if (adcword >= (min + 5*s))
117 {
118 temprange = 6;
119 }
120 else
121 {
122 temprange = 5;
123 }
124 }
125 }
126 else
127 {
128 if (adcword >= (min + 2*s))
129 {
130 if (adcword >= (min + 3*s))
131 {
132 temprange = 4;
133 }
134 else
135 {
136 temprange = 3;
137 }
138 }
139 else
140 {
141 if (adcword >= (min + s))
142 {
143 temprange = 2;
144 }
145 else
146 {
147 if (adcword > min)
148 {
149 temprange = 1;
150 }
151 }
152 }
153 }
154 }
155
156
157 switch (temprange)
158 {
159 case 1:{ // Temp. Range [-50°C , -34.375°C[
160 temp = ((float)adcword / 20764.727846 - 252.7721); //20764.727846 - 252.7721
161 }
162 break;
163
164 case 2:{ // Temp. Range [-34.375°C , -18.75°C[
165 temp = ((float)adcword / 20658.049789 - 253.8995); //20658.049789 - 253.8995
166 }
167 break;
168
169 case 3:{ // Temp. Range [-18.75°C , -3.125°C[
170 temp = ((float)adcword / 20557.997603 - 255.0436); //20557.997603 - 255.0436
171 }
172 break;
173
174 case 4:{ // Temp. Range [-3.125°C , 12.5°C]
175 temp = ((float)adcword / 20462.362624 - 256.2209); //20462.362624 - 256.2209
176 }
177 break;
178
179 case 5:{ // Temp. Range [12.5°C , 28.125°C]
180 temp = ((float)adcword / 20367.745024 - 257.4692); //20367.745024 - 257.4692
181 }
182 break;
183
184 case 6:{ // Temp. Range [28.125°C , 43.75°C]
185 temp = ((float)adcword / 20273.127424 - 258.8021); //20273.127424 - 258.8021
186 }
187 break;
188
189 case 7:{ // Temp. Range [43.75°C , 59.375°C]
190 temp = ((float)adcword / 20178.509824 - 260.2208); //20178.509824 - 260.2208
191 }
192 break;
193
194 case 8:{ // Temp. Range [59.375°C , 75°C]
195 temp = ((float)adcword / 20083.892224 - 261.7265); //20083.892224 - 261.7265
196 }
197 break;
198
199 case 9:{ // Temp. Range [75°C , 90.625°C]
200 temp = ((float)adcword / 19989.274624 - 263.3203); //19989.274624 - 263.3203
201 }
202 break;
203
204 case 10:{ // Temp. Range [90.625°C , 106.25°C]
205 temp = ((float)adcword / 19894.657024 - 265.0037); //19894.657024 - 265.0037
206 }
207 break;
208
209 case 11:{ // Temp. Range [106.25°C , 121.875°C]
210 temp = ((float)adcword / 19800.039424 - 266.7778); //19800.039424 - 266.7778
211 }
212 break;
213
214 case 12:{ // Temp. Range [121.875°C , 137.5°C]
215 temp = ((float)adcword / 19705.421824 - 268.6439); //19705.421824 - 268.6439
216 }
217 break;
218
219 case 13:{ // Temp. Range [137.5°C , 153.125°C]
220 temp = ((float)adcword / 19610.804224 - 270.6035); //19610.804224 - 270.6035
221 }
222 break;
223
224 case 14:{ // Temp. Range [153.125°C , 168.75°C]
225 temp = ((float)adcword / 19516.186624 - 272.6578); //19516.186624 - 272.6578
226 }
227 break;
228
229 case 15:{ // Temp. Range [168.75°C , 184.375°C]
230 temp = ((float)adcword / 19421.569024 - 274.8082); //19421.569024 - 274.8082
231 }
232 break;
233
234 case 16:{ // Temp. Range [184.375°C , 200°C]
235 temp = ((float)adcword / 19326.951424 - 277.0562); //19326.951424 - 277.0562
236 }
237 break;
238 default:{ // Temp. Range beyond [-50C , 200C]
239 PORTC = (PORTC | 0x04);
240 }
241 }// end of switch case statement
242 return temp;
243
244 }
245
246 void readandsendpress(void)
247 {
248 U32 adcword=0;
249 float press;
250
251 adcword = read_adc();
252
253 if ( (adcword > 0x253332) && (adcword < 0xF80000) ) // Press. Range is [0Bar , 250Bar]
254 {
255 press = ((float)adcword / (62 * 836.658790402)) - 63.0091; // 62 Ohm
256 usart_write_float(press,2,5);
257 usart_write_flash_str(space_STR);
258 }
259 else
260 {
261 usart_write_flash_str(OoR_STR); // Press. is beyond [0Bar , 250Bar]
262 PORTC = (PORTC | 0x04);
263 }
264
265 }
266
267
268/*
269Temperatur Sensoren von IST nach DIN60751, Kl.B
270Messfehler +/- 0.30+0.005*|t|
271
272R(t) = R0(1 + At + Bt^2 + C(t-100)t^3) t = [-200, 0]°C
273R(t) = R0(1 + At + Bt^2) t = [0, 850]°C
274
275A = 3.9083*10^-3[°C^-1]
276B = -5.775*10^-7[°C^-2]
277C = -4.183*10^-12[°C^-4]
278
279Strom durch R ist konstant 400uA
280Messung ist ratiometrisch bezogen auf Referenzwiderstand an ADC Pin5 (REFIN-) und Pin6 (REFIN+)
281Referenzwiderstand ist ca. 6.28kOhm.
282ADC hat internes gain von 2.
283
284*/
285
286float getresistance(void)
287{
288
289 U32 adcword=0;
290 float resistance;
291 const float R_REF=6.28; // kilo-ohms
292 adcword = read_adc();
293 U32 fullscale = 16777215L; //2^24 -1
294
295 resistance = ((float)adcword / (float)fullscale) /2.0 * R_REF; // divide through 2.0 because of PGA in ADC.
296
297 return resistance; // in kilo-ohms
298}
299
300U32 getadc(void)
301{
302return read_adc();
303}
Note: See TracBrowser for help on using the repository browser.