// // main.1mhz.c : AVR uC code for flukso sensor board // Copyright (c) 2008-2009 jokamajo.org // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id: main.1mhz.c 3 2009-05-26 20:27:00Z icarus75 $ #include #include #include "wiring/wiring_private.h" #include "main.1mhz.h" #include // pin/register/ISR definitions #include // eeprom library #include // watchdog timer library #include // variable declarations volatile struct state aux[4] = {{false, false, START}, {false, false, START}, {false, false, START}, {false, false, START}}; volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}}; volatile struct sensor measurements[4]; // interrupt service routine for INT0 ISR(INT0_vect) { measurements[0].value++; aux[0].pulse = true; } // interrupt service routine for INT1 ISR(INT1_vect) { measurements[1].value++; aux[1].pulse = true; } // interrupt service routine for PCI2 (PCINT20 = pin4) ISR(PCINT2_vect) { if (aux[2].toggle == false) { aux[2].toggle = true; } else { measurements[2].value++; aux[2].pulse = true; aux[2].toggle = false; } } // interrupt service routine for PCI0 (PCINT1 = pin9) ISR(PCINT0_vect) { if (aux[3].toggle == false) { aux[3].toggle = true; } else { measurements[3].value++; aux[3].pulse = true; aux[3].toggle = false; } } ISR(TIMER2_OVF_vect) { // read ADC result // add to nano(Wh) counter aux[0].nano += (uint32_t)METERCONST * ADC; if (aux[0].nano > 1000000000) { printString("msg ADC0 sample value: "); printIntegerInBase((unsigned long)ADC, 10); printString("\n"); //debugging measurements[0].value++; aux[0].pulse = true; aux[0].nano -= 1000000000; } // start a new ADC conversion ADCSRA |= (1< moved the call to this function to start of the main loop, before init // clock settings: divide by 8 to get a 1Mhz clock, allows us to set the BOD level to 1.8V (DS p.37) CLKPR = (1< fTOV2 = 1000kHz / 256 / 32 = 122.07Hz TCCR2B |= (1< 1000kHz / 8 = 125kHz ADCSRA |= (1<value, buffer, 10); length = strlen(buffer); strcpy(buffer, "pls "); strcpy(&buffer[4], measurement->id); strcpy(&buffer[36], ":"); // insert leading 0's for (i=0; i<10-length; i++) strcpy(&buffer[37+i], "0"); ltoa(measurement->value, &buffer[47-length], 10); strcpy(&buffer[47], "\n"); printString(buffer); // blink the green LED PORTB |= (1<