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 != 0x17 && rc != 0x14)
|
---|
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 | for (U08 rep=0; rep<1; ) {
|
---|
67 | if (eth_red_bytes == get_S0_RX_RSR() ) {
|
---|
68 | rep++;
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | eth_red_bytes =get_S0_RX_RSR();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | //parse_w5300_incoming( w5100_get_RX(ETH_READ_BUFFER_SIZE, true) );
|
---|
75 | }
|
---|
76 | }
|
---|
77 | //----------------------------------------------------------------------------
|
---|
78 | if (check_if_measured_all()) {
|
---|
79 | write_status_via_eth();
|
---|
80 | reset_resistance_done();
|
---|
81 | reset_voltage_done();
|
---|
82 | reset_voltage_values();
|
---|
83 | }
|
---|
84 |
|
---|
85 | //IF ATmega internal ADC did finish a conversion --every 200us
|
---|
86 | if ( (ADCSRA & (1<<ADIF)) && !gReg.adc_measured_all) {
|
---|
87 | lowbyte = ADCL;
|
---|
88 | highbyte = ADCH;
|
---|
89 | ADCSRA |= 1<<ADIF;
|
---|
90 | gReg.adc_current_reading = ((U16)highbyte<<8) | ((U16)lowbyte);
|
---|
91 | if (gReg.adc_readings_since_last_muxing == ADC_READINGS_UNTIL_SETTLED + ADC_READINGS_UNTIL_MEAN - 1) {
|
---|
92 | gReg.adc_values[gReg.adc_current_channel] += gReg.adc_current_reading;
|
---|
93 | gReg.adc_channels_ready[gReg.adc_current_channel/8] |= (1<<(gReg.adc_current_channel%8));
|
---|
94 | gReg.adc_current_channel = increase_adc(gReg.adc_current_channel);
|
---|
95 | Set_V_Muxer(gReg.adc_current_channel);
|
---|
96 | gReg.adc_readings_since_last_muxing = 0;
|
---|
97 | _delay_ms(10); // this is a muxer delay
|
---|
98 | }
|
---|
99 | else if (gReg.adc_readings_since_last_muxing >= ADC_READINGS_UNTIL_SETTLED-1) {
|
---|
100 | gReg.adc_values[gReg.adc_current_channel] += gReg.adc_current_reading;
|
---|
101 | ++gReg.adc_readings_since_last_muxing;
|
---|
102 | }
|
---|
103 | else {
|
---|
104 | ++gReg.adc_readings_since_last_muxing;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | //----------------------------------------------------------------------------
|
---|
108 |
|
---|
109 | //IF AD7719 ADC just finished a conversion -- every 60ms
|
---|
110 | if (AD7719_IS_READY() && !gReg.ad7719_measured_all) {
|
---|
111 | gReg.ad7719_current_reading = read_adc(); // --takes at 4MHz SCLK speed about 6us
|
---|
112 | // AD7719 is only read out if settled. saves time.
|
---|
113 | if (gReg.ad7719_readings_since_last_muxing == AD7719_READINGS_UNTIL_SETTLED-1) {
|
---|
114 | gReg.ad7719_values[gReg.ad7719_current_channel] = gReg.ad7719_current_reading;
|
---|
115 | gReg.ad7719_readings_since_last_muxing=0;
|
---|
116 | // now prepare the data to be send away via USART.
|
---|
117 | // note that this channel is ready, now and
|
---|
118 | // proceed to the next enabled channel.
|
---|
119 | gReg.ad7719_channels_ready[gReg.ad7719_current_channel/8] |= (1<<(gReg.ad7719_current_channel%8));
|
---|
120 | gReg.ad7719_current_channel = increase_ad7719(gReg.ad7719_current_channel);
|
---|
121 | Set_T_Muxer(gReg.ad7719_current_channel);
|
---|
122 | }
|
---|
123 | else { // the AD7719 did not settle yet, we discard the reading
|
---|
124 | ++gReg.ad7719_readings_since_last_muxing;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | } // end of main while loop
|
---|
129 | } // end of main function
|
---|