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

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