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