diff --git a/uc/Makefile b/uc/Makefile index 21f48e0..fb2d8ae 100755 --- a/uc/Makefile +++ b/uc/Makefile @@ -87,7 +87,6 @@ F_CPU = 1000000 # TYPE = 2301 METERCONST = 7091 -POWERCONST = 12466 # SENSOR0 = 0123456789abcdef0123456789abcde0 SENSOR1 = 0123456789abcdef0123456789abcde1 diff --git a/uc/main.c b/uc/main.c index 01671ae..6d28722 100644 --- a/uc/main.c +++ b/uc/main.c @@ -38,12 +38,13 @@ #include // 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 measurements[4]; volatile uint8_t muxn = 0; +volatile uint16_t timer = 0; // interrupt service routine for INT0 ISR(INT0_vect) { @@ -78,22 +79,26 @@ 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) { + if (aux[muxn].nano > WATT) { measurements[muxn].value++; 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 &= 0x7; + if (!(muxn &= 0x7)) timer++; + if (timer > SECOND) timer = 0; // but only use ADC0 and 1 if (muxn < 2) { @@ -236,11 +241,14 @@ void send(uint8_t msg_type, const struct sensor *measurement, const struct state char message[49]; uint32_t value = 0; + int32_t rest; + uint8_t pulse_count; + switch (msg_type) { case PULSE: // blink the green LED PORTB |= (1<adc; + rest = aux->nano_end - aux->nano_start; + pulse_count = aux->pulse_count_final; sei(); - MacU16X16to32(value, adc, POWERCONST); - value /= 1000; + MacU16X16to32(value, (uint16_t)(labs(rest)/65536), 242); + value /= 1024; + + if (rest >= 0) + value += pulse_count*3600; + else + value = pulse_count*3600 - value; strcpy(message, "pwr "); break; @@ -283,9 +297,9 @@ void loop() 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]); - aux[i].toggle = false; + aux[i].power = false; } } wdt_reset(); diff --git a/uc/main.h b/uc/main.h index d84cc7f..5ccb93f 100644 --- a/uc/main.h +++ b/uc/main.h @@ -23,6 +23,9 @@ # define PULSE 0 # define POWER 1 +# define WATT 1000000000 +# define SECOND 487 // rounded down from 488.28125 - 1 + #ifndef SENSOR0 #define SENSOR0 "0123456789abcdef0123456789abcde0" #endif @@ -46,32 +49,26 @@ #ifndef METERCONST #if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate #define METERCONST 6783 - #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 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 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 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 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 POWERCONST 12888 #warning "240V - 3-phase selected. METERCONST set to 7331" #endif #endif @@ -121,7 +118,12 @@ struct state { boolean toggle; uint32_t nano; 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 {