📄 avr
字号:
INITIALISE
Initialise()
Set up I/O pins.
Configure Timer /Counter.
Configure USART & ADC.
ReadCalibration() Read calibration coefficients
from EEPROM.
InitGainControl() Initialise variables that control
the gain.
SetGain(LOW) Set low gain on both current
input channels.
Turn on Watchdog & Interrupts
END
Initialisation
AVR465
29
2566A-AVR-07/04
Figure 17. Character Input Service.
KEY SERVICE
getchar() = 'x' OR
getchar() = 'X' ? Service for Key X
YES
NO
END
Character Input Service
30 AVR465
2566A-AVR-07/04
Figure 18. Calculation Cycle Service.
CYCLE SERVICE
RMS Current
Below Threshold? Clear Active Power
YES
NO
Clear CYCLE_FULL Flag
Sum = Accumulator
Accumulator = 0
Make a copy of accumulated data,
then clear accumulator to make
way for new data.
Adjust Gain &
Clear Gain Control Flags
Increase or decrease gain of current
channels, according to control flags.
Then reset flags.
Normalise &
Add/Subtract Offset
Normalise data, then add a constant
offset to it. Subtract, if accumulated
data is negative.
Calculate & Calibrate
Take square root of current and voltage
data, then multiply accumulated data
with calibration coefficients.
CSS
(Continued on next page)
Calculation Cycle Service
AVR465
31
2566A-AVR-07/04
END
Active Power
Above Threshold?
YES
NO
P(L) within
5% of P(N) ?
NO
YES
P(L) and P(N)
Both Positive ?
NO
YES
Display Measurements
Signal Earth Fault
Signal Reversed Current
Send measurement data over USART interface.
Voltage in volts, current in amps and active
power in watts.
SetPulse() Calculate and set new pulse rate,
based on active power measurements.
CSS
32 AVR465
2566A-AVR-07/04
Figure 19. SetPulse().
SetPulse(Power)
Power <> 0 ?
3600 x fCLK YES
NO
END
Take Absolute of Power
T < TMIN ?
YES
NO
Extended Counter
TOP Value Set ?
YES
NO
T = TMIN
Set New Compare Match for
T/C and New TOP Value for
Extended Counter.
Active power can be positive or negative,
depending on the flow direction of current.
Sign is irrelevant when calculting pulse interval.
Pulse Interval T = TMAX
Power x PRESC x MC
T =
fCLK = Clock frequency
PRESC = T/C Prescaler
MC = Meter constant
T > TMAX ?
NO
YES
Extended Counter = CMAX
Counter = CMAX
Extended Counter = T / CMAX
Counter = Remainder
Set Counter Value as
New T/C Compare Match
SetPulse()
AVR465
33
2566A-AVR-07/04
Figure 20. Timer/Counter Compare A.
TC1_COMPA_ISR
TOP Value Set? Increase Counter X
YES
NO
END
Counter X < TOP?
NO
YES
Set T/C to Clear Output
on Next Match
Counter X > TOP?
NO
YES
Clear Counter X
(Set or Clear T/C Output) This is handled automatically
by Timer/Counter hardware.
Clear Counter X, Set OCR to
Max, Clear on Next Match
Configure T/C to Clear
Output on Next Match
Increase Counter D
Counter D = DPC?
YES
NO
Set DPP Output High
Set Display Pulse Flag High
DPC is a calibration constant
and can therefore be adjusted.
TOP > 0 means extended counter is active.
Maintain counter as long as TOP > 0.
DPP and DPN pulses are maintained by ADC_ISR
function after display pulse flag has been.
Timer/Counter ISRs
34 AVR465
2566A-AVR-07/04
Figure 21. Timer/Counter Compare B.
TC1_COMPB_ISR
END
Configure T/C to
Clear Output on Next Match
Force Compare Match
Configure T/C to
Set Output on Next Match
AVR465
35
2566A-AVR-07/04
Figure 22. ADC ISR.
ADC_ISR
Voltage Sample? Invert Sample
NO
YES
END
Apply High-Pass Filter
Apply Phase Calibration
Current signals are inverted in
the gain stage. Invert them back.
Hysteresis Counter > 0 ?
YES
NO
Remove DC Offset from Signal.
Reset Gain Control Flags &
Decrease Counter
Sample > Max
or Sample < Min ?
YES
NO
Active Gain Control Flag
LESSGAIN
Filtered Sample
Above Threshold?
YES
NO
Clear Gain Control Flag
MOREGAIN
Accumulate Data and set
CYCLE_FULL Flag when Full
Increase ADC Multiplexer
Maintain DPP & DPN Outputs
ADC ISR
36 AVR465
2566A-AVR-07/04
Schematic
Figure 23. Schematic.
LOAD
L N
1 2 3 4 5 6 7 , kWh
Z1
PC6 (/RESET)
PD0 (RXD)
PD1 (TXD)
PD2
PD3
PD4
VCC
GND
PB6 (XTAL1)
PB7 (XTAL2)
PD5
PD6
PD7
PB0
PC5
PC4
PC3
(ADC2) PC2
(ADC1) PC1
(ADC0) PC0
GND
AREF
AVCC
(SCK) PB5
(MISO) PB4
(MOSI) PB3
(/SS) PB2
(OC1A) PB1
U1
T2
T1
AVCC
VCC
X1
Kim Meyer, Atmel Finland, 2004
MISO
SCK
/RST
VCC
MOSI
GND
J1
VCC
L50
EP
R52 D52
R51 D51
R50 D50
EARTH
REVDIR
DPP
DPN
230V
R10
D10
VOUT
U4
+VIN
GND
VCC
V1
RxD
GND
TxD
GND
J2
C57 C56
D11
C12 C11
C58 C59
C50
C10
C51
465
R35
R45
R24
U2
AVCC
U3
C52 C53
4
8
7
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
28
27
26
25
24
23
22
21
20
19
18
17
16
15
R40
R30
R44
U2B
7
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -