uc: second implementation of power messaging

This commit is contained in:
Bart Van Der Meerssche 2010-02-28 20:11:37 +00:00
parent 4e21bf5452
commit 7b8c322628
3 changed files with 40 additions and 25 deletions

View File

@ -87,7 +87,6 @@ F_CPU = 1000000
# #
TYPE = 2301 TYPE = 2301
METERCONST = 7091 METERCONST = 7091
POWERCONST = 12466
# #
SENSOR0 = 0123456789abcdef0123456789abcde0 SENSOR0 = 0123456789abcdef0123456789abcde0
SENSOR1 = 0123456789abcdef0123456789abcde1 SENSOR1 = 0123456789abcdef0123456789abcde1

View File

@ -38,12 +38,13 @@
#include <avr/wdt.h> #include <avr/wdt.h>
// variable declarations // variable declarations
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 state aux[4] = {{false, false, START, 0}, {false, false, START, 0}, {false, false, START, 0}, {false, false, START, 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];
volatile uint8_t muxn = 0; volatile uint8_t muxn = 0;
volatile uint16_t timer = 0;
// interrupt service routine for INT0 // interrupt service routine for INT0
ISR(INT0_vect) { ISR(INT0_vect) {
@ -78,22 +79,26 @@ 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) { if (aux[muxn].nano > WATT) {
aux[muxn].adc = ADC;
aux[muxn].toggle = true;
aux[muxn].count = 0;
}
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 -= 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;
} }
} }
// rotate amongst the available ADC input channels (0 to 7) // rotate the available ADC input channels (0 to 7)
muxn++; muxn++;
muxn &= 0x7; if (!(muxn &= 0x7)) timer++;
if (timer > SECOND) timer = 0;
// but only use ADC0 and 1 // but only use ADC0 and 1
if (muxn < 2) { if (muxn < 2) {
@ -236,11 +241,14 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state
char message[49]; char message[49];
uint32_t value = 0; uint32_t value = 0;
int32_t rest;
uint8_t pulse_count;
switch (msg_type) { switch (msg_type) {
case PULSE: case PULSE:
// blink the green LED // blink the green LED
PORTB |= (1<<PB5); PORTB |= (1<<PB5);
_delay_ms(50); _delay_ms(20);
PORTB &= ~(1<<PB5); PORTB &= ~(1<<PB5);
cli(); cli();
@ -252,11 +260,17 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state
case POWER: case POWER:
cli(); cli();
uint16_t adc = aux->adc; rest = aux->nano_end - aux->nano_start;
pulse_count = aux->pulse_count_final;
sei(); sei();
MacU16X16to32(value, adc, POWERCONST); MacU16X16to32(value, (uint16_t)(labs(rest)/65536), 242);
value /= 1000; value /= 1024;
if (rest >= 0)
value += pulse_count*3600;
else
value = pulse_count*3600 - value;
strcpy(message, "pwr "); strcpy(message, "pwr ");
break; break;
@ -283,9 +297,9 @@ void loop()
aux[i].pulse = false; aux[i].pulse = false;
} }
if (aux[i].toggle == true && i < 2) { if (aux[i].power == true) {
send(POWER, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]); send(POWER, (const struct sensor *)&measurements[i], (const struct state *)&aux[i]);
aux[i].toggle = false; aux[i].power = false;
} }
} }
wdt_reset(); wdt_reset();

View File

@ -23,6 +23,9 @@
# define PULSE 0 # define PULSE 0
# define POWER 1 # define POWER 1
# define WATT 1000000000
# define SECOND 487 // rounded down from 488.28125 - 1
#ifndef SENSOR0 #ifndef SENSOR0
#define SENSOR0 "0123456789abcdef0123456789abcde0" #define SENSOR0 "0123456789abcdef0123456789abcde0"
#endif #endif
@ -46,32 +49,26 @@
#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 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 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 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 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 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 POWERCONST 12888
#warning "240V - 3-phase selected. METERCONST set to 7331" #warning "240V - 3-phase selected. METERCONST set to 7331"
#endif #endif
#endif #endif
@ -121,7 +118,12 @@ struct state {
boolean toggle; boolean toggle;
uint32_t nano; uint32_t nano;
uint16_t adc; uint16_t adc;
uint16_t count;
boolean power;
uint32_t nano_start;
uint32_t nano_end;
uint8_t pulse_count;
uint8_t pulse_count_final;
}; };
struct sensor { struct sensor {