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