| 1 | #include "typedefs.h"
|
|---|
| 2 | #include "application.h" // <-- global Registers
|
|---|
| 3 |
|
|---|
| 4 | #include "spi_master.h"
|
|---|
| 5 | #include "timer.h"
|
|---|
| 6 | #include "atmega_adc.h"
|
|---|
| 7 | #include "ad7719_adc.h"
|
|---|
| 8 | #include "w5100_spi_interface.h"
|
|---|
| 9 |
|
|---|
| 10 | #include "macros.h"
|
|---|
| 11 | #include "muxer_fsc.h"
|
|---|
| 12 |
|
|---|
| 13 | #include <avr/interrupt.h>
|
|---|
| 14 | #include <avr/wdt.h>
|
|---|
| 15 | #include <stdlib.h>
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | int main(void)
|
|---|
| 19 | {
|
|---|
| 20 | U08 highbyte;
|
|---|
| 21 | U08 lowbyte;
|
|---|
| 22 | U16 eth_red_bytes;
|
|---|
| 23 | U08 rc;
|
|---|
| 24 |
|
|---|
| 25 | gReg.ad7719_measured_all = false;
|
|---|
| 26 | gReg.adc_measured_all = false;
|
|---|
| 27 | for ( U08 i=0; i < (RESISTANCE_REGS); ++i ) {
|
|---|
| 28 | gReg.ad7719_enables[i]=0xFF;
|
|---|
| 29 | gReg.ad7719_channels_ready[i]=0x00;
|
|---|
| 30 | }
|
|---|
| 31 | for ( U08 i=0; i < (VOLTAGE_REGS); ++i ) {
|
|---|
| 32 | if (i < (VOLTAGE_REGS)-1) {
|
|---|
| 33 | gReg.adc_enables[i]=0xFF;
|
|---|
| 34 | }
|
|---|
| 35 | else {
|
|---|
| 36 | gReg.adc_enables[i]=0x0F;
|
|---|
| 37 | }
|
|---|
| 38 | gReg.adc_channels_ready[i]=0;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | app_init(); // Setup: Watchdog and I/Os
|
|---|
| 43 | spi_init(); // Initialize SPI interface as master
|
|---|
| 44 | timer_init(); // set up TIMER2: TIMER2_COMP interrupt occurs every 1ms
|
|---|
| 45 | atmega_adc_init();
|
|---|
| 46 |
|
|---|
| 47 | w5100_reset();
|
|---|
| 48 | w5100_init();
|
|---|
| 49 |
|
|---|
| 50 | ad7719_init();
|
|---|
| 51 | sei();
|
|---|
| 52 |
|
|---|
| 53 | //MAIN LOOP
|
|---|
| 54 | while (1)
|
|---|
| 55 | {
|
|---|
| 56 | // checks if socket is okay and resets in case of problem the W5100
|
|---|
| 57 | rc=w5100_caretaker();
|
|---|
| 58 | if ( rc != SR_SOCK_ESTABLISHED && rc != SR_SOCK_LISTEN)
|
|---|
| 59 | {
|
|---|
| 60 | // something is not okay with the ethernet ...
|
|---|
| 61 | // will be reset in the next revolution.. but maybe we want to do more...
|
|---|
| 62 | }
|
|---|
| 63 | // this if should check every 25ms if we have any data on the W5100
|
|---|
| 64 | if ( (gVolReg.time_ms % W5100_INPUT_CHECK_TIME == 0) && (w5100_ready) ) {
|
|---|
| 65 | if ( (eth_red_bytes = get_S0_RX_RSR()) != 0) {
|
|---|
| 66 | //parse_w5300_incoming( w5100_get_RX(ETH_READ_BUFFER_SIZE, true) );
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | //----------------------------------------------------------------------------
|
|---|
| 70 | if (check_if_measured_all()) {
|
|---|
| 71 | write_status_via_eth();
|
|---|
| 72 | reset_resistance_done();
|
|---|
| 73 | reset_voltage_done();
|
|---|
| 74 | reset_voltage_values();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | //IF ATmega internal ADC did finish a conversion --every 200us
|
|---|
| 78 | if ( (ADCSRA & (1<<ADIF)) && !gReg.adc_measured_all) {
|
|---|
| 79 | lowbyte = ADCL;
|
|---|
| 80 | highbyte = ADCH;
|
|---|
| 81 | ADCSRA |= 1<<ADIF;
|
|---|
| 82 | gReg.adc_current_reading = ((U16)highbyte<<8) | ((U16)lowbyte);
|
|---|
| 83 | if (gReg.adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED + ADC_READINGS_UNTIL_MEAN - 1) {
|
|---|
| 84 | gReg.adc_values[gReg.adc_current_channel] += gReg.adc_current_reading;
|
|---|
| 85 | gReg.adc_channels_ready[gReg.adc_current_channel/8] |= (1<<(gReg.adc_current_channel%8));
|
|---|
| 86 | gReg.adc_current_channel = increase_adc(gReg.adc_current_channel);
|
|---|
| 87 | Set_V_Muxer(gReg.adc_current_channel);
|
|---|
| 88 | gReg.adc_readings_since_last_muxing = 0;
|
|---|
| 89 | _delay_ms(10); // this is a muxer delay
|
|---|
| 90 | }
|
|---|
| 91 | else if (gReg.adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
|
|---|
| 92 | gReg.adc_values[gReg.adc_current_channel] += gReg.adc_current_reading;
|
|---|
| 93 | ++gReg.adc_readings_since_last_muxing;
|
|---|
| 94 | }
|
|---|
| 95 | else {
|
|---|
| 96 | ++gReg.adc_readings_since_last_muxing;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | //----------------------------------------------------------------------------
|
|---|
| 100 |
|
|---|
| 101 | //IF AD7719 ADC just finished a conversion -- every 60ms
|
|---|
| 102 | if (AD7719_IS_READY() && !gReg.ad7719_measured_all) {
|
|---|
| 103 | gReg.ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
|
|---|
| 104 | // AD7719 is only read out if settled. saves time.
|
|---|
| 105 | if (gReg.ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
|
|---|
| 106 | gReg.ad7719_values[gReg.ad7719_current_channel] = gReg.ad7719_current_reading;
|
|---|
| 107 | gReg.ad7719_readings_since_last_muxing=0;
|
|---|
| 108 | // now prepare the data to be send away via USART.
|
|---|
| 109 | // note that this channel is ready, now and
|
|---|
| 110 | // proceed to the next enabled channel.
|
|---|
| 111 | gReg.ad7719_channels_ready[gReg.ad7719_current_channel/8] |= (1<<(gReg.ad7719_current_channel%8));
|
|---|
| 112 | gReg.ad7719_current_channel = increase_ad7719(gReg.ad7719_current_channel);
|
|---|
| 113 | Set_T_Muxer(gReg.ad7719_current_channel);
|
|---|
| 114 | }
|
|---|
| 115 | else { // the AD7719 did not settle yet, we discard the reading
|
|---|
| 116 | ++gReg.ad7719_readings_since_last_muxing;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | } // end of main while loop
|
|---|
| 121 | } // end of main function
|
|---|