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.
|
---|
18 | U08 increase_adc (U08 channel);
|
---|
19 | U08 increase_ad7719 (U08 channel);
|
---|
20 | void Set_V_Muxer (U08 channel);
|
---|
21 | void Set_T_Muxer(U08 channel);
|
---|
22 | void parse();
|
---|
23 | void check_if_measured_all() ;
|
---|
24 | void print_ad7719_nicely(bool with_header, bool with_LF) ;
|
---|
25 | void print_adc_nicely(bool with_header, bool with_LF) ;
|
---|
26 |
|
---|
27 | void print_invalid_enable_statement();
|
---|
28 | void print_help() ; // is not printin help at the moment .... eats up too much RAM
|
---|
29 | void print_adc_enable_status(bool with_headline);
|
---|
30 | void print_ad7719_enable_status(bool with_headline) ;
|
---|
31 |
|
---|
32 | bool check_if_adc_measurement_done();
|
---|
33 | bool check_if_ad7719_measurement_done();
|
---|
34 |
|
---|
35 |
|
---|
36 | // end of function definition:
|
---|
37 | //-----------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | // Reference resistance
|
---|
40 | float r_reference = 6.257;
|
---|
41 |
|
---|
42 | // MAIN WORKFLOW GLOBAL VARIABLES
|
---|
43 | bool verbose;
|
---|
44 | bool heartbeat_enable;
|
---|
45 |
|
---|
46 | // USART global variables
|
---|
47 | U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
|
---|
48 | U08 usart_tx_buffer[USART_TX_BUFFER_SIZE];
|
---|
49 | U08 usart_received_chars;
|
---|
50 | U08 usart_rx_buffer_index = 0;
|
---|
51 | U08 usart_tx_buffer_index = 0;
|
---|
52 | U08 usart_last_char; // last received char
|
---|
53 |
|
---|
54 | // USART FLAGS
|
---|
55 | bool usart_tx_buffer_overflow = false; // true if usart_tx_buffer was full.
|
---|
56 | bool usart_rx_ready = false; // EOL was received, parser needs to be called
|
---|
57 |
|
---|
58 | // TIMER global variable
|
---|
59 | volatile U32 local_ms = 0;
|
---|
60 |
|
---|
61 | // AD7719 global variables
|
---|
62 | #define TEMP_CHANNELS 16
|
---|
63 | #define CHANNEL_BITMAP 2
|
---|
64 | #define AD7719_READINGS_UNTIL_SETTLED 3 // bei3:480ms
|
---|
65 | U32 ad7719_values[TEMP_CHANNELS];
|
---|
66 | U08 ad7719_enables[CHANNEL_BITMAP];
|
---|
67 | U08 ad7719_channels_ready[CHANNEL_BITMAP];
|
---|
68 | U08 ad7719_readings_since_last_muxing = 0;
|
---|
69 | U08 ad7719_current_channel = 0;
|
---|
70 | U32 ad7719_current_reading = 0;
|
---|
71 | bool ad7719_measured_all = false;
|
---|
72 | bool ad7719_values_printed = true;
|
---|
73 | bool ad7719_print_endless = false;
|
---|
74 |
|
---|
75 | // ATMEGA ADC global variables
|
---|
76 | #define V_CHANNELS 16
|
---|
77 | #define V_BITMAP 2
|
---|
78 | #define ADC_READINGS_UNTIL_SETTLED 1
|
---|
79 | U08 adc_readings_until_mean=250;
|
---|
80 | U32 adc_values[V_CHANNELS]; // stores measured voltage in steps of 16mV
|
---|
81 | U08 adc_enables[V_BITMAP];
|
---|
82 | U08 adc_channels_ready[V_BITMAP];
|
---|
83 | U08 adc_readings_since_last_muxing = 0;
|
---|
84 | U08 adc_current_channel = 0;
|
---|
85 | U16 adc_current_reading = 0;
|
---|
86 | bool adc_measured_all = false;
|
---|
87 | bool adc_values_printed = true;
|
---|
88 | bool adc_print_endless = false;
|
---|
89 |
|
---|
90 | bool debug_mode = false;
|
---|
91 |
|
---|
92 | //U32 hb_counter = 0;
|
---|
93 |
|
---|
94 | //-----------------------------------------------------------------------------
|
---|
95 | // M A I N --- M A I N --- M A I N --- M A I N --- M A I N
|
---|
96 | //-----------------------------------------------------------------------------
|
---|
97 | int main(void)
|
---|
98 | {
|
---|
99 | app_init(); // Setup: Watchdog and I/Os
|
---|
100 | usart_init(); // Initialize serial interface
|
---|
101 | spi_init(); // Initialize SPI interface as master
|
---|
102 | ad7719_init(); // Initialize AD7719 ADC as SPI slave
|
---|
103 | atmega_adc_init();
|
---|
104 |
|
---|
105 | // TIMER2 is used as local clock:
|
---|
106 | // configure timer 2
|
---|
107 | TCCR2 = (1<<WGM21); // CTC Modus
|
---|
108 | TCCR2 |= (1<<CS21) | (1<<CS20); // Prescaler 64 --> counts up every 8us
|
---|
109 | OCR2 = 125-1; // --> output compare interrupt occurs every 125 x 8us = 1ms
|
---|
110 | // Compare Interrupt erlauben
|
---|
111 | TIMSK |= (1<<OCIE2);
|
---|
112 |
|
---|
113 | // Enable interrupts
|
---|
114 | sei();
|
---|
115 |
|
---|
116 | PORTB &= ~(1<<PB2);
|
---|
117 |
|
---|
118 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
|
---|
119 | ad7719_enables[1]=0x00;
|
---|
120 | ad7719_enables[0]=0xFF;
|
---|
121 |
|
---|
122 | // ad7719_enables[i]=0x00;
|
---|
123 | ad7719_channels_ready[i]=0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | for ( U08 i=0; i<V_BITMAP; ++i ) {
|
---|
127 | adc_enables[1]=0x33;
|
---|
128 | adc_enables[0]=0xFF;
|
---|
129 | // adc_enables[i]=0x00;
|
---|
130 | adc_channels_ready[i]=0;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | static U08 welcome[]="welcome to:\n MSR command interface 1.0 \n build: 29.03.2010\n";
|
---|
135 | usart_write_str(welcome);
|
---|
136 | print_help();
|
---|
137 | print_adc_enable_status(true);
|
---|
138 | usart_write_char('\n');
|
---|
139 | print_ad7719_enable_status(true);
|
---|
140 | usart_write_char('\n');
|
---|
141 | usart_write_str((pU08)"time:");
|
---|
142 | usart_write_float((float)local_ms/1000 , 1,7);
|
---|
143 | usart_write_str((pU08)" sec.\n");
|
---|
144 |
|
---|
145 | //MAIN LOOP
|
---|
146 | while (1)
|
---|
147 | {
|
---|
148 | if (check_if_adc_measurement_done()) {
|
---|
149 | if(adc_print_endless){
|
---|
150 | usart_write_str((pU08)"V|");
|
---|
151 | usart_write_float((float)local_ms/1000 , 1,4);
|
---|
152 | print_adc_nicely(false,true);
|
---|
153 | for ( U08 i=0; i<V_BITMAP; ++i ) {
|
---|
154 | adc_channels_ready[i]=0;
|
---|
155 | }
|
---|
156 | } else
|
---|
157 | if ( !adc_values_printed) {
|
---|
158 | adc_values_printed = true;
|
---|
159 | print_adc_nicely(true,true);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (check_if_ad7719_measurement_done()) {
|
---|
164 | if(ad7719_print_endless){
|
---|
165 | usart_write_str((pU08)"R|");
|
---|
166 | usart_write_float((float)local_ms/1000 , 1,4);
|
---|
167 | print_ad7719_nicely(false,true);
|
---|
168 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
|
---|
169 | ad7719_channels_ready[i]=0;
|
---|
170 | }
|
---|
171 | } else
|
---|
172 | if ( !ad7719_values_printed) {
|
---|
173 | ad7719_values_printed = true;
|
---|
174 | print_ad7719_nicely(true,true);
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | //----------------------------------------------------------------------------
|
---|
179 |
|
---|
180 | if (heartbeat_enable) {
|
---|
181 | PORTA ^= (1<<PA4); // toggle PA4 --> heartbeat
|
---|
182 |
|
---|
183 | // warte 100.000 heartbeats und schreibe die Zeit raus
|
---|
184 | //hb_counter++;
|
---|
185 | //if (hb_counter == 100000){
|
---|
186 | // hb_counter = 0;
|
---|
187 | // usart_write_float((float)local_ms/1000 , 1,7);
|
---|
188 | // usart_write_str((pU08)" sec.\n");
|
---|
189 | //}
|
---|
190 | }
|
---|
191 |
|
---|
192 | //----------------------------------------------------------------------------
|
---|
193 | //IF we need to send away one byte, and ready to send
|
---|
194 |
|
---|
195 | if ( (usart_tx_buffer_index > 0) && (UCSRA & (1<<UDRE)) ) {
|
---|
196 | UDR = usart_tx_buffer[0];
|
---|
197 | // THis is shit
|
---|
198 | for (U08 i=0 ; i < USART_TX_BUFFER_SIZE; ++i) {
|
---|
199 | usart_tx_buffer[i] = usart_tx_buffer[i+1];
|
---|
200 | }
|
---|
201 | usart_tx_buffer_index--;
|
---|
202 | }
|
---|
203 | //----------------------------------------------------------------------------
|
---|
204 |
|
---|
205 | //IF we just received one byte
|
---|
206 | if ( UCSRA & (1<<RXC) )
|
---|
207 | {
|
---|
208 | usart_last_char = UDR; // read the byte from ATmega internal buffer
|
---|
209 | // what are we going to do with this byte?
|
---|
210 | if (usart_rx_buffer_index < USART_RX_BUFFER_SIZE ) // if space in the buffer ... we store it.
|
---|
211 | {
|
---|
212 | usart_rx_buffer[usart_rx_buffer_index] = usart_last_char;
|
---|
213 | usart_rx_buffer_index++;
|
---|
214 | if (usart_last_char == '\n'){ // if EOL was received
|
---|
215 | usart_rx_buffer_index--;
|
---|
216 | usart_rx_buffer[usart_rx_buffer_index] = 0;
|
---|
217 | usart_rx_ready = true;
|
---|
218 | }
|
---|
219 | } else { // if no space in buffer ... we try to parse()
|
---|
220 | usart_write_str((pU08)"receive buffer overflow ... parsing now\n");
|
---|
221 | usart_rx_ready = true;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | //----------------------------------------------------------------------------
|
---|
226 |
|
---|
227 | //IF USART DOR bit is set, PC is sending data to fast!!!
|
---|
228 | if ( UCSRA & (1<<DOR) ){
|
---|
229 | usart_write_str((pU08)"PC sending to fast!\n");
|
---|
230 | usart_last_char = UDR;
|
---|
231 | // flush TX_buffer and write warning message in
|
---|
232 | // maybe even switch off every measurement. ?
|
---|
233 | }
|
---|
234 | //----------------------------------------------------------------------------
|
---|
235 |
|
---|
236 | //IF TX_BUFFER was overrun.
|
---|
237 | if (usart_tx_buffer_overflow) {
|
---|
238 | // flash TX_buffer and write warning message in
|
---|
239 | // maybe even switch off every measurement. ?
|
---|
240 | //
|
---|
241 | // this should only happen, in verbose mode and with low baudrates.
|
---|
242 | }
|
---|
243 | //----------------------------------------------------------------------------
|
---|
244 |
|
---|
245 | //IF one command was received.
|
---|
246 | // -It is not allowed to send more than one command between two '\n'
|
---|
247 | if (usart_rx_ready){
|
---|
248 | parse();
|
---|
249 | usart_rx_ready = false;
|
---|
250 | for ( U08 i =0 ; i < USART_RX_BUFFER_SIZE; i++){
|
---|
251 | usart_rx_buffer[i]=0;
|
---|
252 | }
|
---|
253 | usart_rx_buffer_index = 0;
|
---|
254 |
|
---|
255 | }
|
---|
256 | //----------------------------------------------------------------------------
|
---|
257 |
|
---|
258 | //IF ATmega internal ADC did finish a conversion --every 200us
|
---|
259 | if ( (ADCSRA & (1<<ADIF)) && !adc_measured_all) {
|
---|
260 | adc_current_reading = (U16)ADCL;
|
---|
261 | adc_current_reading += (U16)ADCH<<8;
|
---|
262 | if (adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED+adc_readings_until_mean-1) {
|
---|
263 | adc_channels_ready[adc_current_channel/8] |= (1<<(adc_current_channel%8));
|
---|
264 | adc_current_channel = increase_adc (adc_current_channel);
|
---|
265 | adc_values[adc_current_channel] = 0;
|
---|
266 | Set_V_Muxer(adc_current_channel);
|
---|
267 | adc_readings_since_last_muxing = 0;
|
---|
268 | _delay_ms(10);
|
---|
269 | }
|
---|
270 | else if (adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
|
---|
271 | adc_values[adc_current_channel] += adc_current_reading;
|
---|
272 | ++adc_readings_since_last_muxing;
|
---|
273 | }
|
---|
274 | else {
|
---|
275 | ++adc_readings_since_last_muxing;
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 | //----------------------------------------------------------------------------
|
---|
280 |
|
---|
281 | //IF AD7719 ADC just finished a conversion -- every 60ms
|
---|
282 |
|
---|
283 | if (AD7719_IS_READY() && !ad7719_measured_all) {
|
---|
284 |
|
---|
285 |
|
---|
286 | ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
|
---|
287 | // AD7719 is only read out if settled. saves time.
|
---|
288 | if (ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
|
---|
289 | ad7719_values[ad7719_current_channel] = ad7719_current_reading;
|
---|
290 | ad7719_readings_since_last_muxing=0;
|
---|
291 | if (debug_mode) {
|
---|
292 | usart_write_U32_hex(ad7719_current_reading);
|
---|
293 | usart_write_char('\n');
|
---|
294 | usart_write_char('\n');
|
---|
295 | }
|
---|
296 | // now prepare the data to be send away via USART.
|
---|
297 |
|
---|
298 | // note that this channel is ready, now and
|
---|
299 | // proceed to the next enabled channel.
|
---|
300 | ad7719_channels_ready[ad7719_current_channel/8] |= (1<<(ad7719_current_channel%8));
|
---|
301 | ad7719_current_channel = increase_ad7719 (ad7719_current_channel);
|
---|
302 | Set_T_Muxer(ad7719_current_channel);
|
---|
303 | } else { // the AD7719 did not settle yet, we discard the reading
|
---|
304 | ++ad7719_readings_since_last_muxing;
|
---|
305 | if (debug_mode) {
|
---|
306 | usart_write_U32_hex(ad7719_current_reading);
|
---|
307 | usart_write_char('\n');
|
---|
308 | }
|
---|
309 |
|
---|
310 | // current reading is not used for anything else
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | } // end of MAIN LOOP
|
---|
316 |
|
---|
317 | } // end of main function
|
---|
318 | //-----------------------------------------------------------------------------
|
---|
319 | // E N D E N D E N D E N D E N D E N D E N D
|
---|
320 | //-----------------------------------------------------------------------------
|
---|
321 |
|
---|
322 |
|
---|
323 | ISR (TIMER2_COMP_vect)
|
---|
324 | {
|
---|
325 | ++local_ms;
|
---|
326 | }
|
---|
327 |
|
---|
328 | U08 increase_adc (U08 channel){
|
---|
329 | U08 effective_channel;
|
---|
330 | for ( U08 increase = 1 ; increase <= V_CHANNELS; increase++)
|
---|
331 | {
|
---|
332 | effective_channel = (channel + increase) % (V_CHANNELS);
|
---|
333 | if (adc_enables[effective_channel/8] & (1<<effective_channel%8))
|
---|
334 | return effective_channel;
|
---|
335 | }
|
---|
336 | return channel;
|
---|
337 | } // end if increase_adc;
|
---|
338 |
|
---|
339 | U08 increase_ad7719 (U08 channel){
|
---|
340 | U08 effective_channel;
|
---|
341 | for ( U08 increase = 1 ; increase <= TEMP_CHANNELS; increase++)
|
---|
342 | {
|
---|
343 | effective_channel = (channel + increase) % (TEMP_CHANNELS);
|
---|
344 | if (ad7719_enables[effective_channel/8] & (1<<effective_channel%8))
|
---|
345 | return effective_channel;
|
---|
346 | }
|
---|
347 | return channel;
|
---|
348 | } // end if increase_adc;
|
---|
349 |
|
---|
350 | // Sets voltage Muxer to current channel
|
---|
351 | void Set_V_Muxer (U08 channel){
|
---|
352 | PORTC = (PORTC & 0xF0) | (0x0F & channel);
|
---|
353 | }
|
---|
354 |
|
---|
355 | void Set_T_Muxer(U08 channel) {
|
---|
356 | PORTA = (PORTA & 0xF0) | (0x0F & channel); // Here the muxer is switched.
|
---|
357 | }
|
---|
358 |
|
---|
359 | bool check_if_adc_measurement_done(){
|
---|
360 | adc_measured_all = true;
|
---|
361 | for ( U08 i=0; i<V_BITMAP; ++i ) {
|
---|
362 | if ((adc_enables[i] ^ adc_channels_ready[i]) != 0x00) {
|
---|
363 | adc_measured_all = false;
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | return adc_measured_all;
|
---|
368 | }
|
---|
369 |
|
---|
370 | bool check_if_ad7719_measurement_done(){
|
---|
371 | ad7719_measured_all = true;
|
---|
372 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
|
---|
373 | if ((ad7719_enables[i] ^ ad7719_channels_ready[i]) != 0x00) {
|
---|
374 | ad7719_measured_all = false;
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | return ad7719_measured_all;
|
---|
379 | }
|
---|
380 |
|
---|
381 | void print_ad7719_nicely(bool with_header, bool with_LF) {
|
---|
382 | float value_in_ohms;
|
---|
383 | if (with_header) {
|
---|
384 |
|
---|
385 | usart_write_str((pU08)"\n resistance(Ohm)\n (note: only enabled channels are printed):\n");
|
---|
386 |
|
---|
387 | // Überschrift alles in einer Zeile, hoffentlich sieht das gut aus.
|
---|
388 | for (U08 i=0; i< TEMP_CHANNELS;++i) {
|
---|
389 | usart_write_str((pU08)"|R"); // 1 letter -- R
|
---|
390 | usart_write_U08(i, 2); // 2 letters -- the channel number
|
---|
391 | usart_write_str((pU08)" "); // 5 spaces
|
---|
392 | }
|
---|
393 | usart_write_str((pU08)"|\n");
|
---|
394 | }
|
---|
395 |
|
---|
396 | // Jetzt die Werte, auch alle in einer Zeile
|
---|
397 | usart_write_char('|');
|
---|
398 | for (U08 i=0; i< TEMP_CHANNELS;++i) {
|
---|
399 |
|
---|
400 | if (ad7719_enables[i/8] & (1<<i%8))
|
---|
401 | {
|
---|
402 | value_in_ohms = (r_reference * 1024 * ad7719_values[i]) / ((U32)1 << 25);
|
---|
403 | if (value_in_ohms < 1000.0)
|
---|
404 | usart_write_char(' ');
|
---|
405 | if (value_in_ohms < 100.0)
|
---|
406 | usart_write_char(' ');
|
---|
407 | if (value_in_ohms < 10.0)
|
---|
408 | usart_write_char(' ');
|
---|
409 | usart_write_float(value_in_ohms, 1, 5); // 6 letters .. 1002.4 ohm .. this is 6 letter .. the dot . is also a letter
|
---|
410 | usart_write_char('|'); // 2 spaces
|
---|
411 | } else {
|
---|
412 | usart_write_str((pU08)" |"); // or 8 spaces
|
---|
413 | }
|
---|
414 | }
|
---|
415 | if (with_LF)
|
---|
416 | usart_write_char('\n');
|
---|
417 | }
|
---|
418 |
|
---|
419 | void print_adc_nicely(bool with_header, bool with_LF) {
|
---|
420 |
|
---|
421 | if (with_header) {
|
---|
422 | usart_write_str((pU08)"Voltage(mV), Humidity(mV) & Pressure(mV):\n");
|
---|
423 |
|
---|
424 | for (U08 i=0; i < 8; ++i) {
|
---|
425 | usart_write_str((pU08)"| V");
|
---|
426 | usart_write_U08(i, 1); //data
|
---|
427 | usart_write_str((pU08)" ");
|
---|
428 | }
|
---|
429 |
|
---|
430 | for (U08 i=0; i < 4; ++i) {
|
---|
431 | usart_write_str((pU08)"| H");
|
---|
432 | usart_write_U08(i, 1); //data
|
---|
433 | usart_write_str((pU08)" ");
|
---|
434 | }
|
---|
435 | for (U08 i=0; i < 4; ++i) {
|
---|
436 | usart_write_str((pU08)"| P");
|
---|
437 | usart_write_U08(i, 1); //data
|
---|
438 | usart_write_str((pU08)" ");
|
---|
439 | }
|
---|
440 | usart_write_str((pU08)"|\n");
|
---|
441 | }
|
---|
442 |
|
---|
443 | // Jetzt die gemessenen Werte und zwar nur die Spannungen
|
---|
444 | for (U08 i=0; i < 16; ++i) {
|
---|
445 | usart_write_char('|');
|
---|
446 | if (adc_enables[i/8] & (1<<i%8))
|
---|
447 | usart_write_U32( ( ( adc_values[i] * 4 + 2 ) / adc_readings_until_mean ) , 6 );
|
---|
448 | else
|
---|
449 | usart_write_str((pU08)" ");
|
---|
450 | }
|
---|
451 | usart_write_char('|');
|
---|
452 |
|
---|
453 | if (with_LF)
|
---|
454 | usart_write_char('\n');
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | /////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
459 | /////////////// P A R S E R /////////////////////////////////////////////////////////////////////////
|
---|
460 | /////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
461 | // this method parses the data,
|
---|
462 | // which came in via USART
|
---|
463 | // later it might as well parse the data from ethernet.
|
---|
464 | void parse() {
|
---|
465 |
|
---|
466 | U08 input_number = 0;
|
---|
467 | bool enable = false;
|
---|
468 | // look at first byte
|
---|
469 | // I hope, I can manage to use one byte commands
|
---|
470 | usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
|
---|
471 | usart_write_char(' ');
|
---|
472 | usart_write_str(usart_rx_buffer);
|
---|
473 | usart_write_char('\n');
|
---|
474 | //usart_write_str((pU08)"\n received number of bytes:");
|
---|
475 | //usart_write_U08(usart_rx_buffer_index,3);
|
---|
476 |
|
---|
477 | if (usart_rx_buffer_index >0) {
|
---|
478 | switch (usart_rx_buffer[0]) {
|
---|
479 | case 'E': // user wants to enable/disable something
|
---|
480 | case 'e':
|
---|
481 | enable = true;
|
---|
482 | case 'D':
|
---|
483 | case 'd':
|
---|
484 |
|
---|
485 | if (usart_rx_buffer_index>=1) { // let's see what.
|
---|
486 | if ( usart_rx_buffer[1] != 'r' &&
|
---|
487 | usart_rx_buffer[1] != 'v' &&
|
---|
488 | usart_rx_buffer[1] != 'h' &&
|
---|
489 | usart_rx_buffer[1] != 'p' )
|
---|
490 | {
|
---|
491 | print_invalid_enable_statement();
|
---|
492 | break;
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | input_number = 0;
|
---|
497 | if (usart_rx_buffer_index >2) { // lets check the first digit
|
---|
498 | if ( usart_rx_buffer[2] >= '0' && usart_rx_buffer[2] <= '9' ) {
|
---|
499 | input_number = usart_rx_buffer[2] - '0';
|
---|
500 | } else {
|
---|
501 | print_invalid_enable_statement();
|
---|
502 | break;
|
---|
503 | }
|
---|
504 | }
|
---|
505 | if (usart_rx_buffer_index >3) { // lets check the 2nd digit
|
---|
506 | if ( usart_rx_buffer[3] >= '0' && usart_rx_buffer[3] <= '9' ) {
|
---|
507 | input_number = 10*input_number + usart_rx_buffer[3] - '0';
|
---|
508 | } else {
|
---|
509 | // okay as well ... if the second digit is missing ..
|
---|
510 | // we might have trailing spaces
|
---|
511 | // that's okay .. we just accept the first number.
|
---|
512 | }
|
---|
513 | }
|
---|
514 | if (usart_rx_buffer_index>2) {
|
---|
515 | usart_write_str((pU08)"\n I will switch ");
|
---|
516 | if (enable)
|
---|
517 | usart_write_str((pU08)"on ");
|
---|
518 | else
|
---|
519 | usart_write_str((pU08)"off ");
|
---|
520 |
|
---|
521 | // now we know, what the user wanted ... and we need to do it.
|
---|
522 | switch (usart_rx_buffer[1]) {
|
---|
523 | case 'r':
|
---|
524 | if (enable)
|
---|
525 | ad7719_enables[input_number/8] |= (1<<(input_number%8));
|
---|
526 | else {
|
---|
527 | ad7719_enables[input_number/8] &= ~(1<<(input_number%8));
|
---|
528 | ad7719_channels_ready[input_number/8] &= ~(1<<(input_number%8));
|
---|
529 | }
|
---|
530 | usart_write_str((pU08)" resistance channel ");
|
---|
531 | usart_write_U08(input_number,2);
|
---|
532 | usart_write_char('\n');
|
---|
533 | break;
|
---|
534 | case 'v':
|
---|
535 | if (enable)
|
---|
536 | adc_enables[input_number/8] |= (1<<(input_number%8));
|
---|
537 | else {
|
---|
538 | adc_enables[input_number/8] &= ~(1<<(input_number%8));
|
---|
539 | adc_channels_ready[input_number/8] &= ~(1<<(input_number%8));
|
---|
540 | }
|
---|
541 | usart_write_str((pU08)" voltage channel ");
|
---|
542 | usart_write_U08(input_number,2);
|
---|
543 | usart_write_char('\n');
|
---|
544 | break;
|
---|
545 |
|
---|
546 | case 'h':
|
---|
547 | if (enable)
|
---|
548 | adc_enables[1] |= (1<<(input_number%4));
|
---|
549 | else {
|
---|
550 | adc_enables[1] &= ~(1<<(input_number%4));
|
---|
551 | adc_channels_ready[1] &= ~(1<<(input_number%4));
|
---|
552 | }
|
---|
553 | usart_write_str((pU08)" humidity channel ");
|
---|
554 | usart_write_U08(input_number,2);
|
---|
555 | usart_write_char('\n');
|
---|
556 | break;
|
---|
557 | case 'p':
|
---|
558 | if (enable)
|
---|
559 | adc_enables[1] |= (1<<((input_number%4) + 4));
|
---|
560 | else {
|
---|
561 | adc_enables[1] &= ~(1<<((input_number%4) + 4));
|
---|
562 | adc_channels_ready[1] &= ~(1<<((input_number%4) + 4));
|
---|
563 | }
|
---|
564 | usart_write_str((pU08)" pressure channel ");
|
---|
565 | usart_write_U08(input_number,2);
|
---|
566 | usart_write_char('\n');
|
---|
567 | break;
|
---|
568 | default:
|
---|
569 | usart_write_str((pU08)"\n DAMN! this should never happen"
|
---|
570 | "- enable/disable switch-case!\n");
|
---|
571 | break;
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 |
|
---|
576 | }// end of if usart_rx_buffer_index>2 --> this should not be necessary at all
|
---|
577 | break;
|
---|
578 |
|
---|
579 | case 'b':
|
---|
580 | usart_write_str((pU08)"\nheartbeat ");
|
---|
581 | heartbeat_enable = true;
|
---|
582 | if (usart_rx_buffer[1] == '0'){
|
---|
583 | heartbeat_enable = false;
|
---|
584 | usart_write_str((pU08)"off\n");
|
---|
585 | } else {
|
---|
586 | usart_write_str((pU08)"on\n");
|
---|
587 | }
|
---|
588 | break;
|
---|
589 | case 'G': // GET the Temperature channels, which are enabled
|
---|
590 | ad7719_values_printed = false;
|
---|
591 | for ( U08 i=0; i<CHANNEL_BITMAP; ++i ) {
|
---|
592 | ad7719_channels_ready[i]=0;
|
---|
593 | }
|
---|
594 | break;
|
---|
595 | case 'g': // GET the voltage/current/humidity channels, which are enabled
|
---|
596 | adc_values_printed = false;
|
---|
597 | for ( U08 i=0; i<V_BITMAP; ++i ) {
|
---|
598 | adc_channels_ready[i]=0;
|
---|
599 | }
|
---|
600 | break;
|
---|
601 |
|
---|
602 | case 'p':
|
---|
603 | adc_print_endless = true;
|
---|
604 | if (usart_rx_buffer[1] == '0')
|
---|
605 | adc_print_endless = false;
|
---|
606 | break;
|
---|
607 |
|
---|
608 | case 'P':
|
---|
609 | ad7719_print_endless = true;
|
---|
610 | if (usart_rx_buffer[1] == '0')
|
---|
611 | ad7719_print_endless = false;
|
---|
612 | break;
|
---|
613 |
|
---|
614 |
|
---|
615 | case 's':
|
---|
616 | print_adc_enable_status(true);
|
---|
617 | usart_write_char('\n');
|
---|
618 |
|
---|
619 | print_ad7719_enable_status(true);
|
---|
620 | usart_write_char('\n');
|
---|
621 |
|
---|
622 | usart_write_str((pU08)"time:");
|
---|
623 | usart_write_float((float)local_ms/1000 , 1,7);
|
---|
624 | usart_write_str((pU08)" sec.\n");
|
---|
625 | break;
|
---|
626 |
|
---|
627 | case '!':
|
---|
628 | usart_write_str((pU08)"\ndebug mode ");
|
---|
629 | debug_mode = true;
|
---|
630 | if (usart_rx_buffer[1] == '0'){
|
---|
631 | debug_mode = false;
|
---|
632 | usart_write_str((pU08)"off\n");
|
---|
633 | } else {
|
---|
634 | usart_write_str((pU08)"on\n");
|
---|
635 | }
|
---|
636 | break;
|
---|
637 |
|
---|
638 | case 'h':
|
---|
639 | case 'H':
|
---|
640 | print_help();
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | } else
|
---|
644 | {
|
---|
645 | // zero bytes received
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
|
---|
650 | usart_rx_buffer[i] = 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | void print_invalid_enable_statement(){
|
---|
655 | usart_write_str((pU08)"\nerror: invalid 'enable' or 'disable'statement.\n");
|
---|
656 | usart_write_str((pU08)"usage: [E|e|D|d][r|v|h|p][0..15]\n");
|
---|
657 | usart_write_str((pU08)"e.g.: Ev1 --> enable voltage channel 1\n");
|
---|
658 | usart_write_str((pU08)"e.g.: dr0 --> disable resistance channel 0\n");
|
---|
659 | }
|
---|
660 |
|
---|
661 | void print_help() {
|
---|
662 |
|
---|
663 | usart_write_str((pU08)
|
---|
664 | "List of MSR commands:\n29.03.2011\n"
|
---|
665 | "E or e -- enable channel: type e? for detailed help\n"
|
---|
666 | "D or d -- disable channel: type d? for detailed help\n"
|
---|
667 | "b0 or b1 -- switch heartbeat PA4 on or off\n"
|
---|
668 | "G -- 'get AD7719' -> measure resistances again\n"
|
---|
669 | "g -- 'get ATmega adc' -> measure voltages again\n"
|
---|
670 | "P -- get & print AD7719 infinitly \n"
|
---|
671 | "p -- get & print ATmega adc infinitly \n"
|
---|
672 | "s -- print status\n"
|
---|
673 | "H or h -- print this help\n"
|
---|
674 | );
|
---|
675 |
|
---|
676 | }
|
---|
677 |
|
---|
678 | void print_adc_enable_status(bool with_headline) {
|
---|
679 |
|
---|
680 | if (with_headline) {
|
---|
681 | usart_write_str((pU08)"adc channels 15 downto 0:\n i.e. left is pressure and humidity ... right is voltage\n");
|
---|
682 | }
|
---|
683 | usart_write_U08_bin(adc_enables[1]);
|
---|
684 | usart_write_char(' ');
|
---|
685 | usart_write_U08_bin(adc_enables[0]);
|
---|
686 | usart_write_char(' ');
|
---|
687 |
|
---|
688 | usart_write_char('\n');
|
---|
689 | usart_write_U08_bin(adc_channels_ready[1]);
|
---|
690 | usart_write_char(' ');
|
---|
691 | usart_write_U08_bin(adc_channels_ready[0]);
|
---|
692 | usart_write_char(' ');
|
---|
693 | }
|
---|
694 |
|
---|
695 | void print_ad7719_enable_status(bool with_headline) {
|
---|
696 |
|
---|
697 | if (with_headline) {
|
---|
698 | usart_write_str((pU08)"ad7719 channels: 15 downto 0:\n");
|
---|
699 | }
|
---|
700 | usart_write_U08_bin(ad7719_enables[1]);
|
---|
701 | usart_write_char(' ');
|
---|
702 | usart_write_U08_bin(ad7719_enables[0]);
|
---|
703 | usart_write_char(' ');
|
---|
704 |
|
---|
705 | usart_write_char('\n');
|
---|
706 | usart_write_U08_bin(ad7719_channels_ready[1]);
|
---|
707 | usart_write_char(' ');
|
---|
708 | usart_write_U08_bin(ad7719_channels_ready[0]);
|
---|
709 |
|
---|
710 | }
|
---|
711 |
|
---|