| 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 = 0x16;
|
|---|
| 63 | U08 SB_mux_val = 0x00;
|
|---|
| 64 |
|
|---|
| 65 | //U08 counter = 0;
|
|---|
| 66 | U08 Res_or_Volt = 0x00;
|
|---|
| 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 | ++Res_or_Volt;
|
|---|
| 77 | if (Res_or_Volt <= 64){
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | // if USART data arrives. i.e. data via USB
|
|---|
| 81 | // the usart_rx_ready flag is set TRUE
|
|---|
| 82 | // now process the incoming data which is stored in
|
|---|
| 83 | // U08 usart_rx_buffer[USART_RX_BUFFER_SIZE]
|
|---|
| 84 | // and tell the USART interface, it may receive new data
|
|---|
| 85 | // by setting the usart_rx_ready flag FALSE again
|
|---|
| 86 | ++SA_mux_val;
|
|---|
| 87 | if (Res_or_Volt == 1) SB_mux_val = 16;
|
|---|
| 88 | else if (SA_mux_val == 64) SA_mux_val = 32;
|
|---|
| 89 | else if (SA_mux_val == 16) SA_mux_val = 48;
|
|---|
| 90 | else if (SA_mux_val == 32) SA_mux_val = 0;
|
|---|
| 91 | PORTA = (SA_mux_val & 0x3F);
|
|---|
| 92 |
|
|---|
| 93 | // usart_write_str((pU08)"SA:");
|
|---|
| 94 | usart_write_U08(SA_mux_val,2);
|
|---|
| 95 | usart_write_str((pU08)" Sensor:");
|
|---|
| 96 | usart_write_U08((SA_mux_val % 8)+1,2);
|
|---|
| 97 | usart_write_str((pU08)" an Temperatur_");
|
|---|
| 98 | switch (SA_mux_val / 8)
|
|---|
| 99 | {
|
|---|
| 100 | case 0: usart_write_str((pU08)"C");
|
|---|
| 101 | break;
|
|---|
| 102 | case 1: usart_write_str((pU08)"D");
|
|---|
| 103 | break;
|
|---|
| 104 | case 2: usart_write_str((pU08)"A");
|
|---|
| 105 | break;
|
|---|
| 106 | case 3: usart_write_str((pU08)"B");
|
|---|
| 107 | break;
|
|---|
| 108 | case 4: usart_write_str((pU08)"G");
|
|---|
| 109 | break;
|
|---|
| 110 | case 5: usart_write_str((pU08)"H");
|
|---|
| 111 | break;
|
|---|
| 112 | case 6: usart_write_str((pU08)"E");
|
|---|
| 113 | break;
|
|---|
| 114 | case 7: usart_write_str((pU08)"F");
|
|---|
| 115 | break;
|
|---|
| 116 | default: usart_write_str((pU08)"alarm!");
|
|---|
| 117 | break;
|
|---|
| 118 | }
|
|---|
| 119 | // usart_write_str((pU08)"\n");
|
|---|
| 120 | usart_write_str((pU08)" ");
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | startconv();
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | while (!ADC_IS_READY())
|
|---|
| 127 | {
|
|---|
| 128 | // just wait until ADC is redy -- really bad code here!
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | resistance = getresistance();
|
|---|
| 132 | //Start a new A/D Conversion
|
|---|
| 133 | //temp = readandsendtemp();
|
|---|
| 134 | //adcword = getadc();
|
|---|
| 135 |
|
|---|
| 136 | //temperature = gettemp();
|
|---|
| 137 | usart_write_str((pU08)"R:");
|
|---|
| 138 | usart_write_float(resistance,3,4);
|
|---|
| 139 | usart_write_str((pU08)"kOhm ");
|
|---|
| 140 |
|
|---|
| 141 | //_delay_ms(200);
|
|---|
| 142 |
|
|---|
| 143 | startconv();
|
|---|
| 144 |
|
|---|
| 145 | while (!ADC_IS_READY())
|
|---|
| 146 | {
|
|---|
| 147 | // just wait until ADC is redy -- really bad code here!
|
|---|
| 148 | }
|
|---|
| 149 | //Start a new A/D Conversion
|
|---|
| 150 | //temp = readandsendtemp();
|
|---|
| 151 | //adcword = getadc();
|
|---|
| 152 | resistance = getresistance();
|
|---|
| 153 | //temperature = gettemp();
|
|---|
| 154 | usart_write_str((pU08)"R:");
|
|---|
| 155 | usart_write_float(resistance,3,4);
|
|---|
| 156 | usart_write_str((pU08)"kOhm ");
|
|---|
| 157 |
|
|---|
| 158 | //usart_write_str((pU08)"\n");
|
|---|
| 159 | switch (SA_mux_val)
|
|---|
| 160 | {
|
|---|
| 161 | case 7: usart_write_str((pU08)"\n\n");
|
|---|
| 162 | break;
|
|---|
| 163 | case 15: usart_write_str((pU08)"\n\n");
|
|---|
| 164 | break;
|
|---|
| 165 | case 23: usart_write_str((pU08)"\n\n");
|
|---|
| 166 | break;
|
|---|
| 167 | case 31: usart_write_str((pU08)"\n\n");
|
|---|
| 168 | break;
|
|---|
| 169 | case 39: usart_write_str((pU08)"\n\n");
|
|---|
| 170 | break;
|
|---|
| 171 | case 47: usart_write_str((pU08)"\n\n");
|
|---|
| 172 | break;
|
|---|
| 173 | case 55: usart_write_str((pU08)"\n\n");
|
|---|
| 174 | break;
|
|---|
| 175 | case 63: usart_write_str((pU08)"\n\n");
|
|---|
| 176 | break;
|
|---|
| 177 | default: usart_write_str((pU08)"\n");
|
|---|
| 178 | break;
|
|---|
| 179 | }
|
|---|
| 180 | SB_mux_val = 0;
|
|---|
| 181 | }
|
|---|
| 182 | else if (Res_or_Volt == 148) Res_or_Volt = 0;
|
|---|
| 183 | else {
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | ++SB_mux_val;
|
|---|
| 187 | if (SB_mux_val == 84) SB_mux_val = 0;
|
|---|
| 188 | else if (SB_mux_val == 74) SB_mux_val = 82;
|
|---|
| 189 | else if (SB_mux_val == 82) SB_mux_val = 72;
|
|---|
| 190 | else if (SB_mux_val == 72) SB_mux_val = 74;
|
|---|
| 191 | else if (SB_mux_val == 48) SB_mux_val = 64;
|
|---|
| 192 | else if (SB_mux_val == 64) SB_mux_val = 32;
|
|---|
| 193 | else if (SB_mux_val == 32) SB_mux_val = 48;
|
|---|
| 194 | PORTC = (SB_mux_val & 0x7F);
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | usart_write_str((pU08)"8bit-ADC: ");
|
|---|
| 200 |
|
|---|
| 201 | if (SB_mux_val < 64)
|
|---|
| 202 | {
|
|---|
| 203 | switch (SB_mux_val / 16)
|
|---|
| 204 | {
|
|---|
| 205 | case 0: usart_write_str((pU08)"voltage_A: ");
|
|---|
| 206 | break;
|
|---|
| 207 | case 1: usart_write_str((pU08)"voltage_B: ");
|
|---|
| 208 | break;
|
|---|
| 209 | case 2: usart_write_str((pU08)"voltage_D: ");
|
|---|
| 210 | break;
|
|---|
| 211 | case 3: usart_write_str((pU08)"voltage_C: ");
|
|---|
| 212 | break;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | if (SB_mux_val % 2 == 0) {
|
|---|
| 216 | usart_write_str((pU08)"U");
|
|---|
| 217 | usart_write_U08( (SB_mux_val%16)/2 , 1 );
|
|---|
| 218 | } else {
|
|---|
| 219 | usart_write_str((pU08)"I");
|
|---|
| 220 | usart_write_U08( ((SB_mux_val%16)-1)/2 , 1 );
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | } else {
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 | if (SB_mux_val < 72) {
|
|---|
| 228 | usart_write_str((pU08)"voltage_E: ");
|
|---|
| 229 | if (SB_mux_val % 2 == 0) {
|
|---|
| 230 | usart_write_str((pU08)"U");
|
|---|
| 231 | usart_write_U08( (SB_mux_val%8)/2 , 1 );
|
|---|
| 232 | } else {
|
|---|
| 233 | usart_write_str((pU08)"I");
|
|---|
| 234 | usart_write_U08( ((SB_mux_val%8)-1)/2 , 1 );
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | }
|
|---|
| 238 | else if (SB_mux_val == 72) usart_write_str((pU08)"humidity_A: H0");
|
|---|
| 239 | else if (SB_mux_val == 73) usart_write_str((pU08)"humidity_A: H1");
|
|---|
| 240 |
|
|---|
| 241 | else if (SB_mux_val < 82) {
|
|---|
| 242 | usart_write_str((pU08)"voltage_F: ");
|
|---|
| 243 | if (SB_mux_val % 2 == 0) {
|
|---|
| 244 | usart_write_str((pU08)"U");
|
|---|
| 245 | usart_write_U08( ((SB_mux_val-2)%8)/2 , 1 );
|
|---|
| 246 | } else {
|
|---|
| 247 | usart_write_str((pU08)"I");
|
|---|
| 248 | usart_write_U08( (((SB_mux_val-2)%8)-1)/2 , 1 );
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | }
|
|---|
| 252 | else if (SB_mux_val == 82) usart_write_str((pU08)"humidity_B: H0");
|
|---|
| 253 | else if (SB_mux_val == 83) usart_write_str((pU08)"humidity_B: H1");
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | for (U08 counter = 0; counter < 1; ++counter) {
|
|---|
| 257 | ADCSRA |= (1<<ADSC);
|
|---|
| 258 | while (ADCSRA & (1<<ADSC) ); // wait until internal ADC is ready
|
|---|
| 259 | float voltage;
|
|---|
| 260 | voltage = ( (float)ADCH ) / 256 * 4.096;
|
|---|
| 261 | usart_write_str((pU08)" ");
|
|---|
| 262 | usart_write_float(voltage,3,4);
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | }
|
|---|
| 266 | //usart_write_str((pU08)"\n");
|
|---|
| 267 |
|
|---|
| 268 | switch (SB_mux_val)
|
|---|
| 269 | {
|
|---|
| 270 | case 15: usart_write_str((pU08)"\n\n");
|
|---|
| 271 | break;
|
|---|
| 272 | case 31: usart_write_str((pU08)"\n\n");
|
|---|
| 273 | break;
|
|---|
| 274 | case 47: usart_write_str((pU08)"\n\n");
|
|---|
| 275 | break;
|
|---|
| 276 | case 63: usart_write_str((pU08)"\n\n");
|
|---|
| 277 | break;
|
|---|
| 278 | case 71: usart_write_str((pU08)"\n\n");
|
|---|
| 279 | break;
|
|---|
| 280 | case 73: usart_write_str((pU08)"\n\n");
|
|---|
| 281 | break;
|
|---|
| 282 | case 81: usart_write_str((pU08)"\n\n");
|
|---|
| 283 | break;
|
|---|
| 284 | case 83: usart_write_str((pU08)"\n\n");
|
|---|
| 285 | break;
|
|---|
| 286 | default: usart_write_str((pU08)"\n");
|
|---|
| 287 | break;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | SA_mux_val = 15;
|
|---|
| 291 | }
|
|---|
| 292 | /*
|
|---|
| 293 | if ( usart_rx_ready == TRUE )
|
|---|
| 294 | {
|
|---|
| 295 | //understand what it means and react
|
|---|
| 296 |
|
|---|
| 297 | switch (usart_rx_buffer[0])
|
|---|
| 298 | {
|
|---|
| 299 |
|
|---|
| 300 | case 'h':
|
|---|
| 301 | {
|
|---|
| 302 | // toggle the heartbeat mode on or off.
|
|---|
| 303 | heartbeat_enable = !heartbeat_enable;
|
|---|
| 304 | break;
|
|---|
| 305 | }
|
|---|
| 306 | case 'a':
|
|---|
| 307 | {
|
|---|
| 308 | // conduct adc - AD7719 SPI interface test
|
|---|
| 309 |
|
|---|
| 310 | break;
|
|---|
| 311 | }
|
|---|
| 312 | case 'e':
|
|---|
| 313 | {
|
|---|
| 314 | // conduct ethernet module SPI interface test
|
|---|
| 315 | strtol((char*) usart_rx_buffer+1, NULL, 0);
|
|---|
| 316 | break;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | default:
|
|---|
| 320 | {
|
|---|
| 321 | usart_write_str((pU08)"? you wrote: ");
|
|---|
| 322 | usart_write_str((pU08)usart_rx_buffer);
|
|---|
| 323 | usart_write_str((pU08)"\n");
|
|---|
| 324 | break;
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | heartbeat_enable = !heartbeat_enable;
|
|---|
| 329 | usart_rx_ready = FALSE;
|
|---|
| 330 | }
|
|---|
| 331 | */
|
|---|
| 332 | // das ist ein paar schritte zu früh.
|
|---|
| 333 | // erstmal müssen die interfaces getestet werden.
|
|---|
| 334 | /*
|
|---|
| 335 |
|
|---|
| 336 | for (U08 i = 0; i<16; i++)
|
|---|
| 337 | {
|
|---|
| 338 |
|
|---|
| 339 | if((~PIND) & 0x08) // PD4 is #ADC_RDY input. Inverted logic! if PD4=0 this evaluates to true
|
|---|
| 340 | {
|
|---|
| 341 | PORTA = (PORTA & 0xF0) | ((i) & 0x0F); // switch muxer
|
|---|
| 342 | startconv(); //Start a new A/D Conversion
|
|---|
| 343 | //temp = readandsendtemp();
|
|---|
| 344 | //adcword = getadc();
|
|---|
| 345 | //resistance = getresistance();
|
|---|
| 346 | temperature = gettemp();
|
|---|
| 347 | usart_write_float(temperature,2,4);
|
|---|
| 348 | usart_write_str((pU08)"\t");
|
|---|
| 349 |
|
|---|
| 350 | } // end of if adc ready
|
|---|
| 351 | else
|
|---|
| 352 | {
|
|---|
| 353 | i--;
|
|---|
| 354 | }
|
|---|
| 355 | } // end of for loop over 16 channels
|
|---|
| 356 | usart_write_crlf();
|
|---|
| 357 |
|
|---|
| 358 | */
|
|---|
| 359 |
|
|---|
| 360 | } // end of infinite while loop
|
|---|
| 361 | } // end of main()
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|