source: firmware/FSC/usb_src/FSC.c@ 20049

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