| 1 | //-----------------------------------------------------------------------------
|
|---|
| 2 | #include "typedefs.h"
|
|---|
| 3 | #include "application.h"
|
|---|
| 4 | #include "spare_outs.h"
|
|---|
| 5 | #include "spi_master.h"
|
|---|
| 6 | #include "ad7719_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 | #include "tests.h"
|
|---|
| 16 | //-----------------------------------------------------------------------------
|
|---|
| 17 |
|
|---|
| 18 | int main(void)
|
|---|
| 19 | {
|
|---|
| 20 | // U08 IDN_STR[] = "16ch Pt1000 logger; firmware version of 07.11.10. DN"; // Identity string
|
|---|
| 21 |
|
|---|
| 22 | spare_outs_init(); //set spare out pin I/O modes
|
|---|
| 23 | app_init(); // Setup software modules
|
|---|
| 24 | usart_init(); // Initialize serial interface
|
|---|
| 25 | spi_init(); // Initialize SPI interface as master
|
|---|
| 26 | adc_init(); // Initialize AD7719 ADC as SPI slave
|
|---|
| 27 | // usart_write_crlf();
|
|---|
| 28 | // usart_writeln_flash_str(IDN_STR); // Write string to USART interface
|
|---|
| 29 | // usart_write_crlf();
|
|---|
| 30 |
|
|---|
| 31 | // Enable interrupts
|
|---|
| 32 | sei();
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | // temperature muxer pins init:
|
|---|
| 36 | // SA - pins
|
|---|
| 37 | DDRA |= 0x3F; // set all SA-pins as outputs
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | // voltage, current, humidity - muxer pins:
|
|---|
| 41 | // SB - pins
|
|---|
| 42 | DDRC |= 0x7F; // set all SB - pins as outputs
|
|---|
| 43 |
|
|---|
| 44 | // SB - muxer test
|
|---|
| 45 | // DDRA |= 1<<PA6 ; // set D0-0 lina as output. for tests only !!!
|
|---|
| 46 | // PORTA |= 1<<PA6; // set D0-0 line high. for tests only !!!
|
|---|
| 47 | DDRA &= ~(1<<PA6);
|
|---|
| 48 |
|
|---|
| 49 | //ADC einschalten
|
|---|
| 50 | ADMUX = 0x26; //0010.0110 // interne Referenzspannung nutzen
|
|---|
| 51 | ADCSRA = (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler
|
|---|
| 52 | ADCSRA |= (1<<ADEN); // ADC aktivieren
|
|---|
| 53 | ADCSRA |= (1<<ADSC);
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | // Main loop
|
|---|
| 58 | //float temperature;
|
|---|
| 59 | float resistance;
|
|---|
| 60 | BOOL heartbeat_enable = TRUE;
|
|---|
| 61 |
|
|---|
| 62 | U08 SA_mux_val = 0x00;
|
|---|
| 63 | U08 SB_mux_val = 0x00;
|
|---|
| 64 |
|
|---|
| 65 | //U08 counter = 0;
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | while (TRUE)
|
|---|
| 70 | {
|
|---|
| 71 | // this heartbeat shows how long one single run of this while loop takes
|
|---|
| 72 | // measure with a scope.
|
|---|
| 73 | if (heartbeat_enable) PORTB ^= (1<<PB3); // toggle Out2_spare --> heartbeat
|
|---|
| 74 | //adc_init();
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | _delay_ms(1);
|
|---|
| 79 | /*
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | // if USART data arrives. i.e. data via USB
|
|---|
| 83 | // the usart_rx_ready flag is set TRUE
|
|---|
| 84 | // now process the incoming data which is stored in
|
|---|
| 85 | // U08 usart_rx_buffer[USART_RX_BUFFER_SIZE]
|
|---|
| 86 | // and tell the USART interface, it may receive new data
|
|---|
| 87 | // by setting the usart_rx_ready flag FALSE again
|
|---|
| 88 | ++SA_mux_val;
|
|---|
| 89 | if (SA_mux_val == 64) SA_mux_val = 0;
|
|---|
| 90 | PORTA = (SA_mux_val & 0x3F);
|
|---|
| 91 |
|
|---|
| 92 | usart_write_str((pU08)"SA:");
|
|---|
| 93 | usart_write_U08(SA_mux_val,2);
|
|---|
| 94 | usart_write_str((pU08)" Sensor:");
|
|---|
| 95 | usart_write_U08((SA_mux_val % 8)+1,2);
|
|---|
| 96 | usart_write_str((pU08)" an Temperatur_");
|
|---|
| 97 | switch (SA_mux_val / 8)
|
|---|
| 98 | {
|
|---|
| 99 | case 0: usart_write_str((pU08)"C");
|
|---|
| 100 | break;
|
|---|
| 101 | case 1: usart_write_str((pU08)"D");
|
|---|
| 102 | break;
|
|---|
| 103 | case 2: usart_write_str((pU08)"A");
|
|---|
| 104 | break;
|
|---|
| 105 | case 3: usart_write_str((pU08)"B");
|
|---|
| 106 | break;
|
|---|
| 107 | case 4: usart_write_str((pU08)"G");
|
|---|
| 108 | break;
|
|---|
| 109 | case 5: usart_write_str((pU08)"H");
|
|---|
| 110 | break;
|
|---|
| 111 | case 6: usart_write_str((pU08)"E");
|
|---|
| 112 | break;
|
|---|
| 113 | case 7: usart_write_str((pU08)"F");
|
|---|
| 114 | break;
|
|---|
| 115 | default: usart_write_str((pU08)"alarm!");
|
|---|
| 116 | break;
|
|---|
| 117 | }
|
|---|
| 118 | usart_write_str((pU08)"\n");
|
|---|
| 119 | _delay_us(200);
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | for (U08 counter = 0; counter < 10; ++counter) {
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | while (!ADC_IS_READY())
|
|---|
| 128 | {
|
|---|
| 129 | // just wait until ADC is redy -- really bad code here!
|
|---|
| 130 | }
|
|---|
| 131 | startconv(); //Start a new A/D Conversion
|
|---|
| 132 | //temp = readandsendtemp();
|
|---|
| 133 | //adcword = getadc();
|
|---|
| 134 | resistance = getresistance();
|
|---|
| 135 | //temperature = gettemp();
|
|---|
| 136 | usart_write_str((pU08)"R:");
|
|---|
| 137 | usart_write_float(resistance,3,4);
|
|---|
| 138 | usart_write_str((pU08)"kOhm ");
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | while (!ADC_IS_READY())
|
|---|
| 142 | {
|
|---|
| 143 | // just wait until ADC is redy -- really bad code here!
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | startconv(); //Start a new A/D Conversion
|
|---|
| 147 | //temp = readandsendtemp();
|
|---|
| 148 | //adcword = getadc();
|
|---|
| 149 | resistance = getresistance();
|
|---|
| 150 | //temperature = gettemp();
|
|---|
| 151 | usart_write_str((pU08)"R:");
|
|---|
| 152 | usart_write_float(resistance,3,4);
|
|---|
| 153 | usart_write_str((pU08)"kOhm\n");
|
|---|
| 154 |
|
|---|
| 155 | _delay_ms(500);
|
|---|
| 156 | }
|
|---|
| 157 | usart_write_str((pU08)"\n\n\n");
|
|---|
| 158 | */
|
|---|
| 159 |
|
|---|
| 160 | ++SB_mux_val;
|
|---|
| 161 | if (SB_mux_val == 84) SB_mux_val = 0;
|
|---|
| 162 | PORTC = (SB_mux_val & 0x7F);
|
|---|
| 163 |
|
|---|
| 164 | _delay_ms(5);
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | usart_write_str((pU08)"8bit-ADC: ");
|
|---|
| 168 |
|
|---|
| 169 | if (SB_mux_val < 64)
|
|---|
| 170 | {
|
|---|
| 171 | switch (SB_mux_val / 16)
|
|---|
| 172 | {
|
|---|
| 173 | case 0: usart_write_str((pU08)"voltage_A: ");
|
|---|
| 174 | break;
|
|---|
| 175 | case 1: usart_write_str((pU08)"voltage_B: ");
|
|---|
| 176 | break;
|
|---|
| 177 | case 2: usart_write_str((pU08)"voltage_D: ");
|
|---|
| 178 | break;
|
|---|
| 179 | case 3: usart_write_str((pU08)"voltage_C: ");
|
|---|
| 180 | break;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (SB_mux_val % 2 == 0) {
|
|---|
| 184 | usart_write_str((pU08)"U");
|
|---|
| 185 | usart_write_U08( (SB_mux_val%16)/2 , 1 );
|
|---|
| 186 | } else {
|
|---|
| 187 | usart_write_str((pU08)"I");
|
|---|
| 188 | usart_write_U08( ((SB_mux_val%16)-1)/2 , 1 );
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | } else {
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | if (SB_mux_val < 72) {
|
|---|
| 196 | usart_write_str((pU08)"voltage_E: ");
|
|---|
| 197 | if (SB_mux_val % 2 == 0) {
|
|---|
| 198 | usart_write_str((pU08)"U");
|
|---|
| 199 | usart_write_U08( (SB_mux_val%8)/2 , 1 );
|
|---|
| 200 | } else {
|
|---|
| 201 | usart_write_str((pU08)"I");
|
|---|
| 202 | usart_write_U08( ((SB_mux_val%8)-1)/2 , 1 );
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | }
|
|---|
| 206 | else if (SB_mux_val == 72) usart_write_str((pU08)"humidity_A: H0");
|
|---|
| 207 | else if (SB_mux_val == 73) usart_write_str((pU08)"humidity_A: H1");
|
|---|
| 208 |
|
|---|
| 209 | else if (SB_mux_val < 82) {
|
|---|
| 210 | usart_write_str((pU08)"voltage_F: ");
|
|---|
| 211 | if (SB_mux_val % 2 == 0) {
|
|---|
| 212 | usart_write_str((pU08)"U");
|
|---|
| 213 | usart_write_U08( (SB_mux_val%8)/2 , 1 );
|
|---|
| 214 | } else {
|
|---|
| 215 | usart_write_str((pU08)"I");
|
|---|
| 216 | usart_write_U08( ((SB_mux_val%8)-1)/2 , 1 );
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | }
|
|---|
| 220 | else if (SB_mux_val == 82) usart_write_str((pU08)"humidity_B: H0");
|
|---|
| 221 | else if (SB_mux_val == 83) usart_write_str((pU08)"humidity_B: H1");
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | for (U08 counter = 0; counter < 10; ++counter) {
|
|---|
| 225 | while (ADCSRA & (1<<ADSC) ); // wait until internal ADC is ready
|
|---|
| 226 | ADCSRA |= (1<<ADSC);
|
|---|
| 227 | float voltage;
|
|---|
| 228 | voltage = ( (float)ADCH ) / 256 * 4.096;
|
|---|
| 229 | usart_write_str((pU08)" ");
|
|---|
| 230 | usart_write_float(voltage,3,4);
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | _delay_ms(300);
|
|---|
| 234 | }
|
|---|
| 235 | usart_write_str((pU08)"\n");
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 | /*
|
|---|
| 239 | if ( usart_rx_ready == TRUE )
|
|---|
| 240 | {
|
|---|
| 241 | //understand what it means and react
|
|---|
| 242 |
|
|---|
| 243 | switch (usart_rx_buffer[0])
|
|---|
| 244 | {
|
|---|
| 245 |
|
|---|
| 246 | case 'h':
|
|---|
| 247 | {
|
|---|
| 248 | // toggle the heartbeat mode on or off.
|
|---|
| 249 | heartbeat_enable = !heartbeat_enable;
|
|---|
| 250 | break;
|
|---|
| 251 | }
|
|---|
| 252 | case 'a':
|
|---|
| 253 | {
|
|---|
| 254 | // conduct adc - AD7719 SPI interface test
|
|---|
| 255 |
|
|---|
| 256 | break;
|
|---|
| 257 | }
|
|---|
| 258 | case 'e':
|
|---|
| 259 | {
|
|---|
| 260 | // conduct ethernet module SPI interface test
|
|---|
| 261 | strtol((char*) usart_rx_buffer+1, NULL, 0);
|
|---|
| 262 | break;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | default:
|
|---|
| 266 | {
|
|---|
| 267 | usart_write_str((pU08)"? you wrote: ");
|
|---|
| 268 | usart_write_str((pU08)usart_rx_buffer);
|
|---|
| 269 | usart_write_str((pU08)"\n");
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | heartbeat_enable = !heartbeat_enable;
|
|---|
| 275 | usart_rx_ready = FALSE;
|
|---|
| 276 | }
|
|---|
| 277 | */
|
|---|
| 278 | // das ist ein paar schritte zu früh.
|
|---|
| 279 | // erstmal müssen die interfaces getestet werden.
|
|---|
| 280 | /*
|
|---|
| 281 |
|
|---|
| 282 | for (U08 i = 0; i<16; i++)
|
|---|
| 283 | {
|
|---|
| 284 |
|
|---|
| 285 | if((~PIND) & 0x08) // PD4 is #ADC_RDY input. Inverted logic! if PD4=0 this evaluates to true
|
|---|
| 286 | {
|
|---|
| 287 | PORTA = (PORTA & 0xF0) | ((i) & 0x0F); // switch muxer
|
|---|
| 288 | startconv(); //Start a new A/D Conversion
|
|---|
| 289 | //temp = readandsendtemp();
|
|---|
| 290 | //adcword = getadc();
|
|---|
| 291 | //resistance = getresistance();
|
|---|
| 292 | temperature = gettemp();
|
|---|
| 293 | usart_write_float(temperature,2,4);
|
|---|
| 294 | usart_write_str((pU08)"\t");
|
|---|
| 295 |
|
|---|
| 296 | } // end of if adc ready
|
|---|
| 297 | else
|
|---|
| 298 | {
|
|---|
| 299 | i--;
|
|---|
| 300 | }
|
|---|
| 301 | } // end of for loop over 16 channels
|
|---|
| 302 | usart_write_crlf();
|
|---|
| 303 |
|
|---|
| 304 | */
|
|---|
| 305 |
|
|---|
| 306 | } // end of infinite while loop
|
|---|
| 307 | } // end of main()
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|