//----------------------------------------------------------------------------- #include "typedefs.h" #include "application.h" #include "spi_master.h" #include "ad7719_adc.h" #include "atmega_adc.h" #include "usart.h" #include "macros.h" #include "muxer_fsc.h" #include "output.h" #include "parser.h" #include "interpol.h" #include "w5100_spi_interface.h" #include "timer.h" #include #include #include // MAIN WORKFLOW GLOBAL VARIABLES bool verbose; // should go to w5100.c or so bool heartbeat_enable; bool ad7719_measured_all = false; bool ad7719_values_printed = true; bool ad7719_print_endless = false; // ATMEGA ADC global variables bool adc_measured_all = false; bool adc_values_printed = true; bool adc_print_endless = false; bool once_told_you = true; bool debug_mode = false; //----------------------------------------------------------------------------- // M A I N --- M A I N --- M A I N --- M A I N --- M A I N //----------------------------------------------------------------------------- int main(void) { U08 highbyte; U08 lowbyte; U16 eth_red_bytes; U08 rc; app_init(); // Setup: Watchdog and I/Os usart_init(); spi_init(); // Initialize SPI interface as master timer_init(); // set up TIMER2: TIMER2_COMP interrupt occurs every 1ms atmega_adc_init(); w5100_reset(); w5100_init(); ad7719_init(); // Enable interrupts sei(); for ( U08 i=0; i < (RESISTANCE_CHANNELS/8); ++i ) { ad7719_enables[i]=0xFF; ad7719_channels_ready[i]=0; } for ( U08 i=0; i < (VOLTAGE_REGS); ++i ) { if (i < (VOLTAGE_REGS)-1) adc_enables[i]=0xFF; else adc_enables[i]=0x0F; adc_channels_ready[i]=0; } static U08 welcome[]="FSC 0.2 \n build: 05.06.2010\n"; usart_write_str(welcome); /* print_help(); print_adc_enable_status(true); usart_write_char('\n'); print_ad7719_enable_status(true); usart_write_char('\n'); usart_write_str((pU08)"time:"); usart_write_U32(sec , 7); usart_write_str((pU08)" sec.\n"); */ //MAIN LOOP while (1) { // checks if socket is okay and resets in case of problem the W5100 rc=w5100_caretaker(); if ( rc != 0x17 && rc != 0x14) { usart_write_str((pU08)"caretaker! error code:"); usart_write_U08_hex(rc); usart_write_crlf(); // something is not okay with the ethernet ... // will be reset in the next revolution.. but maybe we want to do more... } // this if should check every 25ms if we have any data on the W5100 if ( (milisec % W5100_INPUT_CHECK_TIME == 0) && (w5100_ready) ) { if ( (eth_red_bytes = get_S0_RX_RSR()) != 0) { for (U08 rep=0; rep<1; ) { if (eth_red_bytes == get_S0_RX_RSR() ) rep++; else eth_red_bytes =get_S0_RX_RSR(); } usart_write_str((pU08)"ethgot:"); usart_write_U16(eth_red_bytes, 7); usart_write_crlf(); parse_w5300_incoming( w5100_get_RX(ETH_READ_BUFFER_SIZE, true) ); } } //---------------------------------------------------------------------------- if (check_if_adc_measurement_done()) { if(adc_print_endless){ usart_write_str((pU08)"V|"); usart_write_U32(sec ,8); print_adc_nicely(false,true); write_status_via_eth(); //print_adc_stupid(); reset_voltage_done(); reset_voltage_values(); //telegram_start(); //w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE); reset_voltage_done(); } else if ( !adc_values_printed) { adc_values_printed = true; print_adc_nicely(true,true); write_status_via_eth(); //print_adc_stupid(); //reset_voltage_done(); //telegram_start(); //w5100_set_TX((U08*) adc_values, ADC_VALUES_LEN_BYTE); } } if (check_if_ad7719_measurement_done()) { if(ad7719_print_endless){ print_ad7719_nicely(); write_status_via_eth(); reset_resistance_done(); reset_voltage_done(); reset_voltage_values(); //telegram_start(); //w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE); } else if ( !ad7719_values_printed) { ad7719_values_printed = true; print_ad7719_nicely(true,true); write_status_via_eth(); reset_resistance_done(); //telegram_start(); //w5100_set_TX((U08*) ad7719_values, AD7719_VALUES_LEN_BYTE); } } //---------------------------------------------------------------------------- // in order to check the user interface on a hardware near level. // user can issue 'heartbeat command'. // then this üpin is toggled with the 'main-while-loop-frequency' // --> nice check for this frequency as well if (heartbeat_enable) { PORTA ^= (1< 0) && (UCSRA & (1<= ADC_READINGS_UNTIL_SETTLED-1) { adc_values[*adc_current_channel] += *adc_current_reading; if (debug_mode) { usart_write_str((pU08)"ch:"); usart_write_U08(*adc_current_channel,4); usart_write_str((pU08)" v:"); usart_write_U16_hex(*adc_current_reading); usart_write_str((pU08)" sum:"); usart_write_U16(adc_values[*adc_current_channel],8); usart_write_crlf(); } ++(*adc_readings_since_last_muxing); } else { ++(*adc_readings_since_last_muxing); } } //---------------------------------------------------------------------------- //IF AD7719 ADC just finished a conversion -- every 60ms if (AD7719_IS_READY() && !ad7719_measured_all) { *ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us // AD7719 is only read out if settled. saves time. if (*ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) { ad7719_values[*ad7719_current_channel] = *ad7719_current_reading; *ad7719_readings_since_last_muxing=0; if (debug_mode) { usart_write_U32_hex(*ad7719_current_reading); usart_write_char('\n'); usart_write_char('\n'); } // now prepare the data to be send away via USART. // note that this channel is ready, now and // proceed to the next enabled channel. ad7719_channels_ready[*ad7719_current_channel/8] |= (1<<(*ad7719_current_channel%8)); *ad7719_current_channel = increase_ad7719 (*ad7719_current_channel); Set_T_Muxer(*ad7719_current_channel); } else { // the AD7719 did not settle yet, we discard the reading ++(*ad7719_readings_since_last_muxing); if (debug_mode) { usart_write_U32_hex(*ad7719_current_reading); usart_write_char('\n'); } // current reading is not used for anything else } } } // end of MAIN LOOP } // end of main function //----------------------------------------------------------------------------- // E N D E N D E N D E N D E N D E N D E N D //-----------------------------------------------------------------------------