From 4e21bf5452dc0ce996f031338c7fa8d1ab2208d4 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Fri, 26 Feb 2010 21:39:46 +0000 Subject: [PATCH] uc: initial support for pwr messages --- uc/Makefile | 6 ++-- uc/main.c | 87 ++++++++++++++++++++++++++++------------------ uc/main.h | 23 +++++++----- uc/wiring/serial.c | 2 +- 4 files changed, 71 insertions(+), 47 deletions(-) diff --git a/uc/Makefile b/uc/Makefile index 07c21b3..21f48e0 100755 --- a/uc/Makefile +++ b/uc/Makefile @@ -83,18 +83,18 @@ F_CPU = 1000000 # # Predefine the TYPE and SENSORx C macros in main.h via this Makefile. Override the # defaults on the command line by typing: -# make TYPE= xxxx SENSOR0=yyyy ... +# make TYPE=xxxx SENSOR0=yyyy ... # TYPE = 2301 METERCONST = 7091 -MUXN = 0 +POWERCONST = 12466 # SENSOR0 = 0123456789abcdef0123456789abcde0 SENSOR1 = 0123456789abcdef0123456789abcde1 SENSOR2 = 0123456789abcdef0123456789abcde2 SENSOR3 = 0123456789abcdef0123456789abcde3 # -CEXTRA = -D TYPE=$(TYPE) -D METERCONST=$(METERCONST) -D MUXN=$(MUXN) -D 'SENSOR0="$(SENSOR0)"' -D 'SENSOR1="$(SENSOR1)"' -D 'SENSOR2="$(SENSOR2)"' -D 'SENSOR3="$(SENSOR3)"' +CEXTRA = -D TYPE=$(TYPE) -D METERCONST=$(METERCONST) -D POWERCONST=$(POWERCONST) -D 'SENSOR0="$(SENSOR0)"' -D 'SENSOR1="$(SENSOR1)"' -D 'SENSOR2="$(SENSOR2)"' -D 'SENSOR3="$(SENSOR3)"' ##################################################################################### ##################################################################################### diff --git a/uc/main.c b/uc/main.c index 7b8b86b..01671ae 100644 --- a/uc/main.c +++ b/uc/main.c @@ -1,6 +1,8 @@ // // 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 @@ -36,12 +38,12 @@ #include // variable declarations -volatile struct state aux[4] = {{false, false, START, 0}, {false, false, START, 0}, {false, false, START, 0}, {false, false, START, 0}}; +volatile struct state aux[4] = {{false, false, START, 0, 0}, {false, false, START, 0, 244}, {false, false, START, 0, 0}, {false, false, START, 0, 0}}; volatile struct sensor EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}}; volatile struct sensor measurements[4]; -uint8_t muxn = 0; +volatile uint8_t muxn = 0; // interrupt service routine for INT0 ISR(INT0_vect) { @@ -76,16 +78,22 @@ ISR(TIMER2_OVF_vect) { if (muxn < 2) { MacU16X16to32(aux[muxn].nano, METERCONST, ADC); + if (++aux[muxn].count > 487) { + aux[muxn].adc = ADC; + aux[muxn].toggle = true; + aux[muxn].count = 0; + } + if (aux[muxn].nano > 1000000000) { measurements[muxn].value++; aux[muxn].pulse = true; aux[muxn].nano -= 1000000000; - aux[muxn].debug = ADC; } } // rotate amongst the available ADC input channels (0 to 7) - muxn = ++muxn & 0x7; + muxn++; + muxn &= 0x7; // but only use ADC0 and 1 if (muxn < 2) { @@ -153,7 +161,7 @@ void WDT_off(void) { // enable WDT void WDT_on(void) { // enable the watchdog timer (1s) - wdt_enable(WDTO_1S); + wdt_enable(WDTO_2S); // set watchdog interrupt enable flag WDTCSR |= (1<value; - sei(); + switch (msg_type) { + case PULSE: + // blink the green LED + PORTB |= (1<id); - strcpy(&pulse[36], ":0000000000\n"); + cli(); + value = measurement->value; + sei(); + + strcpy(message, "pls "); + break; - do { // generate digits in reverse order - pulse[i--] = '0' + value % 10; // get next digit - } while ((value /= 10) > 0); // delete it + case POWER: + cli(); + uint16_t adc = aux->adc; + sei(); - printString(pulse); + MacU16X16to32(value, adc, POWERCONST); + value /= 1000; - // blink the green LED - PORTB |= (1<id); + strcpy(&message[36], ":0000000000\n"); + + do { // generate digits in reverse order + message[i--] = '0' + value % 10; // get next digit + } while ((value /= 10) > 0); // delete it + + printString(message); } void loop() { uint8_t i; - // check whether we have to send out a pls to the deamon + // 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) { - if (i < 2) { - //debugging - printString("msg ADC"); - printInteger((long)i); - printString(" sample value: "); - printIntegerInBase((unsigned long)aux[i].debug, 10); - printString("\n"); - } - send((const struct sensor *)&measurements[i]); + send(PULSE, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]); aux[i].pulse = false; } - } - // reset the watchdog timer + if (aux[i].toggle == true && i < 2) { + send(POWER, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]); + aux[i].toggle = false; + } + } wdt_reset(); } diff --git a/uc/main.h b/uc/main.h index e7b01c7..d84cc7f 100644 --- a/uc/main.h +++ b/uc/main.h @@ -20,6 +20,9 @@ // $Id$ // +# define PULSE 0 +# define POWER 1 + #ifndef SENSOR0 #define SENSOR0 "0123456789abcdef0123456789abcde0" #endif @@ -43,36 +46,38 @@ #ifndef METERCONST #if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate #define METERCONST 6783 -// #define MUXN 0 + #define POWERCONST 11925 #warning "220V - 1-phase selected. METERCONST set to 6783" #elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate #define METERCONST 6721 -// #define MUXN 1 + #define POWERCONST 11816 #warning "220V - 3-phase selected. METERCONST set to 6721" #elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate #define METERCONST 7091 -// #define MUXN 0 + #define POWERCONST 12466 #warning "230V - 1-phase selected. METERCONST set to 7091" #elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate #define METERCONST 7026 -// #define MUXN 1 + #define POWERCONST 12352 #warning "230V - 3-phase selected. METERCONST set to 7026" #elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate #define METERCONST 7399 -// #define MUXN 0 + #define POWERCONST 13007 #warning "240V - 1-phase selected. METERCONST set to 7399" #elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate #define METERCONST 7331 -// #define MUXN 1 + #define POWERCONST 12888 #warning "240V - 3-phase selected. METERCONST set to 7331" #endif #endif +//#define POWERCONST (METERCONST*1758)/1000 // in mW + #define START 0 #define END3 0xffffffff #define END2 0xeeeeeeee @@ -115,7 +120,8 @@ struct state { boolean pulse; boolean toggle; uint32_t nano; - uint16_t debug; + uint16_t adc; + uint16_t count; }; struct sensor { @@ -126,5 +132,4 @@ struct sensor { // prototypes void WDT_off(void); void WDT_on(void); -void send(const struct sensor *measurement); - +void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux); diff --git a/uc/wiring/serial.c b/uc/wiring/serial.c index 0e12d24..57f9503 100755 --- a/uc/wiring/serial.c +++ b/uc/wiring/serial.c @@ -28,7 +28,7 @@ // using a ring buffer (I think), in which rx_buffer_head is the index of the // location to which to write the next incoming character and rx_buffer_tail // is the index of the location from which to read. -#define RX_BUFFER_SIZE 64 +#define RX_BUFFER_SIZE 1 unsigned char rx_buffer[RX_BUFFER_SIZE];