uc: initial support for pwr messages

This commit is contained in:
Bart Van Der Meerssche 2010-02-26 21:39:46 +00:00
parent 6d907016dd
commit 4e21bf5452
4 changed files with 71 additions and 47 deletions

View File

@ -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)"'
#####################################################################################
#####################################################################################

View File

@ -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 <avr/wdt.h>
// 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<<WDIE);
}
@ -222,53 +230,64 @@ void setup()
sei();
}
void send(const struct sensor *measurement)
void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux)
{
uint8_t i = 46;
char pulse[49];
cli();
uint32_t value = measurement->value;
sei();
// generate pulse message structure
strcpy(pulse, "pls ");
strcpy(&pulse[4], measurement->id);
strcpy(&pulse[36], ":0000000000\n");
do { // generate digits in reverse order
pulse[i--] = '0' + value % 10; // get next digit
} while ((value /= 10) > 0); // delete it
printString(pulse);
char message[49];
uint32_t value = 0;
switch (msg_type) {
case PULSE:
// blink the green LED
PORTB |= (1<<PB5);
_delay_ms(100);
_delay_ms(50);
PORTB &= ~(1<<PB5);
cli();
value = measurement->value;
sei();
strcpy(message, "pls ");
break;
case POWER:
cli();
uint16_t adc = aux->adc;
sei();
MacU16X16to32(value, adc, POWERCONST);
value /= 1000;
strcpy(message, "pwr ");
break;
}
strcpy(&message[4], measurement->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();
}

View File

@ -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);

View File

@ -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];