📄 emeter_3ph_background.c
字号:
//get rid of CT delay for the current
I_sample=FIR(chan2_int_params,I_sample);
//store into buffers
current_buffer_chan2[samples_write_index]=I_sample;
voltage_buffer_chan2[samples_write_index]=V_sample;
//if(samples_write_index>=SAMPLE_BUFFER_SIZE)
// samples_write_index=0;
//****************************************************
// Now comes the reading part, both current and voltage is
// out and the power is calculated and put is power_sample.
// Power_sample is accumulated and averaged to work out the average power
// It also accumulated and used to generate the energy count.
//****************************************************
V_sample=voltage_buffer_chan2[samples_read_index];
I_sample=current_buffer_chan2[samples_read_index];
power_sample=SIGNED_MUL(V_sample,I_sample);
chan2_long_params[P_accum]+=power_sample;
++chan2_int_params[P_count];
power_sample>>=4; //need scaling down
//power_sample>>=7;
chan2_long_params[E_accum]+=power_sample;
if(chan2_long_params[E_accum]>=chan2_long_params[E_accum_threshold])
{
TAR_reading=TAR;
chan2_int_params[ENERGY_PERIOD]=TAR_reading;
chan2_int_params[ENERGY_PERIOD]-=chan2_int_params[LAST_TAR];
chan2_int_params[LAST_TAR]=TAR_reading;
chan2_long_params[E_accum]-=chan2_long_params[E_accum_threshold];
++chan2_long_params[E_accum_count];
chan2_int_params[channel_status]|=E_TICK;
P3OUT&=~CHAN2_PULSE;
power_led_length_count=0;
}
//******************************************************
// A second power is calculated based on the current sample
// phase lagged by 60.4 degrees or 11 samples
// each sample = (50/3276.8)*360 = 5.4931640625 degree
// delay by 11 samples or 60.4 degree
// This phase lagged sample is multiplied by the none phase shifted
// voltage sample, the result power sample is used to calculate
// the relative phase shift between the incoming current and voltage
// signal. It is also used to self compensate for the CT lag
// The resultant power is stored in a separate buffer and then averaged
//*******************************************************
i=samples_read_index;
i-=11;
i&=0x3f; //circular buffer adjust, buffer size must be
//64!!!!
I_sample=current_buffer_chan2[i];
chan2_long_params[PS_P_accum]+=SIGNED_MUL(V_sample,I_sample);
++chan2_int_params[PS_P_count];
//--------------------------------------------------------------------------
// CHANNEL THREE
//--------------------------------------------------------------------------
#if SIMULATED_INPUT
//****************************************************
//generate signal for use in testing, assume 50Hz and sampling freq of 3276.8
// 65.536 samples/cycle
//****************************************************
//use previously simulated results
I_sample=simulated_current;
V_sample=simulated_voltage;
#else
//****************************************************
// Read ADC results for Channel 1 voltage and current
//****************************************************
//read current value
I_sample=ADC12MEM[7];
//voltage is sampled twice both before and after current
//so need to average to find out the value in the middle
V_sample=(ADC12MEM[6]+ADC12MEM[8])>>1;
#endif
//****************************************************
// Take DC bias away from voltage and current samples
// Take CT lag away from current sample
// Store in Chan1 buffer
//****************************************************
//get rid of the bias and calculate overall bias
extract_dc_bias(&chan3_long_params[0],&chan3_int_params[0],&V_sample,&I_sample);
//get rid of CT delay for the current
I_sample=FIR(chan3_int_params,I_sample);
//store into buffers
current_buffer_chan3[samples_write_index]=I_sample;
voltage_buffer_chan3[samples_write_index]=V_sample;
//if(samples_write_index>=SAMPLE_BUFFER_SIZE)
// samples_write_index=0;
//****************************************************
// Now comes the reading part, both current and voltage is
// out and the power is calculated and put is power_sample.
// Power_sample is accumulated and averaged to work out the average power
// It also accumulated and used to generate the energy count.
//****************************************************
V_sample=voltage_buffer_chan3[samples_read_index];
I_sample=current_buffer_chan3[samples_read_index];
power_sample=SIGNED_MUL(V_sample,I_sample);
chan3_long_params[P_accum]+=power_sample;
++chan3_int_params[P_count];
power_sample>>=4; //need scaling down
//power_sample>>=7;
chan3_long_params[E_accum]+=power_sample;
if(chan3_long_params[E_accum]>=chan3_long_params[E_accum_threshold])
{
TAR_reading=TAR;
chan3_int_params[ENERGY_PERIOD]=TAR_reading;
chan3_int_params[ENERGY_PERIOD]-=chan3_int_params[LAST_TAR];
chan3_int_params[LAST_TAR]=TAR_reading;
chan3_long_params[E_accum]-=chan3_long_params[E_accum_threshold];
++chan3_long_params[E_accum_count];
chan3_int_params[channel_status]|=E_TICK;
P3OUT&=~CHAN3_PULSE;
power_led_length_count=0;
}
//******************************************************
// A second power is calculated based on the current sample
// phase lagged by 60.4 degrees or 11 samples
// each sample = (50/3276.8)*360 = 5.4931640625 degree
// delay by 11 samples or 60.4 degree
// This phase lagged sample is multiplied by the none phase shifted
// voltage sample, the result power sample is used to calculate
// the relative phase shift between the incoming current and voltage
// signal. It is also used to self compensate for the CT lag
// The resultant power is stored in a separate buffer and then averaged
//*******************************************************
i=samples_read_index;
i-=11;
i&=0x3f; //circular buffer adjust, buffer size must be
//64!!!!
I_sample=current_buffer_chan3[i];
chan3_long_params[PS_P_accum]+=SIGNED_MUL(V_sample,I_sample);
++chan3_int_params[PS_P_count];
//--------------------------------------------------------------------------
//************************************************************
// If LED has been led then need to time it and switch it off
//************************************************************
if(++power_led_length_count>=1000)
{
power_led_length_count=0;
P3OUT|=(CHAN1_PULSE+CHAN2_PULSE+CHAN3_PULSE);
}
//************************************************************
// Manage the circular buffer read and write pointers here
// The buffer is 64 long
//************************************************************
++samples_read_index;
++samples_write_index;
samples_write_index&=0x3f;
samples_read_index&=0x3f;
instant_temperature=ADC12MEM[9];
instant_VeREF_PLUS=ADC12MEM[10];
instant_VeREF_MINUS=ADC12MEM[11];
//*****************
// Time Stampe
//P1OUT&=~BIT0;
//*****************
}
extern int ticker,seconds,minutes,hours,days;
interrupt [BASICTIMER_VECTOR] void TWO_SECOND_TICKER(void)
{
ticker=1;
seconds+=2;
if(seconds>=60)
{
seconds=0;
if(++minutes>=60)
{
minutes=0;
if(++hours>=24)
{
hours=0;
if(++days>=7)
{
days=0;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -