📄 avr
字号:
in Figure 4. The gain stage shown has a fast switching time and allows high AC gain
but a low DC gain.
Figure 4. Inverting Amplifier with Variable Gain Uses Bilateral Switches (74HC4066).
The gain of the inverting amplifier is as follows:
Equation 7. Gain Of Inverting Amplifier.
R1
R2 A ? =
Here R1 consists of the series connection of R11, R12 and R13. Gain is adjusted by
shorting out one of resistors R12 or R13. This is done using the bilateral switches,
which are controlled by two I/O pins of the MCU, shown as I/O0 and I/O1 in the figure.
Gain adjustment resistors are dimensioned such that each range has an amplification
of about eight times the previous. The number of switches may well be increased and
the gain difference decreased, however, it is not recommended to have a larger gain
difference than eight between two subsequent ranges. This is because gain
Calculating the Gain
VCC
I/O0
I/O1
RS
C1 R11 R12 R13
R2
R4
C2
R3
8 AVR465
2566A-AVR-07/04
differences of around ten, and higher, cause the signal to degrade below 1%
accuracy before it can be further amplified.
The table below illustrates how gain is adjusted from the MCU.
Table 2. Adjusting The Gain Of The Inverting Amplifier. R2 = 470 k?.
I/O0 I/O1 Range R1 Impedance Gain
Low Low Low R11+R12+R13 = 6.8k+39k+330k (2) -(470/375.8) = -1.25
Low High Medium R11 + R12 = 6.8k+39k -(470/45.8) = -10.26
High X (1) High R11 = 6.8k -(470/6.8) = -69.11
Notes: 1. No effect, when I/O0 is high
2. All switches open; all resistors connected in series
Shunt resistor RS and the current transformer are scaled such that a voltage signal of
no more than 1V peak-to-peak is present at the amplifier output when maximum
current flows through the primary of the current transformer and the amplifier is set to
minimum gain. For example, using a 2500:1 current transformer, a 68 ? resistor, and
setting amplification to minimum (see above), the voltage at the output is as follows:
Equation 8. Full-Scale Voltage At Amplifier Output.
PP RMS
RMS MAX
MAX 0.96V 0.34V
2500
10A 68? 1.25
N
I RS A U ≈ =
× ×
=
× ×
=
The input signal of the amplifier must be DC decoupled. This is done using capacitor
C1 in the previous figure. It should be noted that R1 + C1 form a high-pass filter
(HPF) that may distort signals, especially at high gains. The corner frequency, or the
–3dB point, of the HPF is calculated as follows:
Equation 9. Corner Frequency Of HPF.
C1 R1 π 2
1 f 3dB - × × ×
=
The closer the corner frequency of the HPF is to the frequency band of the interest,
the higher is the distortion of phase and amplitude. It is recommended to keep a
distance of at least 100x between the two frequencies. At 50Hz line frequency and R1
minimum = R11 = 6.8 k?, C1 should not be less than 47 μF.
The output signal of the gain stage is biased around the DC level present at the noninverting
input of the operational amplifier. Hence, this DC level should be exactly half
of the reference voltage of the ADC in the MCU. Assuming a 1.1V reference, the
voltage divider R3-R4 should produce a stable 0.55V. The voltage divider should
have a large impedance to keep the current consumption low, since high impedance
increases noise. For example, assuming 3V supply voltage, a suitable set of values is
3 M? + 560 k?.
The DC level is stabilized with one, or many, capacitors. It is recommended to include
at least one capacitor in the μF-range.
To make accurate measurements, the input signal must be as clean as possible,
especially at low amplitudes. Input signals with low amplitude are amplified before
being sampled and processed, which means any noise in the signal will be enlarged,
too. At the low end of the measurement range input signals have amplitudes below
Calculating Full-Scale
DC Decoupling
DC Bias
Eliminating Noise
AVR465
9
2566A-AVR-07/04
10mV, which means noise typically not visible on an oscilloscope (say, below 1mV)
may distort the signal by as much as 10%.
Noise is eliminated using good PCB planning and properly sized and placed filter
components.
Energy meters are prone to operate in harsh environments; meters are often subject
to over-voltages and current spikes. If such disturbances are not properly shielded
they may traverse all the way to the MCU and drive it outside operating limits.
There are many techniques available for filtering out unwanted disturbances. Rapid
voltage spikes are usually suppressed using varistors, which normally have very high
impedance. At a given threshold the impedance very rapidly decreases, causing a
short circuit, which then leads the excess energy to ground. Varistors must be picked
such that they do not break down if the spike is too large or lasts too long (i.e. if the
energy of the spike grows too large).
Much depending on the layout of the circuit board and connectors, some inputs may
be prone to pick up radiated noise. In some cases, even the circuit board tracks can
perform as antennas and pick up noise from the environment. Induced noise can be
throttled using well-placed diodes, which clamp signals between ground and supply
voltage.
EMC protection techniques are well covered in application note “AVR040: EMC
Design Considerations”.
The microcontroller section includes the AVR and the clock source to run it. Also
included are all digital input and output circuitry.
In order to keep power consumption low all LED’s are of low-current type. Lowcurrent
LED’s typically produce around 2mcd of light power for 2mA current. The
LED’s have a forward voltage of about 2V and using a series resistor of 1 k? will
therefore limit the current to about 1mA per LED. The resulting LED brightness is
good for most applications.
For display purposes, this application uses what is sometimes referred to as a
stepper motor counter. These types of counters are typically available in a price
competitive range and tend to be rather common in designs like this. The counter is
driven using a pulse waveform as illustrated in Figure 5.
Figure 5. Pulse Waveform To Drive Display Counter.
By default, pulse length tDPX is 100ms, but this is readily adjusted from the firmware to
any other value.
Naturally, the pulse outputs provided by the design can be used to drive standard
impulse counters, as well.
Protection Circuitry
Microcontroller Section
Signal LED’s
Display Counter
DPP
DPN
2tDPX
tDPX
tDPX
10 AVR465
2566A-AVR-07/04
The programming interface is required for programming the microcontroller and for
calibrating the meter. Using the serial programming interface (SPI) of the AVR, it is
possible to access both Flash and EEPROM via the same connector. Complete
system programming and calibration can be performed at any time.
The pin layout of the programming interface is shown in the table below.
Table 3. Pin Layout of SPI Connector
Pin Name Direction Description
1 MISO Out Serial Data Out
2 VCC N/A Supply Voltage
3 SCK In Serial Clock
4 MOSI In Serial Data In
5 /RST In Device Reset
6 GND N/A Ground
The application uses the USART (Universal Synchronous/Asynchronous
Receiver/Transmitter) interface of the AVR to send measurement data. Any terminal
software can be used for sending commands to the meter and for receiving
measurement data. Measurement results, once calibrated, are given in units of watts,
volts and amperes.
The following table shows the pin layout of the asynchronous interface connector.
Table 4. Pin Layout of Serial Interface
Pin Name Direction Description
1 RxD In Asynchronous data in
2 TxD Out Asynchronous data out
3 GND N/A Ground
4 GND N/A Ground
Please note that the meter does not contain a level converter and the signals can
therefore not be directly connected to the serial port of a computer. The RS-232 spare
connector on STK500 contains a level converter and can therefore be used as a
buffer between the meter and the computer.
Asynchronous communication may introduce noise in measurement results,
especially when power supply is unable to provide the amount of current required and
when ground connections have not been properly laid out. By default, the power
supply is capable of delivering enough current for the meter to run, but not
necessarily enough to drive the USART interface, since it is typically not required
after calibration. In addition, the serial interface cable may deliver disturbances to the
meter.
Programming Interface
Serial Interface
Communication Noise
AVR465
11
2566A-AVR-07/04
As a summary, some default values for the hardware are shown in the table below.
Table 5. Hardware Defaults.
Parameter Value Note
System clock 4MHz External crystal by default
Maximum Current 10A External amplifier chops above 10A
Nominal Voltage 230V Varistor chops signals above 250V
Current Gain Ranges 1.25 / 10 / 69
Meter Firmware
The firmware is written in C language and compiles on IAR Embedded Workbench,
versions 3.10 and upward. Previous compiler versions do not support the ATmega88
and cannot be used as such. The language syntax is standard C, so the firmware
should be easy to port to other C compilers of choice.
The firmware is interrupt-driven, which means the main program consists of an
endless loop that is halted by interrupt requests on a regular basis. It is important that
the interrupt requests arrive regularly since they are used as the time base for
calculations. Variations in interrupt intervals will show in the accuracy of
measurement results.
Interrupt requests are generated by the ADC, which is driven by a prescaled system
clock.
The Analog-to-Digital Converter (ADC) is set to operate in Free Running Mode, which
means a new conversion will start immediately after the current conversion
completes. At the end of a conversion, the ADC raises an interrupt request and the
MCU starts executing the Interrupt Service Routine (ISR). The ISR reads, processes,
and accumulates sampled data and then returns execution to the main program.
The main program is mostly idle and is activated only when enough data has been
accumulated for the end-of-cycle calculations to start. Once started, the ISR may still
continue to interrupt the main program, even when it is busy calculating.
ADC interrupt requests occur at sampling rate, which is by default:
Equation 10. Default Sample Rate.
2403.85Hz
1664
4000000Hz
13 128
f f CLK
S = =
×
=
When the ISR is started its first task is to read sampled data from the ADC and store
it in a location in SRAM where it cannot be overwritten by new data. The first task to
perform on the data is then to remove any DC offset. This is carried out using a digital
High-Pass Filter (HPF) of type Infinite Impulse Response (IIR). The transfer function
of the filter can be written as follows:
Equation 11. High-Pass Filter, Infinite Impulse Response Type.
[ ] [ ] [ ] [ ] 1 - n x 0.996 - n x 0.996 1 n y 0.996 n y × × + ? × =
Summary: Defaults
Sampling & Timing
DC Offset Removal
12 AVR465
2566A-AVR-07/04
This is a short – and simple – digital filter, but it removes DC sufficiently well and has
a reasonable settling time. For a step change in input, the filter will settle to within 1%
of its final value in some 1200 samples. This means that after power-on the meter
should be allowed a few seconds to settle before measurements start. This is not a
problem when the meter is running, as there are no large step changes in DC level.
It should be noted that the filter is realized with a bit length higher than the input or
output data. If internal filter data is truncated, the filter will rather generate offset than
remove it. Hence, the filter implementation uses 32-bit data types (signed long) for
intermediate and output data.
Active power is defined as the power used by a device to produce useful work.
Mathematically it is the definite integral of voltage, u(t), times current, i(t), as follows:
Equation 12. Active Power Definition.
( ) ( ) ( ) ? cos I U t i t u
T
1 P
T O
× × ≡ × ≡ ∫ dt
Here U and I are the respective voltage and current Root-Mean-Square (RMS) values
and phi is the phase lag between current and voltage. The discrete time equivalent is:
Equation 13. Active Power Definition in Discrete Time.
( ) ( ) ∫
=
× ≡
1 - N
0 n
n i n u
N
1 P
Here u(n) and i(n) are the sampled instances of u(t) and i(t) and N is the number of
samples. Active power is calculated simply as the average of N voltage-current
products. It can be shown that this method is valid for both sinusoidal and distorted
waveforms.
The implementation uses 32-bit data types (signed long) for storing accumulated
data. The result is stored as a floating-point number. When the meter has been
calibrated, the active power results are in units of watts.
Most energy meters are required to provide a pulse, which frequency is proportional
to the amount of active power measured. The pulse typically drives an LED, which is
used for calibration or verification purposes. The frequency of the energy pulse, EP,
and the amount of active power measured are related as follows:
Equation 14. Frequency of Energy Pulse Output.
000 600 3
MC P f
×
=
Here P is active power in watts and MC is the meter constant in pulses/kilowatt-hour.
The meter constant is part of the calibration coefficient set, and can be set to any
integer value, ranging from one to 65535. Taking into account the system clock, fCLK,
and the timer/counter prescaler, PS, the energy pulse interval is derived as follows:
Active Power
Measurement
Energy Pulse Output
AVR465
13
2566A-AVR-07/04
Equation 15. Interval of Energy Pulse Output.
MC P PS
000 600 3 f T CLK
× ×
×
=
It should be noted that after the meter constant has been set there is only one
variable; active power, P. In order to save instruction cycles, the expression is
reduced to T = N/P, where N is a pre-calculated, constant numerator. The numerator
is calculated during firmware initialization, when MC is read from EEPROM. The
default numerator is as follows:
Equation 16. Constant Numerator at Default Meter Constant.
250 406 1
000 10 024 1
000 600 3 000 000 4
MC PS
000 600 3 f N CLK =
×
×
=
×
×
=
After the required pulse interval has been calculated the timer/counter (T/C) is
programmed to produce a pulse output with an interval as close as possible to the
one derived. The 16-bit T/C is clocked by a prescaled system clock. The prescaler is
chosen such that even the smallest pulse intervals can be accurately represented.
The problem is to also be able to accurately produce pulses with very long intervals.
For this purpose, the T/C is extended with a firmware counter.
The regular T/C mechanism is illustrated in Figure 6. In this mode of operation the
energy pulse is set on each compare match.
Figure 6. Basic Timer/Counter Operation.
A compare match with OCR1A sets the OC1A output and a compare match with
OCR1B clears the same. The pulse interval, tEP, is controlled by OCR1A and the
pulse width, tEPH, by OCR1B.
When the calculated energy pulse interval exceeds 16 bits the firmware extended, 16-
bit counter (TC1ext) is activated. The firmware counter value is increased each time
the hardware counter reaches 16-bit maximum, effectively making the extended
counter 32 bits in length. The extended counter mechanism is illustrated in Figure 7.
Regular Mode T/C
Extended Mode T/C
OCR1A
OCR1B
TCNT1
OC1A
(EP)
tEPH tEP
14 AVR465
2566A-AVR-07/04
Figure 7. Extended Timer/Counter Operation.
TC1extREM
OCR1B
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -