source: firmware/FSC/src/FSC.c@ 10443

Last change on this file since 10443 was 10245, checked in by neise, 14 years ago
File size: 28.5 KB
Line 
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// definition of some functions:
16// these function are implemented in this file, this is not doog coding style.
17// sooner or later, they will be moved into more apropriate files.
18U08 increase_adc (U08 channel);
19U08 increase_ad7719 (U08 channel);
20void Set_V_Muxer (U08 channel);
21void Set_T_Muxer(U08 channel);
22void talk(void);// never used up to now.
23void ad7719_output(U08 channel, U32 data);
24void adc_output(U08 channel, U08 data);
25void adc_output_all();
26void parse(); //doesn't do anything at the moment
27void check_if_measured_all() ;
28void print_ad7719_nicely();
29void print_adc_nicely();
30
31// end of function definition:
32//-----------------------------------------------------------------------------
33
34// MAIN WORKFLOW GLOBAL VARIABLES
35 bool verbose;
36 bool heartbeat_enable;
37
38// USART global variables
39 U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
40 U08 usart_tx_buffer[USART_TX_BUFFER_SIZE];
41 U08 usart_received_chars;
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 5 // 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 U08 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 bool once_told_you = true;
82 bool debug_mode = false;
83
84//-----------------------------------------------------------------------------
85// M A I N --- M A I N --- M A I N --- M A I N --- M A I N
86//-----------------------------------------------------------------------------
87int main(void)
88{
89 app_init(); // Setup: Watchdog and I/Os
90 usart_init(); // Initialize serial interface
91 spi_init(); // Initialize SPI interface as master
92 ad7719_init(); // Initialize AD7719 ADC as SPI slave
93 atmega_adc_init();
94
95// TIMER2 is used as local clock:
96// configure timer 2
97 TCCR2 = (1<<WGM21); // CTC Modus
98 TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us
99 OCR2 = 125-1; // --> output compare interrupt occurs every 125 x 8us = 1ms
100 // Compare Interrupt erlauben
101 TIMSK |= (1<<OCIE2);
102
103 // Enable interrupts
104 sei();
105
106 PORTB &= ~(1<<PB2);
107
108for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
109 ad7719_enables[i]=0x00;
110 ad7719_channels_ready[i]=0;
111 }
112 ad7719_enables[0]=0x01;
113for ( U08 i=0; i<V_BITMAP + I_BITMAP; ++i ) {
114 adc_enables[i]=0xFF;
115 adc_channels_ready[i]=0;
116}
117 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP -1] = 0x0F;
118 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP -1]=0;
119
120 static U08 welcome[]="\n\nwelcome to FACT FSC commandline interface v0.4\nDN 17.03.2011\nready?";
121 usart_write_str(welcome);
122
123
124//MAIN LOOP
125while (1)
126{
127
128 //IF one of the ADC measured all channels, we wanted to know.
129 check_if_measured_all();
130
131 if (ad7719_measured_all && adc_measured_all && !once_told_you)
132 {
133 adc_output_all();
134 once_told_you = true;
135 }
136//----------------------------------------------------------------------------
137
138 if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat
139//----------------------------------------------------------------------------
140 //IF we need to send away one byte, and ready to send
141
142 if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
143 UDR = usart_tx_buffer[0];
144 // THis is shit
145 for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
146 usart_tx_buffer[i] = usart_tx_buffer[i+1];
147 }
148 usart_tx_buffer_index--;
149 }
150//----------------------------------------------------------------------------
151
152 //IF we just received one byte, and there is enough space in the RX_buffer
153 if ( (UCSRA & (1<<RXC)) && (usart_rx_buffer_index < USART_RX_BUFFER_SIZE) ){
154 usart_last_char = UDR;
155 if (usart_last_char == '\n'){ // if EOL was received
156 usart_rx_ready = true;
157 }else {
158 usart_rx_buffer[usart_rx_buffer_index] = usart_last_char;
159 usart_rx_buffer_index++;
160 }
161 // here is still something strange .... better send an enter automatically
162 } else if (UCSRA & (1<<RXC)) { // if there is no scace in the buffer; read anyway.
163 usart_last_char = UDR;
164 usart_rx_buffer_index =0;
165 }
166//----------------------------------------------------------------------------
167
168 //IF USART DOR bit is set, PC is sending data to fast!!!
169 if ( UCSRA & (1<<DOR) ){
170 // flush TX_buffer and write warning message in
171 // maybe even switch off every measurement. ?
172 }
173//----------------------------------------------------------------------------
174
175 //IF TX_BUFFER was overrun.
176 if (usart_tx_buffer_overflow) {
177 // flash TX_buffer and write warning message in
178 // maybe even switch off every measurement. ?
179 //
180 // this should only happen, in verbose mode and with low baudrates.
181 }
182//----------------------------------------------------------------------------
183
184 //IF one command was received.
185 // -It is not allowed to send more than one command between two '\n'
186 if (usart_rx_ready){
187 parse();
188 usart_rx_buffer_index = 0;
189 usart_rx_ready = false;
190 }
191//----------------------------------------------------------------------------
192
193 //IF ATmega internal ADC did finish a conversion --every 200us
194 if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
195 adc_current_reading = ADCH;
196 if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED) {
197 adc_values[adc_current_channel] = adc_current_reading;
198 adc_readings_since_last_muxing=0;
199 // note that this channel is ready, now and
200 //adc_output(adc_current_channel, adc_current_reading);
201 // proceed to the next enabled channel.
202 adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8));
203 adc_current_channel = increase_adc (adc_current_channel);
204 Set_V_Muxer(adc_current_channel);
205 _delay_ms(10);
206 } else { // the ADC did not settle yet, we discard the reading
207 ++adc_readings_since_last_muxing;
208 // current reading is not used for anything else
209 }
210 }
211//----------------------------------------------------------------------------
212
213 //IF AD7719 ADC just finished a conversion -- every 60ms
214
215 if (AD7719_IS_READY() && !ad7719_measured_all) {
216 ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
217 // AD7719 is only read out if settled. saves time.
218 if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED) {
219 ad7719_values[ad7719_current_channel] = ad7719_current_reading;
220 ad7719_readings_since_last_muxing=0;
221 if (debug_mode) {
222 usart_write_U32_hex(ad7719_current_reading);
223 usart_write_char('\n');
224 usart_write_char('\n');
225 }
226 // now prepare the data to be send away via USART.
227 //ad7719_output(ad7719_current_channel, ad7719_current_reading);
228 // note that this channel is ready, now and
229 // proceed to the next enabled channel.
230 ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8));
231 ad7719_current_channel = increase_ad7719 (ad7719_current_channel);
232 Set_T_Muxer(ad7719_current_channel);
233 } else { // the AD7719 did not settle yet, we discard the reading
234 ++ad7719_readings_since_last_muxing;
235 if (debug_mode) {
236 usart_write_U32_hex(ad7719_current_reading);
237 usart_write_char('\n');
238 }
239
240 // current reading is not used for anything else
241 }
242 }
243
244//----------------------------------------------------------------------------
245/*
246 if (verbose == true)
247 // talk() was just defined so the
248 // code is not at this place ... look down.
249 talk();
250*/
251
252} // end of MAIN LOOP
253//-----------------------------------------------------------------------------
254// E N D E N D E N D E N D E N D E N D E N D
255//-----------------------------------------------------------------------------
256
257
258
259float resistance;
260
261U08 SA_mux_val = 0x00;
262U08 SB_mux_val = 0x00;
263
264//U08 counter = 0;
265U08 Res_or_Volt = 0x00;
266
267
268while (TRUE)
269 {
270
271
272 ++Res_or_Volt;
273 if (Res_or_Volt <= 64){
274
275
276 // if USART data arrives. i.e. data via USB
277 // the usart_rx_ready flag is set TRUE
278 // now process the incoming data which is stored in
279 // U08 usart_rx_buffer[USART_RX_BUFFER_SIZE]
280 // and tell the USART interface, it may receive new data
281 // by setting the usart_rx_ready flag FALSE again
282 ++SA_mux_val;
283 if (Res_or_Volt == 1) SB_mux_val = 16;
284 else if (SA_mux_val == 64) SA_mux_val = 32;
285 else if (SA_mux_val == 16) SA_mux_val = 48;
286 else if (SA_mux_val == 32) SA_mux_val = 0;
287 PORTA = (SA_mux_val & 0x3F);
288
289// usart_write_str((pU08)"SA:");
290 usart_write_U08(SA_mux_val,2);
291 usart_write_str((pU08)" Sensor:");
292 usart_write_U08((SA_mux_val % 8)+1,2);
293 usart_write_str((pU08)" an Temperatur_");
294 switch (SA_mux_val / 8)
295 {
296 case 0: usart_write_str((pU08)"C");
297 break;
298 case 1: usart_write_str((pU08)"D");
299 break;
300 case 2: usart_write_str((pU08)"A");
301 break;
302 case 3: usart_write_str((pU08)"B");
303 break;
304 case 4: usart_write_str((pU08)"G");
305 break;
306 case 5: usart_write_str((pU08)"H");
307 break;
308 case 6: usart_write_str((pU08)"E");
309 break;
310 case 7: usart_write_str((pU08)"F");
311 break;
312 default: usart_write_str((pU08)"alarm!");
313 break;
314 }
315// usart_write_str((pU08)"\n");
316 usart_write_str((pU08)" ");
317
318
319 startconv(0);
320
321
322 while (!AD7719_IS_READY())
323 {
324 // just wait until ADC is redy -- really bad code here!
325 }
326
327 resistance = getresistance();
328 //Start a new A/D Conversion
329 //temp = readandsendtemp();
330 //adcword = getadc();
331
332 //temperature = gettemp();
333 usart_write_str((pU08)"R:");
334 usart_write_float(resistance,3,4);
335 usart_write_str((pU08)"kOhm ");
336
337 //_delay_ms(200);
338
339 startconv(0);
340
341 while (!AD7719_IS_READY())
342 {
343 // just wait until ADC is redy -- really bad code here!
344 }
345 //Start a new A/D Conversion
346 //temp = readandsendtemp();
347 //adcword = getadc();
348 resistance = getresistance();
349 //temperature = gettemp();
350 usart_write_str((pU08)"R:");
351 usart_write_float(resistance,3,4);
352 usart_write_str((pU08)"kOhm ");
353
354//usart_write_str((pU08)"\n");
355switch (SA_mux_val)
356 {
357 case 7: usart_write_str((pU08)"\n\n");
358 break;
359 case 15: usart_write_str((pU08)"\n\n");
360 break;
361 case 23: usart_write_str((pU08)"\n\n");
362 break;
363 case 31: usart_write_str((pU08)"\n\n");
364 break;
365 case 39: usart_write_str((pU08)"\n\n");
366 break;
367 case 47: usart_write_str((pU08)"\n\n");
368 break;
369 case 55: usart_write_str((pU08)"\n\n");
370 break;
371 case 63: usart_write_str((pU08)"\n\n");
372 break;
373 default: usart_write_str((pU08)"\n");
374 break;
375 }
376SB_mux_val = 0;
377}
378else if (Res_or_Volt == 148) Res_or_Volt = 0;
379else {
380
381
382 ++SB_mux_val;
383 if (SB_mux_val == 84) SB_mux_val = 0;
384 else if (SB_mux_val == 74) SB_mux_val = 82;
385 else if (SB_mux_val == 82) SB_mux_val = 72;
386 else if (SB_mux_val == 72) SB_mux_val = 74;
387 else if (SB_mux_val == 48) SB_mux_val = 64;
388 else if (SB_mux_val == 64) SB_mux_val = 32;
389 else if (SB_mux_val == 32) SB_mux_val = 48;
390 PORTC = (SB_mux_val & 0x7F);
391
392
393
394
395usart_write_str((pU08)"8bit-ADC: ");
396
397if (SB_mux_val < 64)
398{
399 switch (SB_mux_val / 16)
400 {
401 case 0: usart_write_str((pU08)"voltage_A: ");
402 break;
403 case 1: usart_write_str((pU08)"voltage_B: ");
404 break;
405 case 2: usart_write_str((pU08)"voltage_D: ");
406 break;
407 case 3: usart_write_str((pU08)"voltage_C: ");
408 break;
409 }
410
411 if (SB_mux_val % 2 == 0) {
412 usart_write_str((pU08)"U");
413 usart_write_U08( (SB_mux_val%16)/2 , 1 );
414 } else {
415 usart_write_str((pU08)"I");
416 usart_write_U08( ((SB_mux_val%16)-1)/2 , 1 );
417 }
418
419
420} else {
421
422
423 if (SB_mux_val < 72) {
424 usart_write_str((pU08)"voltage_E: ");
425 if (SB_mux_val % 2 == 0) {
426 usart_write_str((pU08)"U");
427 usart_write_U08( (SB_mux_val%8)/2 , 1 );
428 } else {
429 usart_write_str((pU08)"I");
430 usart_write_U08( ((SB_mux_val%8)-1)/2 , 1 );
431 }
432
433 }
434else if (SB_mux_val == 72) usart_write_str((pU08)"humidity_A: H0");
435else if (SB_mux_val == 73) usart_write_str((pU08)"humidity_A: H1");
436
437else if (SB_mux_val < 82) {
438 usart_write_str((pU08)"voltage_F: ");
439 if (SB_mux_val % 2 == 0) {
440 usart_write_str((pU08)"U");
441 usart_write_U08( ((SB_mux_val-2)%8)/2 , 1 );
442 } else {
443 usart_write_str((pU08)"I");
444 usart_write_U08( (((SB_mux_val-2)%8)-1)/2 , 1 );
445 }
446
447 }
448else if (SB_mux_val == 82) usart_write_str((pU08)"humidity_B: H0");
449else if (SB_mux_val == 83) usart_write_str((pU08)"humidity_B: H1");
450}
451
452for (U08 counter = 0; counter < 1; ++counter) {
453 while (ADCSRA & (1<<ADSC) ); // wait until internal ADC is ready
454 float voltage;
455 voltage = ( (float)ADCH ) / 256 * 4.096;
456 usart_write_str((pU08)" ");
457 usart_write_float(voltage,3,4);
458
459
460}
461//usart_write_str((pU08)"\n");
462
463switch (SB_mux_val)
464 {
465 case 15: usart_write_str((pU08)"\n\n");
466 break;
467 case 31: usart_write_str((pU08)"\n\n");
468 break;
469 case 47: usart_write_str((pU08)"\n\n");
470 break;
471 case 63: usart_write_str((pU08)"\n\n");
472 break;
473 case 71: usart_write_str((pU08)"\n\n");
474 break;
475 case 73: usart_write_str((pU08)"\n\n");
476 break;
477 case 81: usart_write_str((pU08)"\n\n");
478 break;
479 case 83: usart_write_str((pU08)"\n\n");
480 break;
481 default: usart_write_str((pU08)"\n");
482 break;
483 }
484
485SA_mux_val = 15;
486}
487 /*
488 if ( usart_rx_ready == TRUE )
489 {
490 //understand what it means and react
491
492 switch (usart_rx_buffer[0])
493 {
494
495 case 'h':
496 {
497 // toggle the heartbeat mode on or off.
498 heartbeat_enable = !heartbeat_enable;
499 break;
500 }
501 case 'a':
502 {
503 // conduct adc - AD7719 SPI interface test
504
505 break;
506 }
507 case 'e':
508 {
509 // conduct ethernet module SPI interface test
510 strtol((char*) usart_rx_buffer+1, NULL, 0);
511 break;
512 }
513
514 default:
515 {
516 usart_write_str((pU08)"? you wrote: ");
517 usart_write_str((pU08)usart_rx_buffer);
518 usart_write_str((pU08)"\n");
519 break;
520 }
521 }
522
523 heartbeat_enable = !heartbeat_enable;
524 usart_rx_ready = FALSE;
525 }
526*/
527// das ist ein paar schritte zu früh.
528// erstmal müssen die interfaces getestet werden.
529/*
530
531 for (U08 i = 0; i<16; i++)
532 {
533
534 if((~PIND) & 0x08) // PD4 is #ADC_RDY input. Inverted logic! if PD4=0 this evaluates to true
535 {
536 PORTA = (PORTA & 0xF0) | ((i) & 0x0F); // switch muxer
537 startconv(); //Start a new A/D Conversion
538 //temp = readandsendtemp();
539 //adcword = getadc();
540 //resistance = getresistance();
541 temperature = gettemp();
542 usart_write_float(temperature,2,4);
543 usart_write_str((pU08)"\t");
544
545 } // end of if adc ready
546 else
547 {
548 i--;
549 }
550 } // end of for loop over 16 channels
551 usart_write_crlf();
552
553*/
554
555 } // end of infinite while loop
556} // end of main()
557
558
559ISR (TIMER2_COMP_vect)
560{
561 ++local_ms;
562}
563
564
565U08 increase_adc (U08 channel){
566bool valid_ch_found = false;
567 while (!valid_ch_found){
568
569 // just increase 'channel' or turnover to zero.
570 ++channel;
571 if (channel == V_CHANNELS + I_CHANNELS + H_CHANNELS)
572 channel = 0;
573
574 // check if this channel is enabled in the bitmap
575 if (adc_enables[channel/8] & (1<<channel%8))
576 valid_ch_found = true;
577 } // end of while loop
578 return channel;
579} // end if increase_adc;
580
581U08 increase_ad7719 (U08 channel){
582bool valid_ch_found = false;
583 while (!valid_ch_found){
584
585 // just increase 'channel' or turnover to zero.
586 ++channel;
587 if (channel == TEMP_CHANNELS)
588 channel = 0;
589
590 // check if this channel is enabled in the bitmap
591 if (ad7719_enables[channel/8] & (1<<channel%8))
592 valid_ch_found = true;
593 } // end of while loop
594 return channel;
595} // end if increase_adc;
596
597
598// Sets voltage Muxer to current channel
599// this is a Muxing, and therefor the adc might need some time to settle.
600// Since there are:
601// - 40 voltage monitor channels
602// - 40 current monitor channels
603// - 4 humidity monitor channels
604// the muxer is set as follows.
605// channel 00..39 --> looking at the voltage channels
606// channel 40..79 --> looking at the current channels
607// channel 80..83 --> looking at the humidities
608void Set_V_Muxer (U08 channel){
609U08 SB = 0;
610 // voltages
611 if (channel < 40) {
612 if (channel < 36)
613 SB = channel*2;
614 else
615 SB = (channel+1)*2;
616 }
617 // currents
618 else if (channel < 80) {
619 channel -= 40;
620 if (channel < 36)
621 SB = channel*2+1;
622 else
623 SB = (channel+1)*2+1;
624 }
625 // humidities
626 else if (channel < 84) {
627 channel -= 80;
628 switch (channel) {
629 case 0:
630 SB = 0x48; //0100.1000
631 break;
632 case 1:
633 SB = 0x49; //0100.1001
634 break;
635 case 2:
636 SB = 0x58; //0101.0010
637 break;
638 case 3:
639 SB = 0x58; //0101.0011
640 break;
641 } // end of switch-case
642 } // end of if (channel < some_number)
643
644 PORTC = (PORTC & 0x80) | (0x7F & SB); // Here the muxer is switched.
645}
646
647void Set_T_Muxer(U08 channel) {
648U08 SA = 0x00;
649
650 switch (channel/16) {
651 case 0:
652 SA |= 1<<4; // 0001.0000
653 break;
654 case 1:
655 break; // 0000.0000
656 case 2:
657 SA |= (1<<4)|(1<<5); // 0011.0000
658 break;
659 case 3:
660 SA |= 1<<5; // 0010.0000
661 break;
662 }
663
664 SA = SA | (channel%16);
665
666 PORTA = (PORTA & 0xC0) | (0x3F & SA); // Here the muxer is switched.
667}
668
669void talk(void){
670
671/*
672// makes no sense to declare the 'new_measurement' vars here
673// but maybe the whole function will be deleted, anyway ...
674// I'm thinking about it.
675bool ad7719_new_measurement;
676bool atmega_adc_new_measurement;
677 if (verbose == true) {
678 // somebody wants to read every new measured value, even if it is trash!
679 // do not actually send away data !
680 // just prepare the data to be send away.
681 if ( ad7719_new_measurement == true ) {
682 add_str_to_output_stream("ad7719: reading:");
683 add_dec_to_output_stream(reading_since_last_muxer_switch,1);
684 add_str_to_output_stream(" temperature channel:");
685 add_dec_to_output_stream(current_temperature_channel,2);
686 add_str_to_output_stream(" = ");
687 add_float_to_output_stream(current_ad7719_value,4,3);
688 add_str_to_output_stream("\n");
689 }
690 if (atmega_adc_new_measurement == true) {
691 add_str_to_output_stream("atmega_adc: reading:");
692 add_dec_to_output_stream(reading_since_last_muxer_switch,1);
693 add_str_to_output_stream(" voltage channel:");
694 add_dec_to_output_stream(current_voltage_channel,2);
695 add_str_to_output_stream(" = ");
696 add_float_to_output_stream(current_atmega_adc_value,4,3);
697 add_str_to_output_stream("\n");
698 }
699 } // end of: if verbose
700*/
701} // end of talk()
702
703// this function generates some output.
704void ad7719_output(U08 channel, U32 data) {
705float value = 0;
706 usart_write_str((pU08)"R:"); //R for resistance
707 usart_write_char('A'+channel/8); // Letters A,B,C,D,E,F,G,H
708 //usart_write_char(' ');
709 usart_write_U08(channel%8+1,1); // Numbers 1...8
710 usart_write_char(':');
711
712
713 value = (6.25 * data) / ((U32)1 << 25);
714 usart_write_float(value, 3,6);
715 //usart_write_U32_hex(data); //data
716
717
718}
719
720void adc_output(U08 channel, U08 data) {
721
722// if (channel < 40)
723// usart_write_str((pU08)"V:");
724// else if (channel < 80)
725// usart_write_str((pU08)"I:");
726// else if (channel < 84)
727// usart_write_str((pU08)"H:");
728
729 if (channel <80)
730 {
731 switch ((channel%40)/4) {
732 case 0:
733 case 1:
734 usart_write_char('A');
735 break;
736 case 2:
737 case 3:
738 usart_write_char('B');
739 break;
740 case 4:
741 case 5:
742 usart_write_char('C');
743 break;
744 case 6:
745 case 7:
746 usart_write_char('D');
747 break;
748 case 8:
749 usart_write_char('E');
750 break;
751 case 9:
752 usart_write_char('F');
753 break;
754 default:
755 usart_write_char('?');
756 break;
757 }
758 }
759 else // channel 80..83
760 {
761 usart_write_char('H');
762 }
763 //usart_write_char(' ');
764
765 if ( (channel%40)/4 == 9)
766 usart_write_U08((channel)%4+1,1); // Numbers 1...4
767 else
768 usart_write_U08((channel)%8+1,1); // Numbers 1...8
769
770
771 //usart_write_U08(channel,2); // Numbers 1...8
772 usart_write_char(':');
773 usart_write_U16((U16)data*16, 4); //data
774}
775
776
777void adc_output_all() {
778 print_adc_nicely();
779 print_ad7719_nicely();
780}
781
782
783// this method parses the data,
784// which came in via USART
785// later it might as well parse the data from ethernet.
786void parse() {
787U08 command = usart_rx_buffer[0];
788// look at first byte
789// I hope, I can manage to use one byte commands
790 usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
791 usart_write_str((pU08)"got:");
792 usart_write_str(usart_rx_buffer);
793
794
795
796 switch (command) {
797 case 'E': // AD7719 enable bitmaps may be set
798 usart_write_str((pU08)"\n set enable bits of AD7719 Port ");
799 if ((usart_rx_buffer_index>=5) &&
800 (usart_rx_buffer[2] >= 'A' && usart_rx_buffer[2] <= 'H'))
801 {
802 usart_write_char(usart_rx_buffer[2]);
803 usart_write_str((pU08)" to ");
804 usart_write_U08_hex(usart_rx_buffer[4]);
805 usart_write_char('\n');
806 ad7719_enables[usart_rx_buffer[2]-'A']=usart_rx_buffer[4];
807 ad7719_channels_ready[usart_rx_buffer[2]-'A']=0x00;
808 }
809 else if ((usart_rx_buffer_index=3) &&
810 (usart_rx_buffer[1] >= 'A' && usart_rx_buffer[1] <= 'H'))
811 {
812 usart_write_char(usart_rx_buffer[1]);
813 if (usart_rx_buffer[2]!='0') {
814 usart_write_str((pU08)" to 0xFF\n");
815 ad7719_enables[usart_rx_buffer[1]-'A']=0xFF;
816 } else
817 {
818 usart_write_str((pU08)" to 0x00\n");
819 ad7719_enables[usart_rx_buffer[1]-'A']=0x00;
820 }
821 ad7719_channels_ready[usart_rx_buffer[1]-'A']=0x00;
822 }
823 else
824 {
825 usart_write_str((pU08)"\n something wrong\n");
826 usart_write_str((pU08)"usart_rx_buffer_index: ");
827 usart_write_U08(usart_rx_buffer_index, 3);
828 usart_write_str((pU08)"\n usart_rx_buffer[2]: ");
829 usart_write_char(usart_rx_buffer[2]);
830 usart_write_str((pU08)"\n usart_rx_buffer[4]: ");
831 usart_write_U08_hex(usart_rx_buffer[4]);
832 usart_write_char('\n');
833 }
834 break;
835 case 'e': // ATmega internal ADC enable bitmaps may be set
836 usart_write_str((pU08)"\n setting ADC enable registers all");
837 if (usart_rx_buffer[1] == '0'){
838 usart_write_str((pU08)"OFF \n");
839 for ( U08 i=0; i<V_BITMAP + I_BITMAP; ++i ) {
840 adc_enables[i]=0x00;
841 adc_channels_ready[i]=0;
842 }
843 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP -1] = 0x00;
844 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP -1]=0;
845 } else {
846 usart_write_str((pU08)"ON\n");
847 for ( U08 i=0; i<V_BITMAP + I_BITMAP; ++i ) {
848 adc_enables[i]=0xFF;
849 adc_channels_ready[i]=0;
850 }
851 adc_enables[V_BITMAP + I_BITMAP + H_BITMAP -1] = 0x0F;
852 adc_channels_ready[V_BITMAP + I_BITMAP + H_BITMAP -1]=0;
853 }
854
855
856 break;
857 case 'h':
858 usart_write_str((pU08)"\nheartbeat ");
859 heartbeat_enable = true;
860 if (usart_rx_buffer[1] == '0'){
861 heartbeat_enable = false;
862 usart_write_str((pU08)"off\n");
863 } else {
864 usart_write_str((pU08)"on\n");
865 }
866 break;
867 case 'G': // GET the Temperature channels, which are enabled
868 once_told_you = false;
869 for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
870 ad7719_channels_ready[i]=0;
871 }
872 break;
873 case 'g': // GET the voltage/current/humidity channels, which are enabled
874 once_told_you = false;
875 for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) {
876 adc_channels_ready[i]=0;
877 }
878 break;
879
880 case 'Q':
881 for (U08 i=0; i< TEMP_CHANNELS;++i) {
882 if (i%8 == 0) usart_write_char('\n');
883 usart_write_U32_hex(ad7719_values[i]);
884 usart_write_char('\t');
885 }
886 usart_write_char('\n');
887 break;
888
889 case 'q':
890 // output: U08 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS];
891 for (U08 i=0; i< V_CHANNELS + I_CHANNELS + H_CHANNELS;++i) {
892 if (i%8 == 0) usart_write_char('\n');
893 usart_write_U16(adc_values[i]*16, 5);
894 usart_write_char('\t');
895 }
896 usart_write_char('\n');
897 break;
898
899 case 'P':
900 print_ad7719_nicely();
901 break;
902
903 case 'p':
904 print_adc_nicely();
905 break;
906
907
908
909 case 's':
910
911 usart_write_str((pU08)"adc status:\n");
912 for (U08 i=0; i< V_BITMAP + I_BITMAP + H_BITMAP;++i) {
913 usart_write_U08_bin(adc_enables[i]);
914 usart_write_char(' ');
915 }
916 usart_write_char('\n');
917 for (U08 i=0; i< V_BITMAP + I_BITMAP + H_BITMAP;++i){
918 usart_write_U08_bin(adc_channels_ready[i]);
919 usart_write_char(' ');
920 }
921 usart_write_char('\n');
922
923 usart_write_str((pU08)"ad7719 status:\n");
924 for (U08 i=0; i< CHANNEL_BITMAP;++i) {
925 usart_write_U08_bin(ad7719_enables[i]);
926 usart_write_char(' ');
927 }
928 usart_write_char('\n');
929 for (U08 i=0; i< CHANNEL_BITMAP;++i){
930 usart_write_U08_bin(ad7719_channels_ready[i]);
931 usart_write_char(' ');
932 }
933 usart_write_char('\n');
934
935 usart_write_str((pU08)"time:");
936 usart_write_float((float)local_ms/1000 , 1,7);
937 usart_write_str((pU08)" sec.\n");
938
939 usart_write_str((pU08)"adc measured all: ");
940 if (adc_measured_all)
941 usart_write_str((pU08)" true\n");
942 else
943 usart_write_str((pU08)"false\n");
944
945 usart_write_str((pU08)"ad7719 measured all: ");
946 if (ad7719_measured_all)
947 usart_write_str((pU08)" true\n");
948 else
949 usart_write_str((pU08)"false\n");
950
951 usart_write_str((pU08)"adc current channel:");
952 usart_write_U08(adc_current_channel,2);
953 usart_write_char('\n');
954
955 usart_write_str((pU08)"ad7719 current channel:");
956 usart_write_U08(ad7719_current_channel,2);
957 usart_write_char('\n');
958 break;
959
960 case 'd':
961 usart_write_str((pU08)"\ndebug mode ");
962 debug_mode = true;
963 if (usart_rx_buffer[1] == '0'){
964 debug_mode = false;
965 usart_write_str((pU08)"off\n");
966 } else {
967 usart_write_str((pU08)"on\n");
968 }
969 break;
970 }
971
972
973 usart_write_str((pU08)"\nready?");
974 for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
975 usart_rx_buffer[i] = 0;
976}
977
978void check_if_measured_all() {
979 adc_measured_all = true;
980 for ( U08 i=0; i<V_BITMAP + I_BITMAP + H_BITMAP; ++i ) {
981 if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
982 adc_measured_all = false;
983 break;
984 }
985 }
986 ad7719_measured_all = true;
987 for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
988 if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
989 ad7719_measured_all = false;
990 break;
991 }
992 }
993
994
995}
996
997void print_ad7719_nicely()
998{
999 float value;
1000
1001 usart_write_str((pU08)"\n printing measured resistance in kohms:\n");
1002
1003for (U08 i=0; i< TEMP_CHANNELS;++i) {
1004 if (i%8 == 0) usart_write_char('\n');
1005
1006 // print channel name:
1007 usart_write_str((pU08)"R:"); //R for resistance
1008 usart_write_char('A'+i/8); // Letters A,B,C,D,E,F,G,H
1009 //usart_write_char(' ');
1010 usart_write_U08(i%8+1,1); // Numbers 1...8
1011 usart_write_char(':');
1012
1013 // check if this channel is enabled in the bitmap
1014 if (ad7719_enables[i/8] & (1<<i%8))
1015 {
1016 value = (6.25 * 1.024 * ad7719_values[i]) / ((U32)1 << 25);
1017 usart_write_float(value, 3,6);
1018 //usart_write_U32(ad7719_values[i],8);
1019 //usart_write_U32_hex(data); //data
1020 usart_write_str((pU08)" ");
1021 } else {
1022 usart_write_str((pU08)" ");
1023 }
1024 //usart_write_char('\n');
1025}
1026}
1027
1028void print_adc_nicely() {
1029 usart_write_str((pU08)"\n printing voltages in mV:\n");
1030 // output: U08 adc_values[V_CHANNELS + I_CHANNELS + H_CHANNELS];
1031 for (U08 i=0; i< V_CHANNELS + I_CHANNELS + H_CHANNELS;++i) {
1032 if (i%8 == 0) usart_write_char('\n');
1033
1034 if (i==0)
1035 usart_write_str((pU08)"voltages:\n");
1036 if (i==40)
1037 usart_write_str((pU08)"currents in mV :-) :\n");
1038 if (i==80)
1039 usart_write_str((pU08)"humiditiesin mV :-) :\n");
1040
1041
1042
1043 adc_output(i, adc_values[i]);
1044 usart_write_str((pU08)" ");
1045 }
1046 usart_write_char('\n');
1047}
Note: See TracBrowser for help on using the repository browser.