2009-06-05 19:59:55 +00:00
|
|
|
//
|
2009-11-16 00:56:00 +00:00
|
|
|
// main.h : AVR uC header file for flukso sensor board
|
2009-06-05 19:59:55 +00:00
|
|
|
// Copyright (c) 2008-2009 jokamajo.org
|
2010-01-05 13:51:09 +00:00
|
|
|
// Copyright (c) 2010 flukso.net
|
2009-06-05 19:59:55 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
2009-06-22 12:54:51 +00:00
|
|
|
// $Id$
|
2009-06-05 19:59:55 +00:00
|
|
|
//
|
|
|
|
|
2010-02-26 21:39:46 +00:00
|
|
|
# define PULSE 0
|
|
|
|
# define POWER 1
|
|
|
|
|
2010-02-28 20:11:37 +00:00
|
|
|
# define WATT 1000000000
|
|
|
|
# define SECOND 487 // rounded down from 488.28125 - 1
|
|
|
|
|
2010-01-05 20:05:10 +00:00
|
|
|
#ifndef SENSOR0
|
|
|
|
#define SENSOR0 "0123456789abcdef0123456789abcde0"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR1
|
|
|
|
#define SENSOR1 "0123456789abcdef0123456789abcde1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR2
|
|
|
|
#define SENSOR2 "0123456789abcdef0123456789abcde2"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR3
|
|
|
|
#define SENSOR3 "0123456789abcdef0123456789abcde3"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TYPE
|
|
|
|
#define TYPE 2301
|
|
|
|
#endif
|
2009-10-28 14:26:47 +00:00
|
|
|
|
2010-01-18 23:24:11 +00:00
|
|
|
#ifndef METERCONST
|
|
|
|
#if TYPE == 2201 // 220V - 1-phase @ 488.28Hz sampling rate
|
|
|
|
#define METERCONST 6783
|
|
|
|
#warning "220V - 1-phase selected. METERCONST set to 6783"
|
2009-10-28 14:26:47 +00:00
|
|
|
|
2010-01-18 23:24:11 +00:00
|
|
|
#elif TYPE == 2203 // 220V - 3-phase @ 488.28Hz sampling rate
|
|
|
|
#define METERCONST 6721
|
|
|
|
#warning "220V - 3-phase selected. METERCONST set to 6721"
|
2010-01-05 13:51:09 +00:00
|
|
|
|
2010-02-12 19:57:07 +00:00
|
|
|
#elif TYPE == 2301 // 230V - 1-phase @ 488.28Hz sampling rate
|
2010-01-18 23:24:11 +00:00
|
|
|
#define METERCONST 7091
|
|
|
|
#warning "230V - 1-phase selected. METERCONST set to 7091"
|
2010-01-05 13:51:09 +00:00
|
|
|
|
2010-01-18 23:24:11 +00:00
|
|
|
#elif TYPE == 2303 // 230V - 3-phase @ 488.28Hz sampling rate
|
|
|
|
#define METERCONST 7026
|
|
|
|
#warning "230V - 3-phase selected. METERCONST set to 7026"
|
2009-10-28 21:05:24 +00:00
|
|
|
|
2010-01-18 23:24:11 +00:00
|
|
|
#elif TYPE == 2401 // 240V - 1-phase @ 488.28Hz sampling rate
|
|
|
|
#define METERCONST 7399
|
|
|
|
#warning "240V - 1-phase selected. METERCONST set to 7399"
|
2009-10-28 21:05:24 +00:00
|
|
|
|
2010-01-18 23:24:11 +00:00
|
|
|
#elif TYPE == 2403 // 240V - 3-phase @ 488.28Hz sampling rate
|
|
|
|
#define METERCONST 7331
|
|
|
|
#warning "240V - 3-phase selected. METERCONST set to 7331"
|
|
|
|
#endif
|
2009-10-28 14:26:47 +00:00
|
|
|
#endif
|
2009-06-05 19:59:55 +00:00
|
|
|
|
2010-02-26 21:39:46 +00:00
|
|
|
//#define POWERCONST (METERCONST*1758)/1000 // in mW
|
|
|
|
|
2009-06-05 19:59:55 +00:00
|
|
|
#define START 0
|
|
|
|
#define END3 0xffffffff
|
|
|
|
#define END2 0xeeeeeeee
|
|
|
|
#define END1 0xdddddddd
|
|
|
|
#define END0 0xcccccccc
|
|
|
|
|
2009-11-16 00:56:00 +00:00
|
|
|
|
|
|
|
// This macro performs a 16x16 -> 32 unsigned MAC in 37 cycles with operands and results in memory
|
|
|
|
// based on http://www2.ife.ee.ethz.ch/~roggend/publications/wear/DSPMic_v1.1.pdf par 3.4 and table 31.
|
|
|
|
#define MacU16X16to32(uint_32Acc, uint_16In1, uint_16In2) \
|
|
|
|
asm volatile ( \
|
|
|
|
"clr r2 \n\t" \
|
|
|
|
"mul %B2, %B1 \n\t" \
|
|
|
|
"movw r4, r0 \n\t" \
|
|
|
|
"mul %A2, %A1 \n\t" \
|
|
|
|
"add %A0, r0 \n\t" \
|
|
|
|
"adc %B0, r1 \n\t" \
|
|
|
|
"adc %C0, r4 \n\t" \
|
|
|
|
"adc %D0, r5 \n\t" \
|
|
|
|
"mul %B2, %A1 \n\t" \
|
|
|
|
"add %B0, r0 \n\t" \
|
|
|
|
"adc %C0, r1 \n\t" \
|
|
|
|
"adc %D0, r2 \n\t" \
|
|
|
|
"mul %A2, %B1 \n\t" \
|
|
|
|
"add %B0, r0 \n\t" \
|
|
|
|
"adc %C0, r1 \n\t" \
|
|
|
|
"adc %D0, r2 \n\t" \
|
|
|
|
"clr r1 \n\t" \
|
|
|
|
: \
|
|
|
|
"+r" (uint_32Acc) \
|
|
|
|
: \
|
|
|
|
"a" (uint_16In1), \
|
|
|
|
"a" (uint_16In2) \
|
|
|
|
: \
|
|
|
|
"r2", "r4", "r5" \
|
|
|
|
)
|
|
|
|
|
2009-06-05 19:59:55 +00:00
|
|
|
// datastructures
|
|
|
|
struct state {
|
|
|
|
boolean pulse;
|
|
|
|
boolean toggle;
|
|
|
|
uint32_t nano;
|
2010-02-26 21:39:46 +00:00
|
|
|
uint16_t adc;
|
2010-02-28 20:11:37 +00:00
|
|
|
|
|
|
|
boolean power;
|
|
|
|
uint32_t nano_start;
|
|
|
|
uint32_t nano_end;
|
|
|
|
uint8_t pulse_count;
|
|
|
|
uint8_t pulse_count_final;
|
2009-06-05 19:59:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sensor {
|
|
|
|
char id[33];
|
|
|
|
uint32_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// prototypes
|
|
|
|
void WDT_off(void);
|
|
|
|
void WDT_on(void);
|
2010-02-26 21:39:46 +00:00
|
|
|
void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux);
|