📄 emeter_dcbias.c
字号:
//**************************************************************************
// This program extracts the DC bias from an input signal and returns the AC signal
// It almost accumulates multiple samples and autodetect when a predeterminted
// number of complete cycles have been accumulated and the number of samples counted
// during these number of complete cycles. These information is logged and used
// by the main routine for further processing.
//
// Vincent Chan
// Texas Instruments Hong Kong Ltd
// Date Comments
// =====================
// 01/09/19 Code Starts
// 01/10/5 module more or less working.
// 01/11/27 Now V zero crossings also does I accumlation. This overcomes the problem
// of switching range when I may not trigger with the old DC bias.
//**************************************************************************
#include "emeter_3phase.h"
//--------------------------------------------------------------------------
extern long logged_shifted_p_accum,shifted_p_accum;
extern int logged_shifted_p_count,shifted_p_count,shifted_samples_read_index;
void extract_dc_bias(long* channel_long_params, int* channel_int_params, int* voltage, int* current)
{
//Accumulate input value.
//Accumulate current and voltage values (used for DC extraction)
channel_long_params[V_accum] += *voltage;
++channel_int_params[V_count];
channel_long_params[I_accum] += *current;
++channel_int_params[I_count];
//extract the average DC bias level so far
//this will turn the signal to a signed singal with zero DC
*voltage-=channel_int_params[V_bias];
*current-=channel_int_params[I_bias];
//do zero crossing for voltage (cycle detection)
if(*voltage<0)
{
channel_int_params[channel_status]&=~V_POS; //log the sign of the signal
}
else
{ //detect start of a new cycle
//by detection NEG->POS transition
if(!(channel_int_params[channel_status]&V_POS))
{ //if so see if 64 cycles has been recorded
if(++channel_int_params[V_cycle_counter]>=64)
{ //once 64 cycles has been recorde
//logged the accumulated values for V and P
channel_long_params[logged_V_accum]=channel_long_params[V_accum];
channel_int_params[logged_V_count]=channel_int_params[V_count];
//tell foreground that there are things to process
channel_int_params[channel_status]|=NEW_V_LOG;
channel_long_params[V_accum]=0; //reset the accum. and count registers
channel_int_params[V_count]=0;
channel_int_params[V_cycle_counter]=0;
}
if(++channel_int_params[P_cycle_counter]>=32)
{ //once 32 cycles has been recorded
//logged the accumulated values for V and P
channel_long_params[logged_P_accum]=channel_long_params[P_accum];
channel_int_params[logged_P_count]=channel_int_params[P_count];
channel_long_params[logged_PS_P_accum]=channel_long_params[PS_P_accum];
channel_int_params[logged_PS_P_count]=channel_int_params[PS_P_count];
//tell foreground that there are things to process
channel_int_params[channel_status]|=NEW_P_LOG;
channel_long_params[P_accum]=0; //reset the accum. and count registers
channel_int_params[P_count]=0;
channel_long_params[PS_P_accum]=0;
channel_int_params[PS_P_count]=0;
channel_int_params[P_cycle_counter]=0;
}
if(++channel_int_params[I_cycle_counter]>=64)
{
channel_long_params[logged_I_accum]=channel_long_params[I_accum];
channel_int_params[logged_I_count]=channel_int_params[I_count];
channel_int_params[logged_I_above_threshold_counter]=
channel_int_params[I_above_threshold_counter];
channel_int_params[channel_status]|=NEW_I_LOG;
channel_long_params[I_accum]=0;
channel_int_params[I_count]=0;
channel_int_params[I_cycle_counter]=0;
channel_int_params[I_above_threshold_counter]=0;
}
}
channel_int_params[channel_status]|=V_POS; //log the sign of the signal
}
if(*current<0) //same detection is done for current signal
{
channel_int_params[channel_status]&=~I_POS;
}
else
{
// if(!(channel_int_params[channel_status]&I_POS))
// {
//moved to Voltage zero crossing detect
// }
channel_int_params[channel_status]|=I_POS;
if(*current>I_SWITCHING_THRESHOLD)
{
++channel_int_params[I_above_threshold_counter];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -