⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avr

📁 使用MEGA88制作电表的程序
💻
📖 第 1 页 / 共 5 页
字号:
2566A-AVR-07/04
As a summary, some firmware defaults are shown in the table below.
Table 7. Firmware Defaults.
Parameter Value Note
Compiler IAR 3.10
Device ATmega88 8kB FLASH, 1024b RAM, 512b EEPROM
Starting current 2mA See I_MIN constant
Calculation cycle 200ms See system clock and NMAX constant
Energy pulse rate 10000 imp/kWh See MC calibration coefficient
Energy pulse length 50ms See system clock and OCR1B initialisation
Display pulse rate 100 imp/kWh See DPC calibration coefficient
Display pulse length 100ms See system clock and DP_ON constant
Terminal setting 38400 bauds See USART initialisation
Setting Up the Meter
In stand-alone mode the meter only needs to be attached to the mains wires. During
calibration and testing, the meter must be connected to a computer terminal via the
serial interface and for programming purposes the meter must be connected to a
computer via the In-System Programming (ISP) interface.
It is recommended to isolate the meter from other hardware using optocouplers.
The meter must be operated with care when connected to mains (high voltage) lines.
One common pitfall is to neglect ground wirings and nonchalantly proceed with
connecting the meter to external hardware, such as a personal computer. The results
can be destructive.
Figure 11. Short Circuit between Live and Neutral Wire.
OTHER
HARDWARE 465
L N
SHORT
CIRCUIT
The meter has no galvanic isolation from live wires, which means even the apparently
harmless meter ground is firmly bolted to the live voltage. Connecting the meter to
another mains-powered hardware can lead to short-circuit between live and neutral
wires, as shown in the figure above. Not a recommend thing to do.
Summary:
Defaults
Warning:
Ground Precaution
22 AVR465
2566A-AVR-07/04
For stand-alone operation, the meter is simply connected to the mains wires as
illustrated in the schematic. The meter automatically starts to measure power and
stores energy consumption data in the display counter. Note that the meter needs to
be calibrated before reliable measurement data can be expected.
For programming, calibration and testing purposes the meter needs to be temporarily
connected to external hardware, such as a computer or a microcontroller. The
external hardware writes the firmware into the FLASH memory of the meter MCU. It
then assesses the accuracy of the meter and writes calibration data into the
EEPROM of the MCU. The meter is ready to run in stand-alone mode after
programming and calibration has completed successfully.
A general illustration on how to connect the meter for programming, calibration and
metering is shown in Figure 12. Please note that this is a generalized block schematic
and does not include all recommended safety precautions.
Figure 12. Connecting the Meter to External Hardware.
465
STK500,
AVRISP,
JTAGICE mkII,
or similar SIGNAL
GENERATOR HIGH
VOLTAGE
& CURRENT
USART
ISP
STK500,
or similar
Computer
running
AVR Studio
and
terminal
RS-232
RS-232
It is recommended to create a galvanic isolation barrier between the mains-coupled
meter and the other hardware. This can be done using optocouplers at the ISP and
USART interface or at the RS-232 interface. Optocoupling both ISP and USART
makes the STK500 and debugger “safe”, but requires many optocouplers.
Alternatively, creating the galvanic barrier at the RS-232 interfaces requires less
optocouplers but makes all devices except the PC hazardous.
Another alternative is to use an isolation transformer to create a floating front-end,
which can then be bolted to the same ground as the rest of the electronics.
The meter AVR is programmed via the In-System Programming (ISP) interface, which
means the device does not need to be removed for programming. The ISP connector
(J1, see schematic) is connected to a programmer (STK500, ATAVRISP, or similar)
or a debugger (ATJTAGICEmkII, or similar). Software such as AVR Studio can then
be used for programming FLASH and EEPROM of the target AVR.
The meter constantly sends measurement data via the asynchronous interface. To
read measurement data (also during calibration), the asynchronous interface must be
connected. The asynchronous connector (J2, see schematic) is wired to a
microcontroller directly or to a computer via a RS-232 buffer. The asynchronous
interface cannot be wired to a RS-232 port directly, since it is not buffered.
A RS-232 buffer is readily found on the STK500. Connect the asynchronous interface
of the meter to STK500 pins labeled RS232 SPARE. Then connect the STK500 Dconnector
labeled RS232 SPARE to the serial port of the computer. Use a terminal
software of choice to read data from the serial port.
A general illustration on how to connect the meter for programming, calibration and
metering is shown in Figure 13. Also shown in the figure is a recommended optical
isolation board.
Hardware Set-Up
Warning: Galvanic Isolation
Programming
Calibration & Metering
AVR465
23
2566A-AVR-07/04
Figure 13. Example Set-Up for Calibration and/or Metering.
To download the firmware into the meter first open the compiler workspace, compile
the project and use AVR Studio to send the compiled file to the target, as described
above. Alternatively, use AVR Studio to send the precompiled firmware directly to the
target.
Set up fuses to match hardware environment. The default is to use external crystal,
with full output swing and long start-up time. This means fuses CKSEL are set to
0111 and fuses SUT to 11. Fuses CKDIV8 and CKOUT should be unprogrammed.
Calibration Example
There are many methods for calibrating the meter. Here is presented one. This
method requires a programmable, high-voltage, high-current signal generator. Also
required is hardware for programming the AVR in the meter. An STK500 can be used,
but a debugger such as JTAGICE mkII is recommended, since it allows EEPROM to
be edited more easily.
Connect the signal generator to the meter front-end. Then connect the meter to a PC
using a RS-232 buffer circuit, such as the spare RS-232 interface on the STK500.
Start a terminal software on the PC and configure it to receive data from the meter.
Start by writing the following set of calibration coefficients to EEPROM. This will set
phase calibration to zero and gain to unity in all calculations.
Table 8. Initialization of Calibration Coefficients.
ADDR. + 0x00 + 0x01 + 0x02 + 0x03 + 0x04 + 0x05 + 0x06 + 0x07
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01
0x08 0x00 0x01 0x00 0x01 0x00 0x01 0x00 0x01
0x10 0x00 0x01 0xFF 0xFF 0x27 0x10 0x00 0x64
0x18 0x00 0x00 0x00 0x00 0x43 0x57 0x00 0x00
If using a programmer, create a text file as shown below and load the contents of the
file into the EEPROM. The file content is of type Intel Hex.
Firmware Set-Up
Set-Up
Step 1 - Initialisation
465
RS232 SPARE
SIGNAL
GENERATOR
HIGH VOLTAGE
&
HIGH CURRENT
STK500
RS232 CTRL
RS232 SPARE
ISP6PIN
J2
J1
COM1
COM2
PC
DC
DC
RxD
/RST
SCK
MOSI
VOUT
V0
VIN
GND
TxD
MISO
GALVANIC ISOLATION "SAFE" SIDE
POWER SUPPLY AVR Studio
Terminal
24 AVR465
2566A-AVR-07/04
:1000000000000000000000010001000100010001EB
:100010000001FFFF271000640000000043570000AC
:00000001FF
Next, reset the meter. This will load the calibration coefficients into memory.
Apply nominal voltage and large – preferably full-scale – current to the meter. Voltage
and current should be in phase, i.e. the power factor should be unity. Allow
measurement results to settle for a few seconds and then record voltage and current
readings. Calculate gain coefficients using the previously presented equations.
Table 9. Example Readings.
Input Measured Gain Error Calibration Coefficient
U = 1241.699 5.3987 UG = 65536 / 5.3987 = 0x2F6B
IL = 630.63784 126.1276 ILG[0] = 0x007E
230V
5A
PF 1 IN = 626.58277 125.3166 ING[0] = 0x007D
Using a programmer or a debugger of choice, load gain coefficients to EEPROM. In
this example, the following coefficients are loaded.
Table 10. New Calibration Coefficients.
Address Coefficient Data
0x06 ILG0, High Byte 0x00
0x07 ILG0, Low Byte 0x7E
0x0C ING0, High Byte 0x00
0x0D ING0, Low Byte 0x7D
0x12 UG, High Byte 0x2F
0x13 UG, Low Byte 0x6B
0x1C CRCW, High Byte 0x43
0x1D CRCW, Low Byte 0x57
Reset the meter.
Continue to apply nominal voltage and a large current to the meter. Allow readings to
stabilize and then record active power, voltage and current readings. Use voltage and
current readings to calculate apparent power; S = UI. Compare with active power
readings and evaluate phase error, as follows:
Equation 27. Phase Error.
??
?
?
??
?
?
=
S
P
acos φ
Derive phase calibration coefficient based on phase error, and add the result to the
default phase calibration coefficient. An example is shown in the table below.
Step 2 - Gain
Step 3 - Phase
AVR465
25
2566A-AVR-07/04
Table 11. Example Readings.
Input Measured Apparent Power Phase Error PCC
U = 230.038 PCC0 = 0
IL = 4.99541
PL = 1131.194
SL = 1149.1341 PhiL = 10.138 PCC1 = 0x7359
230V
5A
PF 1
IN = 5.00943
PN = 1106.641
SN = 1152.3593 PhiN = 16.193 PCC2 = 0xB83E
Enter phase calibration coefficients into EEPROM (locations 0x00…0x05), leave
other coefficients untouched, and write 0x4357 to CRCW. Reset the meter.
Still apply nominal voltage and high current to the meter. Record current
measurements, and then reduce input current by a factor of about 8, so that the meter
automatically switches to medium amplification range and that the sampled data has
a high amplitude. Again record current and then again reduce input current by a factor
of about 8. Record third set of current measurements. Then use the recorded data to
calculate final gain coefficients for all current ranges, as shown in the table below.
Table 12. Example Readings.
Input Measured Gain Error Calibration Coefficient
230V, 5A
IL = 4.90191
IN = 4.93608
0.9804 x 126.1276 = 123.66
0.9872 x 125.3166 = 123.71
ILG[0] = 0x007C
ING[0] = 0x007B
230V, 1A
IL = 1017.25948
IN =1016.93559
1017.25948 / 1 = 1017
1017
ILG[1] = 0x03F9
ING[1] = 0x03F9
230V, 0.1A
IL = 687.69546
IN = 690.78431
687.69546 / 0.1 = 6877
6908
ILG[2] = 0x1ADD
ING[2] = 0x1AFC
Note that the gain errors for low amplification are multiplied with the previously
evaluated. This is because the previous coefficients were in effect during this set of
measurement.
Write new current gain coefficients to EEPROM (locations 0x06…0x11), update
CRCW to 0x4357 and reset meter.
The example meter was tested after the above calibration. The signal generator was
connected to the front end of the meter and was programmed to perform a current
sweep from 10mA to 10A. Active power readings were recorded from the Energy
Pulse output. All measurement results were within 1%.
Step 4 - Final Gain
Step 5 - Verify
26 AVR465
2566A-AVR-07/04
Measurement Results
Measurement results were recorded for a manually assembled prototype at room
temperature.
The following table shows typical current consumption figures for this application.
Table 13. Typical Current Consumption Figures.
Condition Typical Duration
Meter Running, All LED’s Off, Display Pulses Low 3.0 mA Continuous
Meter Running, All LED’s On, Display Pulses Low 7.5 mA Continuous
Meter Running, All LED’s On, Display Counter Increment 12.5 mA 100ms
The measurement accuracy was tested on a calibrated energy meter. The reference
was a programmable, three-phase signal generator (type Omicron, model CMC56).
The signal generator was programmed to supply the meter with known voltages and
currents at various power factors and then read back the frequency of the energy
pulse (EP) at each setting.
For each measurement point, only one pulse interval was recorded, i.e. no averaging
was done. The results from 57 measurement points were then gathered into three
curves illustrating the dynamic accuracy for three given power factors.
Figure 14 illustrates how meter accuracy stays within 1% over a wide dynamic range.
The current scale has been normalised, i.e. 1.000 corresponds to maximum current
(10A, 20A, or whatever). The solid line represents the relative error at unity power
factor, i.e. when voltage and current are in phase. The dotted line indicates relative
error at power factor 0.8, capacitive, and the dashed line shows relative error at
power factor 0.5, inductive.
Figure 14. Typical Measurement Accuracy.
PF = 0.5i
PF = 1
PF = 0.8c
1.000 0.100 0.001 0.010
+1.00%
-1.00%
+2.00%
+3.00%
-2.00%
-3.00%
(SCALE NORMALISED)
Power Consumption
Accuracy
AVR465
27
2566A-AVR-07/04
Flow Charts
Figure 15. Main Program.
RESET
INITIALISE
KEY PRESSED?
CYCLE FULL?
KEY SERVICE
CYCLE SERVICE
YES
YES
NO
NO
Main Program
28 AVR465
2566A-AVR-07/04
Figure 16. Initialisation.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -