// // main.c : AVR uC code for flukso sensor board // // Copyright (c) 2008-2009 jokamajo.org // 2010 flukso.net // // 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$ #include #include #include "wiring/wiring_private.h" #include "main.h" #include // pin/register/ISR definitions #include // eeprom library #include // watchdog timer library #include // variable declarations volatile struct state aux[4] = {{false, false, false, START, 0}, {false, false, false, START, 0}, {false, false, false, START, 0}, {false, false, false, START, 0}}; volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}}; volatile struct sensor measurements[4]; volatile struct time_struct time = {false, 0}; volatile uint8_t muxn = 0; volatile uint16_t timer = 0; // interrupt service routine for INT0 ISR(INT0_vect) { pulse_add(&measurements[2], &aux[2], PULSE_CONST_2, PULSE_HALF_2); } // interrupt service routine for INT1 ISR(INT1_vect) { pulse_add(&measurements[3], &aux[3], PULSE_CONST_3, PULSE_HALF_3); } // interrupt service routine for PCI2 (PCINT20) /** ISR(PCINT2_vect) { if (aux[4].toggle == false) { aux[4].toggle = true; } else { pulse_add(&measurements[4], &aux[4], PULSE_CONST_4, PULSE_HALF_4); } } **/ void pulse_add(volatile struct sensor *measurement, volatile struct state *aux, uint32_t pulse_const, uint32_t pulse_half) { measurement->value += pulse_const; if (aux->half == true) { measurement->value += 1; } if (pulse_half) { aux->half = !aux->half; } aux->pulse = true; aux->time = time.ms; } // interrupt service routine for ADC ISR(TIMER2_COMPA_vect) { #if DBG > 0 PORTD |= (1< WATT) { measurements[muxn].value++; aux[muxn].pulse = true; aux[muxn].nano -= WATT; aux[muxn].pulse_count++; } if (timer == SECOND) { aux[muxn].nano_start = aux[muxn].nano_end; aux[muxn].nano_end = aux[muxn].nano; aux[muxn].pulse_count_final = aux[muxn].pulse_count; aux[muxn].pulse_count = 0; aux[muxn].power = true; } // cycle through the available ADC input channels (0 and 1) muxn++; if (!(muxn &= 0x1)) timer++; if (timer > SECOND) timer = 0; // We have timer interrupts occcuring at a frequency of 1250Hz. // In order to map this to 1000Hz (=ms) we have to skip every fifth interrupt. if (!time.skip) time.ms++; time.skip = (((time.ms & 0x3) == 3) && !time.skip) ? true : false; ADMUX &= 0xF8; ADMUX |= muxn; // start a new ADC conversion ADCSRA |= (1< 0 PORTD &= ~(1< 1 aux[muxn].nano = WATT+1; timer = SECOND; #endif } // interrupt service routine for analog comparator ISR(ANALOG_COMP_vect) { uint8_t i; //debugging: //measurements[3].value = END3; //measurements[2].value = END2; //measurements[1].value = END1; //measurements[0].value = END0; //disable uC sections to consume less power while writing to EEPROM //disable UART Tx and Rx: UCSR0B &= ~((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< 0 // re-use PD4 pin for tracing interrupt times DDRD |= (1< 0 // Toggle pin OC2A=PB3 on compare match TCCR2A |= 1< fTOV2 = 1000kHz / 256 / 8 = 488.28Hz (DS p.158) TCCR2B |= (1< 1000kHz / 8 = 125kHz (DS p.258) ADCSRA |= (1<value; ms = aux->time; sei(); strcpy(message, "pls "); break; case POWER: cli(); rest = aux->nano_end - aux->nano_start; pulse_count = aux->pulse_count_final; sei(); // Since the AVR has no dedicated floating-point hardware, we need // to resort to fixed-point calculations for converting nWh/s to W. // 1W = 10^6/3.6 nWh/s // value[watt] = 3.6/10^6 * rest[nWh/s] // value[watt] = 3.6/10^6 * 65536 * (rest[nWh/s] / 65536) // value[watt] = 3.6/10^6 * 65536 * 262144 / 262144 * (rest[nWh/s] / 65536) // value[watt] = 61847.53 / 262144 * (rest[nWh/s] / 65536) // We round the constant down to 61847 to prevent 'underflow' in the // consecutive else statement. // The error introduced in the fixed-point rounding equals 8.6*10^-6. MacU16X16to32(value, (uint16_t)(labs(rest)/65536), 61847); value /= 262144; if (rest >= 0) value += pulse_count*3600; else value = pulse_count*3600 - value; strcpy(message, "pwr "); break; } strcpy(&message[4], measurement->id); strcpy(&message[36], ":0000000000\n"); i = 46; do { // generate digits in reverse order message[i--] = '0' + value % 10; // get next digit } while ((value /= 10) > 0); // delete it if ((msg_type == PULSE) && ms) { strcpy(&message[47], ":0000000000\n"); i = 57; do { // generate digits in reverse order message[i--] = '0' + ms % 10; // get next digit } while ((ms /= 10) > 0); // delete it } printString(message); } void loop() { uint8_t i; // check whether we have to send out a pls or pwr to the deamon for (i=0; i<4; i++) { if (aux[i].pulse == true) { send(PULSE, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]); aux[i].pulse = false; } if (aux[i].power == true) { send(POWER, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]); aux[i].power = false; } } wdt_reset(); } int main(void) { uint8_t i; WDT_off(); setup(); // insert a startup delay of 20s to prevent interference with redboot // interrupts are already enabled at this stage // so the pulses are counted but not sent to the deamon for (i=0; i<4; i++) _delay_ms(5000); serialFlush(); printString("\n"); WDT_on(); for (;;) loop(); return 0; }