📄 emeter_3ph_foreground.c
字号:
for(j=1;j<2;j++)
#else
for(j=1;j<4;j++)
#endif
{
if(j==1)
{
i_ptr=(int*) &CH1_FLASH_INFO_FIR_PTR;
replace_flash_word(i_ptr,chan1_int_params[FIR_POINTER]);
l_ptr =&CH1_FLASH_INFO_E_accum_threshold;
replace_flash_long(l_ptr,chan1_long_params[E_accum_threshold]);
l_ptr =&CH1_FLASH_INFO_POWER_SCALE_FACTOR;
replace_flash_long(l_ptr,chan1_long_params[POWER_SCALE_FACTOR]);
}
if(j==2)
{
i_ptr=(int*) &CH2_FLASH_INFO_FIR_PTR;
replace_flash_word(i_ptr,chan2_int_params[FIR_POINTER]);
l_ptr =&CH2_FLASH_INFO_E_accum_threshold;
replace_flash_long(l_ptr,chan2_long_params[E_accum_threshold]);
l_ptr =&CH2_FLASH_INFO_POWER_SCALE_FACTOR;
replace_flash_long(l_ptr,chan2_long_params[POWER_SCALE_FACTOR]);
}
if(j==3)
{
i_ptr=(int*) &CH3_FLASH_INFO_FIR_PTR;
replace_flash_word(i_ptr,chan3_int_params[FIR_POINTER]);
l_ptr =&CH3_FLASH_INFO_E_accum_threshold;
replace_flash_long(l_ptr,chan3_long_params[E_accum_threshold]);
l_ptr =&CH3_FLASH_INFO_POWER_SCALE_FACTOR;
replace_flash_long(l_ptr,chan3_long_params[POWER_SCALE_FACTOR]);
}
}
#endif
#if STORE_TEMP_COEFF
i_ptr=&FLASH_INFO_average_temperature;
Flash_ww(i_ptr,average_temperature);
i_ptr=&FLASH_INFO_average_VeREF_range;
Flash_ww(i_ptr,average_VeREF_range);
Flash_secure();
#endif
#endif
_EINT();
//use simulated power factor of 30 degrees lag
sinne_2-=0x1555; //add 30 degree phase lag to current to simulate non
// unity power factor = 30/360 *65536
j=k=l=0;
master_stability_counter=60;
display_stage=0;
display_phase=1;
do
{
//--------------------------------------------------------------------------
// CHANNEL ONE
//--------------------------------------------------------------------------
//wait in loop until we have logged a predefined number of cycles
if(chan1_int_params[channel_status]&(NEW_P_LOG+NEW_I_LOG+NEW_V_LOG))
{
//this flags indicates an X number of cycles of voltage signals as been
// added, the sum is divided by the number of samples to extract the
// DC bias level
if(chan1_int_params[channel_status]&NEW_V_LOG)
{
i=chan1_long_params[logged_V_accum]/chan1_int_params[logged_V_count];
chan1_int_params[V_bias]=(i+chan1_int_params[V_bias])/2;
chan1_int_params[channel_status]&=~NEW_V_LOG;
average_VeREF_range=(average_VeREF_range+(instant_VeREF_PLUS-instant_VeREF_MINUS))/2;
//DISPLAY(chan1_int_params[V_bias],0);
}
//this flags indicates an X number of cycles of current signals as been
// added, the sum is divided by the number of samples to extract the
// DC bias level
#if 1
if(chan1_int_params[channel_status]&NEW_I_LOG)
{
i=chan1_long_params[logged_I_accum]/chan1_int_params[logged_I_count];
chan1_int_params[I_bias]=(chan1_int_params[I_bias]+i)/2;
chan1_int_params[channel_status]&=~NEW_I_LOG;
// DISPLAY(chan1_int_params[I_bias],0);
// toggle^=1;
// DISPLAY_1DIGIT(toggle,7);
#if 1
//only deals with current switching after bias has become stable
if(!master_stability_counter)
{
if(chan1_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
{ //need to turn to high gain
// DISPLAY_1DIGIT(0,6);
if(ADC12MCTL1&SREF_1) //if it is currently in low gain
{
ADC12CTL0 &=~ ENC; //temp stop ADC
ADC12MCTL1&=~(SREF_7); //set value to higher gain, SREF_7 is mask
ADC12MCTL1|=SREF_6; //by using the lower voltage external reference
ADC12CTL0 |= ENC; //start ADC again
chan1_int_params[channel_status]|=CURRENT_HIGH_GAIN;
chan1_long_params[E_accum_threshold]=chan1_high_gain_threshold;
master_stability_counter=40; //recalculate security
}
P1OUT|=BIT0;
}//EOF if(chan1_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
if(chan1_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
{
// DISPLAY_1DIGIT(1,6);
if(!(ADC12MCTL1&SREF_1))
{
ADC12CTL0 &=~ ENC; //temp stop ADC
ADC12MCTL1&=~(SREF_7); //set value to low gain, SREF_7 is mask
ADC12MCTL1|=SREF_1; //by using 2.7Vref and AGND
ADC12CTL0 |= ENC; //start ADC again
chan1_int_params[channel_status]&=~CURRENT_HIGH_GAIN;
master_stability_counter=40; //recalculate security
//we are changing from high gain to low gain
//then the threshold need to be reduced
}
power_gain = (float) chan1_high_gain_threshold;
power_gain=power_gain*(float) average_VeREF_range;
power_gain = power_gain/4096;
chan1_long_params[E_accum_threshold]= (long)power_gain;
P1OUT&=~BIT0;
}//EOF if(chan1_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
}//EOF if(!master_stability_counter)
#endif
chan1_int_params[logged_I_above_threshold_counter]=0;
} //EOF if(chan1_int_params[channel_status]&NEW_I_LOG)
#endif
//this flags indicates an X number of cycles of power product samples as been
// added, the sum is divided by the number of samples to get an average
// power level, the results are then converted to WATTS using the
// correct scaling factors. In the dispaly routine, the results in hex
// is first converted to BCD before being output to the LCD module
if(chan1_int_params[channel_status]&NEW_P_LOG)
{
if(master_stability_counter) --master_stability_counter;
chan1_total_power+=chan1_long_params[logged_P_accum]/chan1_int_params[logged_P_count];
chan1_shifted_total_power+=chan1_long_params[logged_PS_P_accum]/chan1_int_params[logged_PS_P_count];
chan1_int_params[channel_status]&=~NEW_P_LOG;
if(++j>=8)
{
chan1_total_power/=8;
chan1_shifted_total_power/=8;
chan1_ratio = (double) chan1_shifted_total_power/chan1_total_power;
if(chan1_total_power<0)
chan1_total_power=-chan1_total_power;
f_ptr= (float*) &chan1_long_params[POWER_SCALE_FACTOR];
power_gain=*f_ptr;
if(!(chan1_int_params[channel_status]&CURRENT_HIGH_GAIN))
{
power_gain=power_gain * 4096;
power_gain=power_gain / (float) average_VeREF_range;
}
chan1_power=power_conversion(chan1_total_power, power_gain);
// DISPLAY(chan1_power,0);
// DISPLAY_1DIGIT(D_a,6);
// DISPLAY_1DIGIT(D_P,5);
// LCDM13|=0x80; //turn cursor 1 on to indicate phase 1
chan1_total_power=0;
chan1_shifted_total_power=0;
j=0;
}
}
}
//--------------------------------------------------------------------------
// CHANNEL TWO
//--------------------------------------------------------------------------
#if !SINGLE_PHASE
//wait in loop until we have logged a predefined number of cycles
if(chan2_int_params[channel_status]&(NEW_P_LOG+NEW_I_LOG+NEW_V_LOG))
{
//this flags indicates an X number of cycles of voltage signals as been
// added, the sum is divided by the number of samples to extract the
// DC bias level
if(chan2_int_params[channel_status]&NEW_V_LOG)
{
i=chan2_long_params[logged_V_accum]/chan2_int_params[logged_V_count];
chan2_int_params[V_bias]=(i+chan2_int_params[V_bias])/2;
chan2_int_params[channel_status]&=~NEW_V_LOG;
}
//this flags indicates an X number of cycles of current signals as been
// added, the sum is divided by the number of samples to extract the
// DC bias level
#if 1
if(chan2_int_params[channel_status]&NEW_I_LOG)
{
i=chan2_long_params[logged_I_accum]/chan2_int_params[logged_I_count];
chan2_int_params[I_bias]=(chan2_int_params[I_bias]+i)/2;
chan2_int_params[channel_status]&=~NEW_I_LOG;
// DISPLAY(chan2_int_params[logged_I_above_threshold_counter],0);
// toggle^=1;
// DISPLAY_1DIGIT(toggle,7);
#if 1
//only deals with current switching after bias has become stable
if(!master_stability_counter)
{
if(chan2_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
{ //need to turn to high gain
if(ADC12MCTL4&SREF_1) //if it is currently in low gain
{
ADC12CTL0 &=~ ENC; //temp stop ADC
ADC12MCTL4&=~(SREF_7); //set value to higher gain, SREF_7 is mask
ADC12MCTL4|=SREF_6; //by using the lower voltage external reference
ADC12CTL0 |= ENC; //start ADC again
chan2_int_params[channel_status]|=CURRENT_HIGH_GAIN;
chan2_long_params[E_accum_threshold]=chan2_high_gain_threshold;
master_stability_counter=40; //recalculate security
}
}//EOF if(chan2_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
if(chan2_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
{
if(!(ADC12MCTL4&SREF_1))
{
ADC12CTL0 &=~ ENC; //temp stop ADC
ADC12MCTL4&=~(SREF_7); //set value to low gain, SREF_7 is mask
ADC12MCTL4|=SREF_1; //by using 2.7Vref and AGND
ADC12CTL0 |= ENC; //start ADC again
chan2_int_params[channel_status]&=~CURRENT_HIGH_GAIN;
master_stability_counter=40; //recalculate security
//we are changing from high gain to low gain
//then the threshold need to be reduced
}
power_gain = (float) chan2_high_gain_threshold;
power_gain=power_gain*(float) average_VeREF_range;
power_gain = power_gain/4096;
chan2_long_params[E_accum_threshold]= (long)power_gain;
}//EOF if(chan2_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
}//EOF if(!master_stability_counter)
#endif
chan2_int_params[logged_I_above_threshold_counter]=0;
} //EOF if(chan2_int_params[channel_status]&NEW_I_LOG)
#endif
//this flags indicates an X number of cycles of power product samples as been
// added, the sum is divided by the number of samples to get an average
// power level, the results are then converted to WATTS using the
// correct scaling factors. In the dispaly routine, the results in hex
// is first converted to BCD before being output to the LCD module
if(chan2_int_params[channel_status]&NEW_P_LOG)
{
chan2_total_power+=chan2_long_params[logged_P_accum]/chan2_int_params[logged_P_count];
chan2_shifted_total_power+=chan2_long_params[logged_PS_P_accum]/chan2_int_params[logged_PS_P_count];
chan2_int_params[channel_status]&=~NEW_P_LOG;
if(++k>=8)
{
chan2_total_power/=8;
chan2_shifted_total_power/=8;
chan2_ratio = (double) chan2_shifted_total_power/chan2_total_power;
if(chan2_total_power<0)
chan2_total_power=-chan2_total_power;
//the scale power is stored as a two word
//float value in a "long array"
//so have to use this means
//to convert it to float without changing its value
f_ptr= (float*) &chan2_long_params[POWER_SCALE_FACTOR];
power_gain=*f_ptr;
if(!(chan2_int_params[channel_status]&CURRENT_HIGH_GAIN))
{
power_gain=power_gain * 4096;
power_gain=power_gain / (float) average_VeREF_range;
}
chan2_power=power_conversion(chan2_total_power,power_gain);
// DISPLAY(chan2_power,0);
// DISPLAY_1DIGIT(D_a,6);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -