1 | //----------------------------------------------------------------------------- |
---|
2 | #include "typedefs.h" |
---|
3 | #include "application.h" |
---|
4 | #include "spi_master.h" |
---|
5 | #include "ad7719_adc.h" |
---|
6 | #include "atmega_adc.h" |
---|
7 | #include "usart.h" |
---|
8 | #include "macros.h" |
---|
9 | #include "interpol.h" |
---|
10 | #include "w5100_spi_interface.h" |
---|
11 | #include <avr/interrupt.h> |
---|
12 | #include <avr/wdt.h> |
---|
13 | #include <stdlib.h> |
---|
14 | |
---|
15 | |
---|
16 | //----------------------------------------------------------------------------- |
---|
17 | // definition of some functions: |
---|
18 | // these function are implemented below the main() |
---|
19 | // here in FSC.c |
---|
20 | // |
---|
21 | // sooner or later, they will be moved into more apropriate files. |
---|
22 | U08 increase_adc (U08 channel); |
---|
23 | U08 increase_ad7719 (U08 channel); |
---|
24 | void Set_V_Muxer (U08 channel); |
---|
25 | void Set_T_Muxer(U08 channel); |
---|
26 | void talk(void);// never used up to now. |
---|
27 | void ad7719_output(U08 channel, U32 data); |
---|
28 | void adc_output(U08 channel, U08 data); |
---|
29 | void adc_output_all(); |
---|
30 | void parse(); //doesn't do anything at the moment |
---|
31 | void check_if_measured_all() ; |
---|
32 | // end of function definition: |
---|
33 | //----------------------------------------------------------------------------- |
---|
34 | |
---|
35 | // MAIN WORKFLOW GLOBAL VARIABLES |
---|
36 | bool verbose; |
---|
37 | bool heartbeat_enable; |
---|
38 | |
---|
39 | // USART global variables |
---|
40 | U08 usart_rx_buffer[USART_RX_BUFFER_SIZE]; |
---|
41 | U08 usart_tx_buffer[USART_TX_BUFFER_SIZE]; |
---|
42 | U08 usart_rx_buffer_index = 0; |
---|
43 | U08 usart_tx_buffer_index = 0; |
---|
44 | U08 usart_last_char; // last received char |
---|
45 | |
---|
46 | // USART FLAGS |
---|
47 | bool usart_tx_buffer_overflow = false; // true if usart_tx_buffer was full. |
---|
48 | bool usart_rx_ready = false; // EOL was received, parser needs to be called |
---|
49 | |
---|
50 | // TIMER global variable |
---|
51 | volatile U32 local_ms = 0; |
---|
52 | |
---|
53 | // AD7719 global variables |
---|
54 | #define TEMP_CHANNELS 64 |
---|
55 | #define CHANNEL_BITMAP 8 |
---|
56 | #define AD7719_READINGS_UNTIL_SETTLED 1 // bei3:480ms |
---|
57 | U32 ad7719_values[TEMP_CHANNELS]; |
---|
58 | U08 ad7719_enables[CHANNEL_BITMAP]; |
---|
59 | U08 ad7719_channels_ready[CHANNEL_BITMAP]; |
---|
60 | U08 ad7719_readings_since_last_muxing = 0; |
---|
61 | U08 ad7719_current_channel = 0; |
---|
62 | U32 ad7719_current_reading = 0; |
---|
63 | bool ad7719_measured_all = false; |
---|
64 | |
---|
65 | // ATMEGA ADC global variables |
---|
66 | #define V_CHANNELS 40 |
---|
67 | #define I_CHANNELS 40 |
---|
68 | #define H_CHANNELS 4 |
---|
69 | #define V_BITMAP 5 |
---|
70 | #define I_BITMAP 5 |
---|
71 | #define H_BITMAP 1 |
---|
72 | #define ADC_READINGS_UNTIL_SETTLED 1 |
---|
73 | U32 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS]; // stores measured voltage in steps of 16mV |
---|
74 | U08 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP]; |
---|
75 | U08 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP]; |
---|
76 | U08 adc_readings_since_last_muxing = 0; |
---|
77 | U08 adc_current_channel = 0; |
---|
78 | U08 adc_current_reading = 0; |
---|
79 | bool adc_measured_all = false; |
---|
80 | |
---|
81 | //----------------------------------------------------------------------------- |
---|
82 | // M A I N --- M A I N --- M A I N --- M A I N --- M A I N |
---|
83 | //----------------------------------------------------------------------------- |
---|
84 | int main(void) |
---|
85 | { |
---|
86 | app_init(); // Setup: Watchdog and I/Os |
---|
87 | usart_init(); // Initialize serial interface |
---|
88 | spi_init(); // Initialize SPI interface as master |
---|
89 | ad7719_init(); // Initialize AD7719 ADC as SPI slave |
---|
90 | atmega_adc_init(); |
---|
91 | |
---|
92 | // TIMER2 is used as local clock: |
---|
93 | // configure timer 2 |
---|
94 | TCCR2 = (1<<WGM21); // CTC Modus |
---|
95 | TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us |
---|
96 | OCR2 = 125-1; // --> output compare interrupt occurs every 125 x 8us = 1ms |
---|
97 | // Compare Interrupt erlauben |
---|
98 | TIMSK |= (1<<OCIE2); |
---|
99 | |
---|
100 | // Enable interrupts |
---|
101 | sei(); |
---|
102 | |
---|
103 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) { |
---|
104 | ad7719_enables[i]=0xFF; |
---|
105 | ad7719_channels_ready[i]=0; |
---|
106 | } |
---|
107 | for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) { |
---|
108 | adc_enables[i]=0xFF; |
---|
109 | adc_channels_ready[i]=0; |
---|
110 | } |
---|
111 | static U08 welcome[]="\n\nwelcome to FACT FSC commandline interface v0.1\nready?"; |
---|
112 | usart_write_str(welcome); |
---|
113 | |
---|
114 | |
---|
115 | //MAIN LOOP |
---|
116 | while (1) |
---|
117 | { |
---|
118 | if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat |
---|
119 | //---------------------------------------------------------------------------- |
---|
120 | //IF we need to send away one byte, and ready to send |
---|
121 | |
---|
122 | if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) { |
---|
123 | UDR = usart_tx_buffer[0]; |
---|
124 | // THis is shit |
---|
125 | for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) { |
---|
126 | usart_tx_buffer[i] = usart_tx_buffer[i+1]; |
---|
127 | } |
---|
128 | usart_tx_buffer_index--; |
---|
129 | } |
---|
130 | //---------------------------------------------------------------------------- |
---|
131 | |
---|
132 | //IF we just received one byte, and there is enough space in the RX_buffer |
---|
133 | if ( (UCSRA & (1<<RXC)) && (usart_rx_buffer_index < USART_RX_BUFFER_SIZE) ){ |
---|
134 | usart_last_char = UDR; |
---|
135 | if (usart_last_char == '\n'){ // if EOL was received |
---|
136 | usart_rx_ready = true; |
---|
137 | }else { |
---|
138 | usart_rx_buffer[usart_rx_buffer_index] = usart_last_char; |
---|
139 | usart_rx_buffer_index++; |
---|
140 | } |
---|
141 | // here is still something strange .... better send an enter automatically |
---|
142 | } else if (UCSRA & (1<<RXC)) { // if there is no scace in the buffer; read anyway. |
---|
143 | usart_last_char = UDR; |
---|
144 | usart_rx_buffer_index =0; |
---|
145 | } |
---|
146 | //---------------------------------------------------------------------------- |
---|
147 | |
---|
148 | //IF USART DOR bit is set, PC is sending data to fast!!! |
---|
149 | if ( UCSRA & (1<<DOR) ){ |
---|
150 | // flush TX_buffer and write warning message in |
---|
151 | // maybe even switch off every measurement. ? |
---|
152 | } |
---|
153 | //---------------------------------------------------------------------------- |
---|
154 | |
---|
155 | //IF TX_BUFFER was overrun. |
---|
156 | if (usart_tx_buffer_overflow) { |
---|
157 | // flash TX_buffer and write warning message in |
---|
158 | // maybe even switch off every measurement. ? |
---|
159 | // |
---|
160 | // this should only happen, in verbose mode and with low baudrates. |
---|
161 | } |
---|
162 | //---------------------------------------------------------------------------- |
---|
163 | |
---|
164 | //IF one command was received. |
---|
165 | // -It is not allowed to send more than one command between two '\n' |
---|
166 | if (usart_rx_ready){ |
---|
167 | parse(); |
---|
168 | usart_rx_buffer_index = 0; |
---|
169 | usart_rx_ready = false; |
---|
170 | } |
---|
171 | //---------------------------------------------------------------------------- |
---|
172 | |
---|
173 | //IF ATmega internal ADC did finish a conversion --every 200us |
---|
174 | if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) { |
---|
175 | adc_current_reading = ADCH; |
---|
176 | if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED) { |
---|
177 | adc_values[adc_current_channel] = adc_current_reading; |
---|
178 | adc_readings_since_last_muxing=0; |
---|
179 | // note that this channel is ready, now and |
---|
180 | adc_output(adc_current_channel, adc_current_reading); |
---|
181 | // proceed to the next enabled channel. |
---|
182 | adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8)); |
---|
183 | adc_current_channel = increase_adc (adc_current_channel); |
---|
184 | Set_V_Muxer(adc_current_channel); |
---|
185 | } else { // the ADC did not settle yet, we discard the reading |
---|
186 | ++adc_readings_since_last_muxing; |
---|
187 | // current reading is not used for anything else |
---|
188 | } |
---|
189 | } |
---|
190 | //---------------------------------------------------------------------------- |
---|
191 | |
---|
192 | //IF AD7719 ADC just finished a conversion -- every 60ms |
---|
193 | |
---|
194 | if (AD7719_IS_READY()) { |
---|
195 | ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us |
---|
196 | // AD7719 is only read out if settled. saves time. |
---|
197 | if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED) { |
---|
198 | ad7719_values[ad7719_current_channel] = ad7719_current_reading; |
---|
199 | ad7719_readings_since_last_muxing=0; |
---|
200 | // now prepare the data to be send away via USART. |
---|
201 | //ad7719_output(ad7719_current_channel, ad7719_current_reading); |
---|
202 | // note that this channel is ready, now and |
---|
203 | // proceed to the next enabled channel. |
---|
204 | ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8)); |
---|
205 | ad7719_current_channel = increase_ad7719 (ad7719_current_channel); |
---|
206 | Set_T_Muxer(ad7719_current_channel); |
---|
207 | } else { // the AD7719 did not settle yet, we discard the reading |
---|
208 | ++ad7719_readings_since_last_muxing; |
---|
209 | |
---|
210 | // current reading is not used for anything else |
---|
211 | } |
---|
212 | } |
---|
213 | //---------------------------------------------------------------------------- |
---|
214 | //IF one of the ADC measured all channels, we wanted to know. |
---|
215 | check_if_measured_all(); |
---|
216 | |
---|
217 | if (ad7719_measured_all && adc_measured_all) |
---|
218 | adc_output_all(); |
---|
219 | |
---|
220 | //---------------------------------------------------------------------------- |
---|
221 | /* |
---|
222 | if (verbose == true) |
---|
223 | // talk() was just defined so the |
---|
224 | // code is not at this place ... look down. |
---|
225 | talk(); |
---|
226 | */ |
---|
227 | |
---|
228 | } // end of MAIN LOOP |
---|
229 | //----------------------------------------------------------------------------- |
---|
230 | // E N D E N D E N D E N D E N D E N D E N D |
---|
231 | //----------------------------------------------------------------------------- |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | float resistance; |
---|
236 | |
---|
237 | U08 SA_mux_val = 0x00; |
---|
238 | U08 SB_mux_val = 0x00; |
---|
239 | |
---|
240 | //U08 counter = 0; |
---|
241 | U08 Res_or_Volt = 0x00; |
---|
242 | |
---|
243 | |
---|
244 | while (TRUE) |
---|
245 | { |
---|
246 | |
---|
247 | |
---|
248 | ++Res_or_Volt; |
---|
249 | if (Res_or_Volt <= 64){ |
---|
250 | |
---|
251 | |
---|
252 | // if USART data arrives. i.e. data via USB |
---|
253 | // the usart_rx_ready flag is set TRUE |
---|
254 | // now process the incoming data which is stored in |
---|
255 | // U08 usart_rx_buffer[USART_RX_BUFFER_SIZE] |
---|
256 | // and tell the USART interface, it may receive new data |
---|
257 | // by setting the usart_rx_ready flag FALSE again |
---|
258 | ++SA_mux_val; |
---|
259 | if (Res_or_Volt == 1) SB_mux_val = 16; |
---|
260 | else if (SA_mux_val == 64) SA_mux_val = 32; |
---|
261 | else if (SA_mux_val == 16) SA_mux_val = 48; |
---|
262 | else if (SA_mux_val == 32) SA_mux_val = 0; |
---|
263 | PORTA = (SA_mux_val & 0x3F); |
---|
264 | |
---|
265 | // usart_write_str((pU08)"SA:"); |
---|
266 | usart_write_U08(SA_mux_val,2); |
---|
267 | usart_write_str((pU08)" Sensor:"); |
---|
268 | usart_write_U08((SA_mux_val % 8)+1,2); |
---|
269 | usart_write_str((pU08)" an Temperatur_"); |
---|
270 | switch (SA_mux_val / 8) |
---|
271 | { |
---|
272 | case 0: usart_write_str((pU08)"C"); |
---|
273 | break; |
---|
274 | case 1: usart_write_str((pU08)"D"); |
---|
275 | break; |
---|
276 | case 2: usart_write_str((pU08)"A"); |
---|
277 | break; |
---|
278 | case 3: usart_write_str((pU08)"B"); |
---|
279 | break; |
---|
280 | case 4: usart_write_str((pU08)"G"); |
---|
281 | break; |
---|
282 | case 5: usart_write_str((pU08)"H"); |
---|
283 | break; |
---|
284 | case 6: usart_write_str((pU08)"E"); |
---|
285 | break; |
---|
286 | case 7: usart_write_str((pU08)"F"); |
---|
287 | break; |
---|
288 | default: usart_write_str((pU08)"alarm!"); |
---|
289 | break; |
---|
290 | } |
---|
291 | // usart_write_str((pU08)"\n"); |
---|
292 | usart_write_str((pU08)" "); |
---|
293 | |
---|
294 | |
---|
295 | startconv(0); |
---|
296 | |
---|
297 | |
---|
298 | while (!AD7719_IS_READY()) |
---|
299 | { |
---|
300 | // just wait until ADC is redy -- really bad code here! |
---|
301 | } |
---|
302 | |
---|
303 | resistance = getresistance(); |
---|
304 | //Start a new A/D Conversion |
---|
305 | //temp = readandsendtemp(); |
---|
306 | //adcword = getadc(); |
---|
307 | |
---|
308 | //temperature = gettemp(); |
---|
309 | usart_write_str((pU08)"R:"); |
---|
310 | usart_write_float(resistance,3,4); |
---|
311 | usart_write_str((pU08)"kOhm "); |
---|
312 | |
---|
313 | //_delay_ms(200); |
---|
314 | |
---|
315 | startconv(0); |
---|
316 | |
---|
317 | while (!AD7719_IS_READY()) |
---|
318 | { |
---|
319 | // just wait until ADC is redy -- really bad code here! |
---|
320 | } |
---|
321 | //Start a new A/D Conversion |
---|
322 | //temp = readandsendtemp(); |
---|
323 | //adcword = getadc(); |
---|
324 | resistance = getresistance(); |
---|
325 | //temperature = gettemp(); |
---|
326 | usart_write_str((pU08)"R:"); |
---|
327 | usart_write_float(resistance,3,4); |
---|
328 | usart_write_str((pU08)"kOhm "); |
---|
329 | |
---|
330 | //usart_write_str((pU08)"\n"); |
---|
331 | switch (SA_mux_val) |
---|
332 | { |
---|
333 | case 7: usart_write_str((pU08)"\n\n"); |
---|
334 | break; |
---|
335 | case 15: usart_write_str((pU08)"\n\n"); |
---|
336 | break; |
---|
337 | case 23: usart_write_str((pU08)"\n\n"); |
---|
338 | break; |
---|
339 | case 31: usart_write_str((pU08)"\n\n"); |
---|
340 | break; |
---|
341 | case 39: usart_write_str((pU08)"\n\n"); |
---|
342 | break; |
---|
343 | case 47: usart_write_str((pU08)"\n\n"); |
---|
344 | break; |
---|
345 | case 55: usart_write_str((pU08)"\n\n"); |
---|
346 | break; |
---|
347 | case 63: usart_write_str((pU08)"\n\n"); |
---|
348 | break; |
---|
349 | default: usart_write_str((pU08)"\n"); |
---|
350 | break; |
---|
351 | } |
---|
352 | SB_mux_val = 0; |
---|
353 | } |
---|
354 | else if (Res_or_Volt == 148) Res_or_Volt = 0; |
---|
355 | else { |
---|
356 | |
---|
357 | |
---|
358 | ++SB_mux_val; |
---|
359 | if (SB_mux_val == 84) SB_mux_val = 0; |
---|
360 | else if (SB_mux_val == 74) SB_mux_val = 82; |
---|
361 | else if (SB_mux_val == 82) SB_mux_val = 72; |
---|
362 | else if (SB_mux_val == 72) SB_mux_val = 74; |
---|
363 | else if (SB_mux_val == 48) SB_mux_val = 64; |
---|
364 | else if (SB_mux_val == 64) SB_mux_val = 32; |
---|
365 | else if (SB_mux_val == 32) SB_mux_val = 48; |
---|
366 | PORTC = (SB_mux_val & 0x7F); |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | |
---|
371 | usart_write_str((pU08)"8bit-ADC: "); |
---|
372 | |
---|
373 | if (SB_mux_val < 64) |
---|
374 | { |
---|
375 | switch (SB_mux_val / 16) |
---|
376 | { |
---|
377 | case 0: usart_write_str((pU08)"voltage_A: "); |
---|
378 | break; |
---|
379 | case 1: usart_write_str((pU08)"voltage_B: "); |
---|
380 | break; |
---|
381 | case 2: usart_write_str((pU08)"voltage_D: "); |
---|
382 | break; |
---|
383 | case 3: usart_write_str((pU08)"voltage_C: "); |
---|
384 | break; |
---|
385 | } |
---|
386 | |
---|
387 | if (SB_mux_val % 2 == 0) { |
---|
388 | usart_write_str((pU08)"U"); |
---|
389 | usart_write_U08( (SB_mux_val%16)/2 , 1 ); |
---|
390 | } else { |
---|
391 | usart_write_str((pU08)"I"); |
---|
392 | usart_write_U08( ((SB_mux_val%16)-1)/2 , 1 ); |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | } else { |
---|
397 | |
---|
398 | |
---|
399 | if (SB_mux_val < 72) { |
---|
400 | usart_write_str((pU08)"voltage_E: "); |
---|
401 | if (SB_mux_val % 2 == 0) { |
---|
402 | usart_write_str((pU08)"U"); |
---|
403 | usart_write_U08( (SB_mux_val%8)/2 , 1 ); |
---|
404 | } else { |
---|
405 | usart_write_str((pU08)"I"); |
---|
406 | usart_write_U08( ((SB_mux_val%8)-1)/2 , 1 ); |
---|
407 | } |
---|
408 | |
---|
409 | } |
---|
410 | else if (SB_mux_val == 72) usart_write_str((pU08)"humidity_A: H0"); |
---|
411 | else if (SB_mux_val == 73) usart_write_str((pU08)"humidity_A: H1"); |
---|
412 | |
---|
413 | else if (SB_mux_val < 82) { |
---|
414 | usart_write_str((pU08)"voltage_F: "); |
---|
415 | if (SB_mux_val % 2 == 0) { |
---|
416 | usart_write_str((pU08)"U"); |
---|
417 | usart_write_U08( ((SB_mux_val-2)%8)/2 , 1 ); |
---|
418 | } else { |
---|
419 | usart_write_str((pU08)"I"); |
---|
420 | usart_write_U08( (((SB_mux_val-2)%8)-1)/2 , 1 ); |
---|
421 | } |
---|
422 | |
---|
423 | } |
---|
424 | else if (SB_mux_val == 82) usart_write_str((pU08)"humidity_B: H0"); |
---|
425 | else if (SB_mux_val == 83) usart_write_str((pU08)"humidity_B: H1"); |
---|
426 | } |
---|
427 | |
---|
428 | for (U08 counter = 0; counter < 1; ++counter) { |
---|
429 | while (ADCSRA & (1<<ADSC) ); // wait until internal ADC is ready |
---|
430 | float voltage; |
---|
431 | voltage = ( (float)ADCH ) / 256 * 4.096; |
---|
432 | usart_write_str((pU08)" "); |
---|
433 | usart_write_float(voltage,3,4); |
---|
434 | |
---|
435 | |
---|
436 | } |
---|
437 | //usart_write_str((pU08)"\n"); |
---|
438 | |
---|
439 | switch (SB_mux_val) |
---|
440 | { |
---|
441 | case 15: usart_write_str((pU08)"\n\n"); |
---|
442 | break; |
---|
443 | case 31: usart_write_str((pU08)"\n\n"); |
---|
444 | break; |
---|
445 | case 47: usart_write_str((pU08)"\n\n"); |
---|
446 | break; |
---|
447 | case 63: usart_write_str((pU08)"\n\n"); |
---|
448 | break; |
---|
449 | case 71: usart_write_str((pU08)"\n\n"); |
---|
450 | break; |
---|
451 | case 73: usart_write_str((pU08)"\n\n"); |
---|
452 | break; |
---|
453 | case 81: usart_write_str((pU08)"\n\n"); |
---|
454 | break; |
---|
455 | case 83: usart_write_str((pU08)"\n\n"); |
---|
456 | break; |
---|
457 | default: usart_write_str((pU08)"\n"); |
---|
458 | break; |
---|
459 | } |
---|
460 | |
---|
461 | SA_mux_val = 15; |
---|
462 | } |
---|
463 | /* |
---|
464 | if ( usart_rx_ready == TRUE ) |
---|
465 | { |
---|
466 | //understand what it means and react |
---|
467 | |
---|
468 | switch (usart_rx_buffer[0]) |
---|
469 | { |
---|
470 | |
---|
471 | case 'h': |
---|
472 | { |
---|
473 | // toggle the heartbeat mode on or off. |
---|
474 | heartbeat_enable = !heartbeat_enable; |
---|
475 | break; |
---|
476 | } |
---|
477 | case 'a': |
---|
478 | { |
---|
479 | // conduct adc - AD7719 SPI interface test |
---|
480 | |
---|
481 | break; |
---|
482 | } |
---|
483 | case 'e': |
---|
484 | { |
---|
485 | // conduct ethernet module SPI interface test |
---|
486 | strtol((char*) usart_rx_buffer+1, NULL, 0); |
---|
487 | break; |
---|
488 | } |
---|
489 | |
---|
490 | default: |
---|
491 | { |
---|
492 | usart_write_str((pU08)"? you wrote: "); |
---|
493 | usart_write_str((pU08)usart_rx_buffer); |
---|
494 | usart_write_str((pU08)"\n"); |
---|
495 | break; |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | heartbeat_enable = !heartbeat_enable; |
---|
500 | usart_rx_ready = FALSE; |
---|
501 | } |
---|
502 | */ |
---|
503 | // das ist ein paar schritte zu früh. |
---|
504 | // erstmal müssen die interfaces getestet werden. |
---|
505 | /* |
---|
506 | |
---|
507 | for (U08 i = 0; i<16; i++) |
---|
508 | { |
---|
509 | |
---|
510 | if((~PIND) & 0x08) // PD4 is #ADC_RDY input. Inverted logic! if PD4=0 this evaluates to true |
---|
511 | { |
---|
512 | PORTA = (PORTA & 0xF0) | ((i) & 0x0F); // switch muxer |
---|
513 | startconv(); //Start a new A/D Conversion |
---|
514 | //temp = readandsendtemp(); |
---|
515 | //adcword = getadc(); |
---|
516 | //resistance = getresistance(); |
---|
517 | temperature = gettemp(); |
---|
518 | usart_write_float(temperature,2,4); |
---|
519 | usart_write_str((pU08)"\t"); |
---|
520 | |
---|
521 | } // end of if adc ready |
---|
522 | else |
---|
523 | { |
---|
524 | i--; |
---|
525 | } |
---|
526 | } // end of for loop over 16 channels |
---|
527 | usart_write_crlf(); |
---|
528 | |
---|
529 | */ |
---|
530 | |
---|
531 | } // end of infinite while loop |
---|
532 | } // end of main() |
---|
533 | |
---|
534 | |
---|
535 | ISR (TIMER2_COMP_vect) |
---|
536 | { |
---|
537 | ++local_ms; |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | U08 increase_adc (U08 channel){ |
---|
542 | bool valid_ch_found = false; |
---|
543 | while (!valid_ch_found){ |
---|
544 | |
---|
545 | // just increase 'channel' or turnover to zero. |
---|
546 | ++channel; |
---|
547 | if (channel == V_CHANNELS + I_CHANNELS + H_CHANNELS) |
---|
548 | channel = 0; |
---|
549 | |
---|
550 | // check if this channel is enabled in the bitmap |
---|
551 | if (adc_enables[channel/8] & (1<<channel%8)) |
---|
552 | valid_ch_found = true; |
---|
553 | } // end of while loop |
---|
554 | return channel; |
---|
555 | } // end if increase_adc; |
---|
556 | |
---|
557 | U08 increase_ad7719 (U08 channel){ |
---|
558 | bool valid_ch_found = false; |
---|
559 | while (!valid_ch_found){ |
---|
560 | |
---|
561 | // just increase 'channel' or turnover to zero. |
---|
562 | ++channel; |
---|
563 | if (channel == TEMP_CHANNELS) |
---|
564 | channel = 0; |
---|
565 | |
---|
566 | // check if this channel is enabled in the bitmap |
---|
567 | if (ad7719_enables[channel/8] & (1<<channel%8)) |
---|
568 | valid_ch_found = true; |
---|
569 | } // end of while loop |
---|
570 | return channel; |
---|
571 | } // end if increase_adc; |
---|
572 | |
---|
573 | |
---|
574 | // Sets voltage Muxer to current channel |
---|
575 | // this is a Muxing, and therefor the adc might need some time to settle. |
---|
576 | // Since there are: |
---|
577 | // - 40 voltage monitor channels |
---|
578 | // - 40 current monitor channels |
---|
579 | // - 4 humidity monitor channels |
---|
580 | // the muxer is set as follows. |
---|
581 | // channel 00..39 --> looking at the voltage channels |
---|
582 | // channel 40..79 --> looking at the current channels |
---|
583 | // channel 80..83 --> looking at the humidities |
---|
584 | void Set_V_Muxer (U08 channel){ |
---|
585 | U08 SB = 0; |
---|
586 | // voltages |
---|
587 | if (channel < 40) { |
---|
588 | if (channel < 36) |
---|
589 | SB = channel*2; |
---|
590 | else |
---|
591 | SB = (channel+1)*2; |
---|
592 | } |
---|
593 | // currents |
---|
594 | else if (channel < 80) { |
---|
595 | channel -= 40; |
---|
596 | if (channel < 36) |
---|
597 | SB = channel*2+1; |
---|
598 | else |
---|
599 | SB = (channel+1)*2+1; |
---|
600 | } |
---|
601 | // humidities |
---|
602 | else if (channel < 84) { |
---|
603 | channel -= 80; |
---|
604 | switch (channel) { |
---|
605 | case 0: |
---|
606 | SB = 0x48; //0100.1000 |
---|
607 | break; |
---|
608 | case 1: |
---|
609 | SB = 0x49; //0100.1001 |
---|
610 | break; |
---|
611 | case 2: |
---|
612 | SB = 0x58; //0101.0010 |
---|
613 | break; |
---|
614 | case 3: |
---|
615 | SB = 0x58; //0101.0011 |
---|
616 | break; |
---|
617 | } // end of switch-case |
---|
618 | } // end of if (channel < some_number) |
---|
619 | |
---|
620 | PORTC = (PORTC & 0x80) | (0x7F & SB); // Here the muxer is switched. |
---|
621 | } |
---|
622 | |
---|
623 | void Set_T_Muxer(U08 channel) { |
---|
624 | U08 SA = 0x00; |
---|
625 | |
---|
626 | switch (channel/16) { |
---|
627 | case 0: |
---|
628 | SA |= 1<<4; // 0001.0000 |
---|
629 | break; |
---|
630 | case 1: |
---|
631 | break; // 0000.0000 |
---|
632 | case 2: |
---|
633 | SA |= (1<<4)|(1<<5); // 0011.0000 |
---|
634 | break; |
---|
635 | case 3: |
---|
636 | SA |= 1<<5; // 0010.0000 |
---|
637 | break; |
---|
638 | } |
---|
639 | |
---|
640 | SA = SA | (channel%16); |
---|
641 | |
---|
642 | PORTA = (PORTA & 0xC0) | (0x3F & SA); // Here the muxer is switched. |
---|
643 | } |
---|
644 | |
---|
645 | void talk(void){ |
---|
646 | |
---|
647 | /* |
---|
648 | // makes no sense to declare the 'new_measurement' vars here |
---|
649 | // but maybe the whole function will be deleted, anyway ... |
---|
650 | // I'm thinking about it. |
---|
651 | bool ad7719_new_measurement; |
---|
652 | bool atmega_adc_new_measurement; |
---|
653 | if (verbose == true) { |
---|
654 | // somebody wants to read every new measured value, even if it is trash! |
---|
655 | // do not actually send away data ! |
---|
656 | // just prepare the data to be send away. |
---|
657 | if ( ad7719_new_measurement == true ) { |
---|
658 | add_str_to_output_stream("ad7719: reading:"); |
---|
659 | add_dec_to_output_stream(reading_since_last_muxer_switch,1); |
---|
660 | add_str_to_output_stream(" temperature channel:"); |
---|
661 | add_dec_to_output_stream(current_temperature_channel,2); |
---|
662 | add_str_to_output_stream(" = "); |
---|
663 | add_float_to_output_stream(current_ad7719_value,4,3); |
---|
664 | add_str_to_output_stream("\n"); |
---|
665 | } |
---|
666 | if (atmega_adc_new_measurement == true) { |
---|
667 | add_str_to_output_stream("atmega_adc: reading:"); |
---|
668 | add_dec_to_output_stream(reading_since_last_muxer_switch,1); |
---|
669 | add_str_to_output_stream(" voltage channel:"); |
---|
670 | add_dec_to_output_stream(current_voltage_channel,2); |
---|
671 | add_str_to_output_stream(" = "); |
---|
672 | add_float_to_output_stream(current_atmega_adc_value,4,3); |
---|
673 | add_str_to_output_stream("\n"); |
---|
674 | } |
---|
675 | } // end of: if verbose |
---|
676 | */ |
---|
677 | } // end of talk() |
---|
678 | |
---|
679 | // this function generates some output. |
---|
680 | void ad7719_output(U08 channel, U32 data) { |
---|
681 | usart_write_str((pU08)"R:"); //R for resistance |
---|
682 | usart_write_char('A'+channel/8); // Letters A,B,C,D,E,F,G,H |
---|
683 | usart_write_char(' '); |
---|
684 | usart_write_U08(channel%8+1,1); // Numbers 1...8 |
---|
685 | usart_write_char(':'); |
---|
686 | usart_write_U32_hex(data); //data |
---|
687 | usart_write_char('\n'); |
---|
688 | } |
---|
689 | |
---|
690 | void adc_output(U08 channel, U08 data) { |
---|
691 | |
---|
692 | if (channel < 40) |
---|
693 | usart_write_str((pU08)"V:"); //V for Vendetta |
---|
694 | else if (channel < 80) |
---|
695 | usart_write_str((pU08)"I:"); //I for Irregular verbs |
---|
696 | else if (channel < 84) |
---|
697 | usart_write_str((pU08)"H:"); //H for Huurrray!!! |
---|
698 | |
---|
699 | switch (channel/16) { |
---|
700 | case 0: |
---|
701 | usart_write_char('A'); //V for Vendetta |
---|
702 | case 1: |
---|
703 | usart_write_char('B'); //V for Vendetta |
---|
704 | case 2: |
---|
705 | usart_write_char('D'); //V for Vendetta |
---|
706 | case 3: |
---|
707 | usart_write_char('C'); //V for Vendetta |
---|
708 | case 4: |
---|
709 | usart_write_char('EF'); //V for Vendetta |
---|
710 | } |
---|
711 | usart_write_char(' '); |
---|
712 | usart_write_U08((channel/2)%8+1,1); // Numbers 1...8 |
---|
713 | usart_write_char(':'); |
---|
714 | usart_write_U16((U16)data*16,5); //data |
---|
715 | usart_write_char('\n'); |
---|
716 | |
---|
717 | |
---|
718 | } |
---|
719 | |
---|
720 | |
---|
721 | void adc_output_all() { |
---|
722 | // output all values, which are enabled |
---|
723 | for (U08 i=0 ; i<40; ++i){ |
---|
724 | if (i==0) usart_write_str((pU08)"voltages:(in units of 16mV)\n"); |
---|
725 | if (i==40) usart_write_str((pU08)"currents:\n"); |
---|
726 | if (i==80) usart_write_str((pU08)"humidities:\n"); |
---|
727 | if (adc_enables[i/8] & i%8){ |
---|
728 | usart_write_U08(adc_values[i],3); |
---|
729 | usart_write_char('\t'); |
---|
730 | } |
---|
731 | if (i%8==7) usart_write_char('\n'); |
---|
732 | if (i==83) usart_write_char('\n'); |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | |
---|
737 | // this method parses the data, |
---|
738 | // which came in via USART |
---|
739 | // later it might as well parse the data from ethernet. |
---|
740 | void parse() { |
---|
741 | U08 command = usart_rx_buffer[0]; |
---|
742 | // look at first byte |
---|
743 | // I hope, I can manage to use one byte commands |
---|
744 | usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0; |
---|
745 | usart_write_str((pU08)"got:"); |
---|
746 | usart_write_str(usart_rx_buffer); |
---|
747 | |
---|
748 | |
---|
749 | |
---|
750 | switch (command) { |
---|
751 | case 'E': // AD7719 enable bitmaps may be set |
---|
752 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) { |
---|
753 | ad7719_enables[i]=usart_rx_buffer[i+1]; |
---|
754 | ad7719_channels_ready[i]=0; |
---|
755 | } |
---|
756 | break; |
---|
757 | case 'e': // ATmega internal ADC enable bitmaps may be set |
---|
758 | for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) { |
---|
759 | adc_enables[i]=usart_rx_buffer[i+1]; |
---|
760 | adc_channels_ready[i]=0; |
---|
761 | } |
---|
762 | break; |
---|
763 | case 'h': |
---|
764 | usart_write_str((pU08)"\nheartbeat "); |
---|
765 | heartbeat_enable = true; |
---|
766 | if (usart_rx_buffer[1] == '0'){ |
---|
767 | heartbeat_enable = false; |
---|
768 | usart_write_str((pU08)"off\n"); |
---|
769 | } else { |
---|
770 | usart_write_str((pU08)"on\n"); |
---|
771 | } |
---|
772 | break; |
---|
773 | case 'G': // GET the Temperature channels, which are enabled |
---|
774 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) { |
---|
775 | ad7719_channels_ready[i]=0; |
---|
776 | } |
---|
777 | break; |
---|
778 | case 'g': // GET the voltage/current/humidity channels, which are enabled |
---|
779 | for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) { |
---|
780 | adc_channels_ready[i]=0; |
---|
781 | } |
---|
782 | break; |
---|
783 | case 's': |
---|
784 | usart_write_char('\n'); |
---|
785 | for (U08 i=0; i< CHANNEL_BITMAP;++i) { |
---|
786 | usart_write_U08_bin(ad7719_enables[i]); |
---|
787 | usart_write_char('\t'); |
---|
788 | } |
---|
789 | usart_write_char('\n'); |
---|
790 | for (U08 i=0; i< CHANNEL_BITMAP;++i){ |
---|
791 | usart_write_U08_bin(ad7719_channels_ready[i]); |
---|
792 | usart_write_char('\t'); |
---|
793 | } |
---|
794 | usart_write_char('\n'); |
---|
795 | usart_write_U32_hex(local_ms); |
---|
796 | break; |
---|
797 | } |
---|
798 | usart_write_str((pU08)"\nready?"); |
---|
799 | for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i) |
---|
800 | usart_rx_buffer[i] = 0; |
---|
801 | } |
---|
802 | |
---|
803 | void check_if_measured_all() { |
---|
804 | adc_measured_all = true; |
---|
805 | for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) { |
---|
806 | if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) { |
---|
807 | adc_measured_all = false; |
---|
808 | break; |
---|
809 | } |
---|
810 | } |
---|
811 | ad7719_measured_all = true; |
---|
812 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) { |
---|
813 | if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) { |
---|
814 | ad7719_measured_all = false; |
---|
815 | break; |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | |
---|
820 | } |
---|