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

@ -87,14 +87,14 @@ F_CPU = 1000000
# #
TYPE = 2301 TYPE = 2301
METERCONST = 7091 METERCONST = 7091
MUXN = 0 POWERCONST = 12466
# #
SENSOR0 = 0123456789abcdef0123456789abcde0 SENSOR0 = 0123456789abcdef0123456789abcde0
SENSOR1 = 0123456789abcdef0123456789abcde1 SENSOR1 = 0123456789abcdef0123456789abcde1
SENSOR2 = 0123456789abcdef0123456789abcde2 SENSOR2 = 0123456789abcdef0123456789abcde2
SENSOR3 = 0123456789abcdef0123456789abcde3 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 // main.c : AVR uC code for flukso sensor board
//
// Copyright (c) 2008-2009 jokamajo.org // Copyright (c) 2008-2009 jokamajo.org
// 2010 flukso.net
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License // modify it under the terms of the GNU General Public License
@ -36,12 +38,12 @@
#include <avr/wdt.h> #include <avr/wdt.h>
// variable declarations // 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 EEMEM EEPROM_measurements[4] = {{SENSOR0, START}, {SENSOR1, START}, {SENSOR2, START}, {SENSOR3, START}};
volatile struct sensor measurements[4]; volatile struct sensor measurements[4];
uint8_t muxn = 0; volatile uint8_t muxn = 0;
// interrupt service routine for INT0 // interrupt service routine for INT0
ISR(INT0_vect) { ISR(INT0_vect) {
@ -76,16 +78,22 @@ ISR(TIMER2_OVF_vect) {
if (muxn < 2) { if (muxn < 2) {
MacU16X16to32(aux[muxn].nano, METERCONST, ADC); 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) { if (aux[muxn].nano > 1000000000) {
measurements[muxn].value++; measurements[muxn].value++;
aux[muxn].pulse = true; aux[muxn].pulse = true;
aux[muxn].nano -= 1000000000; aux[muxn].nano -= 1000000000;
aux[muxn].debug = ADC;
} }
} }
// rotate amongst the available ADC input channels (0 to 7) // rotate amongst the available ADC input channels (0 to 7)
muxn = ++muxn & 0x7; muxn++;
muxn &= 0x7;
// but only use ADC0 and 1 // but only use ADC0 and 1
if (muxn < 2) { if (muxn < 2) {
@ -153,7 +161,7 @@ void WDT_off(void) {
// enable WDT // enable WDT
void WDT_on(void) { void WDT_on(void) {
// enable the watchdog timer (1s) // enable the watchdog timer (1s)
wdt_enable(WDTO_1S); wdt_enable(WDTO_2S);
// set watchdog interrupt enable flag // set watchdog interrupt enable flag
WDTCSR |= (1<<WDIE); WDTCSR |= (1<<WDIE);
} }
@ -222,53 +230,64 @@ void setup()
sei(); 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; uint8_t i = 46;
char pulse[49]; char message[49];
uint32_t value = 0;
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);
switch (msg_type) {
case PULSE:
// blink the green LED // blink the green LED
PORTB |= (1<<PB5); PORTB |= (1<<PB5);
_delay_ms(100); _delay_ms(50);
PORTB &= ~(1<<PB5); 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() void loop()
{ {
uint8_t i; 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++) { for (i=0; i<4; i++) {
if (aux[i].pulse == true) { if (aux[i].pulse == true) {
if (i < 2) { send(PULSE, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]);
//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]);
aux[i].pulse = false; 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(); wdt_reset();
} }

View File

@ -20,6 +20,9 @@
// $Id$ // $Id$
// //
# define PULSE 0
# define POWER 1
#ifndef SENSOR0 #ifndef SENSOR0
#define SENSOR0 "0123456789abcdef0123456789abcde0" #define SENSOR0 "0123456789abcdef0123456789abcde0"
#endif #endif
@ -43,36 +46,38 @@
#ifndef METERCONST #ifndef METERCONST
#if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate #if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 6783 #define METERCONST 6783
// #define MUXN 0 #define POWERCONST 11925
#warning "220V - 1-phase selected. METERCONST set to 6783" #warning "220V - 1-phase selected. METERCONST set to 6783"
#elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 6721 #define METERCONST 6721
// #define MUXN 1 #define POWERCONST 11816
#warning "220V - 3-phase selected. METERCONST set to 6721" #warning "220V - 3-phase selected. METERCONST set to 6721"
#elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate #elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 7091 #define METERCONST 7091
// #define MUXN 0 #define POWERCONST 12466
#warning "230V - 1-phase selected. METERCONST set to 7091" #warning "230V - 1-phase selected. METERCONST set to 7091"
#elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 7026 #define METERCONST 7026
// #define MUXN 1 #define POWERCONST 12352
#warning "230V - 3-phase selected. METERCONST set to 7026" #warning "230V - 3-phase selected. METERCONST set to 7026"
#elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate #elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate
#define METERCONST 7399 #define METERCONST 7399
// #define MUXN 0 #define POWERCONST 13007
#warning "240V - 1-phase selected. METERCONST set to 7399" #warning "240V - 1-phase selected. METERCONST set to 7399"
#elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate #elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate
#define METERCONST 7331 #define METERCONST 7331
// #define MUXN 1 #define POWERCONST 12888
#warning "240V - 3-phase selected. METERCONST set to 7331" #warning "240V - 3-phase selected. METERCONST set to 7331"
#endif #endif
#endif #endif
//#define POWERCONST (METERCONST*1758)/1000 // in mW
#define START 0 #define START 0
#define END3 0xffffffff #define END3 0xffffffff
#define END2 0xeeeeeeee #define END2 0xeeeeeeee
@ -115,7 +120,8 @@ struct state {
boolean pulse; boolean pulse;
boolean toggle; boolean toggle;
uint32_t nano; uint32_t nano;
uint16_t debug; uint16_t adc;
uint16_t count;
}; };
struct sensor { struct sensor {
@ -126,5 +132,4 @@ struct sensor {
// prototypes // prototypes
void WDT_off(void); void WDT_off(void);
void WDT_on(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 // 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 // location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read. // 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]; unsigned char rx_buffer[RX_BUFFER_SIZE];