AVR1631 - Energy Meter Reference Design with ATxmega32A4  Rev 1.0
 All Data Structures Files Functions Variables Typedefs Macros
meter_calculations.c File Reference

Metering Algorithm implementation. More...

#include "meter.h"

Go to the source code of this file.

Functions

 ISR (TCC1_OVF_vect)
 Timer/Counter 1 interrupt occur in every 250 uSec, (4KHz). The ISR routine enables ADC Conversion start on channel 0. This function also perform the pulsing of the led for meter constant of 3200 impulse/kwh.
 ISR (ADCA_CH0_vect)
 ADC ISR channel 0 interrupt reads the phase value from shunt for Irms and energy calculation.
 ISR (ADCA_CH1_vect)
 ADC ISR channel 1 interrupt reads the voltage value for Vrms and energy calculation The ADC ISR also check for the sign change in voltage signal for frequency calucaulation.
 ISR (ADCA_CH2_vect)
 ADC ISR channel 2 interrupt reads the neutral value from CT and accumulate it for neutal current calculation.
int16_t lp_filter (int16_t *inp_array)
void calculate (void)
void calculate_freq (void)
void get_frequency (void)
void meter_flush ()

Variables

uint8_t gain_stage = 0
 variables used in metering calculations
uint32_t freq_sum = 0
uint16_t frequency = 0
uint16_t max_demand = 0
uint16_t Vrms [5] = {0}
uint16_t Irms [5] = {0}
uint16_t Nrms = 0
int16_t p_mean = 0
int16_t Vmean = 0
int16_t n_mean = 0
int32_t phase_mean = 0
int32_t neutral_mean = 0
int32_t volt_mean = 0
uint64_t phase_sum = 0
uint64_t volt_sum = 0
uint64_t neutral_sum = 0
int64_t watts_sum = 0
int64_t watts_sum_neutral = 0
int64_t watts_sum_calib = 0
uint16_t adc_samples = 0
int32_t active_energy_signed = 0
int32_t neutral_power = 0
uint16_t active_power [5] = {0}
uint16_t apparent_power = 0
int16_t power_factor
int16_t offset [7] = {0}
int32_t volt_temp
int32_t ct_temp
int32_t shunt_temp
volatile int16_t adc_read_ch0
volatile int16_t adc_read_ch1
int16_t lp_phase [LP_ORDER] = {0}
int16_t lp_power [LP_ORDER] = {0}
int16_t lp_volt [LP_ORDER] = {0}
uint8_t freq_cnt
uint8_t offset_cnt = 0
const int16_t lp_coeff [LP_ORDER] = {5252, 5568, 5728, 5728, 5568, 5252}
float pulse_energy_2_5ms = 0
uint8_t pulse_count = 0
uint8_t On_Time_counter = 0
float pulse_energy_acc = 0

Detailed Description

Metering Algorithm implementation.

Application note:
AVR1631: Energy Meter Reference Design with ATxmega32A4
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author
Atmel Corporation: http://www.atmel.com
Support email: avr@a.nosp@m.tmel.nosp@m..com

$Revision: 1.0
$Date: 2012-07-01 10:10:10 +0530

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  1. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  1. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file meter_calculations.c.

Function Documentation

void calculate ( void  )

brief This function is called on every 1sec and perform the calculation of metering paramters

Parameters
Irmscontains the root mean square value of current on phase line
Vrmscontains the root mean square value of voltage
Nrmscontains the root mean square value of current on neutral line
apparent_powerholds the apparent energy (VA)
active_powerholds the active energy (Watts)
power_factorhols the power factor(PF)

Definition at line 233 of file meter_calculations.c.

void calculate_freq ( void  )

brief This function calculate line frequency from the value

Parameters
frequencyhold the line frequency.
freq_avghold the average timer value.

Definition at line 396 of file meter_calculations.c.

void get_frequency ( void  )

function check for the sign change in voltage signal for frequency calucaulation.

Parameters
sign_flagupdated when there is a sign change
sign_flag = 0, during the negative half cycle
sign_flag = 1, during the positive half cycle
freq_valhold the timer value of one sine wave
freq_sumhold the accumulated timer counter for frequency calculation
freq_cnthold the number of sine waves taken for frequency measument

Definition at line 420 of file meter_calculations.c.

ISR ( TCC1_OVF_vect  )

Timer/Counter 1 interrupt occur in every 250 uSec, (4KHz). The ISR routine enables ADC Conversion start on channel 0. This function also perform the pulsing of the led for meter constant of 3200 impulse/kwh.

Parameters
pulse_energy_2_5mshold the active energy for 2.5msec
pulse_energy_acchold the accumulated energy on every 2.5msec
On_Time_counterhold the duration for which the pulse led to be ON

Definition at line 85 of file meter_calculations.c.

ISR ( ADCA_CH0_vect  )

ADC ISR channel 0 interrupt reads the phase value from shunt for Irms and energy calculation.

Parameters
adc_read_ch0hold the ADC channel result result register

Definition at line 120 of file meter_calculations.c.

ISR ( ADCA_CH1_vect  )

ADC ISR channel 1 interrupt reads the voltage value for Vrms and energy calculation The ADC ISR also check for the sign change in voltage signal for frequency calucaulation.

Parameters
adc_read_ch1hold the ADC channel result register

Definition at line 133 of file meter_calculations.c.

ISR ( ADCA_CH2_vect  )

ADC ISR channel 2 interrupt reads the neutral value from CT and accumulate it for neutal current calculation.

Parameters
p_meansubtract the DC offset value form the ADC_result register.
phase_sumcontains the accumulated phase value
v_meansubstract the DC offset value form the ADC_result register.
volt_sumcontains the accumulated volt value
n_meansubstract the DC offset value form the ADC_result register.
neutral_sumcontains the accumulated neutral value
watts_sumcontain the accumulated power value of active power calculation
lp_phasearray contain the phase signal for low pass filtering
lp_voltarray contain the volt signal for low pass filtering
lp_powerarray contain the instantaneous power signal for low pass filtering

Definition at line 155 of file meter_calculations.c.

int16_t lp_filter ( int16_t *  inp_array)

brief This function performs the low pass filtering of the input array

Parameters
lp_coeffcontains the coefficients for the low pass filtering
lp_outputreturn the filtered value

Definition at line 208 of file meter_calculations.c.

void meter_flush ( void  )

brief This function clear all the varilable used in for energy calculation

Definition at line 447 of file meter_calculations.c.