⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emeter_3ph_foreground.c

📁 基于MSPF449的三相电压表功率的开发程序
💻 C
📖 第 1 页 / 共 3 页
字号:
//        DISPLAY_1DIGIT(D_P,5);
//        LCDM14|=0x80; 

//       DISPLAY(chan2_power,0);        
        chan2_total_power=0;
        chan2_shifted_total_power=0;
        k=0;
        }
      }
    }
   #endif 
//--------------------------------------------------------------------------
//  CHANNEL THREE
//--------------------------------------------------------------------------
  #if !SINGLE_PHASE
                      //wait in loop until we have logged a predefined number of cycles
  if(chan3_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(chan3_int_params[channel_status]&NEW_V_LOG)
      {
      i=chan3_long_params[logged_V_accum]/chan3_int_params[logged_V_count];
      chan3_int_params[V_bias]=(i+chan3_int_params[V_bias])/2;
      chan3_int_params[channel_status]&=~NEW_V_LOG;
      //DISPLAY(chan3_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(chan3_int_params[channel_status]&NEW_I_LOG)
      {
      i=chan3_long_params[logged_I_accum]/chan3_int_params[logged_I_count]; 
      chan3_int_params[I_bias]=(chan3_int_params[I_bias]+i)/2;  
      chan3_int_params[channel_status]&=~NEW_I_LOG;
      
//      DISPLAY(chan3_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(chan3_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
          {                             //need to turn to high gain
          if(ADC12MCTL7&SREF_1)         //if it is currently in low gain
            {
            ADC12CTL0 &=~ ENC;          //temp stop ADC
            ADC12MCTL7&=~(SREF_7);        //set value to higher gain, SREF_7 is mask
            ADC12MCTL7|=SREF_6;         //by using the lower voltage external reference
            ADC12CTL0 |=  ENC;          //start ADC again
            chan3_int_params[channel_status]|=CURRENT_HIGH_GAIN;
            chan3_long_params[E_accum_threshold]=chan3_high_gain_threshold;
                        
            master_stability_counter=40;  //recalculate security
            }

          }//EOF if(chan3_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
        if(chan3_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
          {          
          if(!(ADC12MCTL7&SREF_1))
            {
            ADC12CTL0 &=~ ENC;          //temp stop ADC            
            ADC12MCTL7&=~(SREF_7);        //set value to low gain, SREF_7 is mask
            ADC12MCTL7|=SREF_1;         //by using 2.7Vref and AGND
            ADC12CTL0 |=  ENC;          //start ADC again                        
            chan3_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) chan3_high_gain_threshold;            
          power_gain=power_gain*(float) average_VeREF_range;
          power_gain = power_gain/4096;
          chan3_long_params[E_accum_threshold]= (long)power_gain;              
          P1OUT|=BIT0;            
          }//EOF if(chan3_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
        }//EOF if(!master_stability_counter)
      #endif
      chan3_int_params[logged_I_above_threshold_counter]=0;          
      } //EOF if(chan3_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(chan3_int_params[channel_status]&NEW_P_LOG)
      {
      chan3_total_power+=chan3_long_params[logged_P_accum]/chan3_int_params[logged_P_count];
      chan3_shifted_total_power+=chan3_long_params[logged_PS_P_accum]/chan3_int_params[logged_PS_P_count];
      chan3_int_params[channel_status]&=~NEW_P_LOG;
      if(++l>=8)
        {
        chan3_total_power/=8;
        chan3_shifted_total_power/=8;
        chan3_ratio = (double) chan3_shifted_total_power/chan3_total_power;
        if(chan3_total_power<0)
          chan3_total_power=-chan3_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*) &chan3_long_params[POWER_SCALE_FACTOR];           
        power_gain=*f_ptr;
        if(!(chan3_int_params[channel_status]&CURRENT_HIGH_GAIN))
          {  
          power_gain=power_gain * 4096;     
          power_gain=power_gain / (float) average_VeREF_range; 
          }
        chan3_power=power_conversion(chan3_total_power,power_gain); 
        chan3_total_power=0;
        chan3_shifted_total_power=0;
        l=0;
        }
      }
    }
    #endif
 //---------------------------------------------------------------------------------------   
 
//-----------------------------------------------------------------------------------------
//Do display and other house keeping here
//-----------------------------------------------------------------------------------------
  if(ticker)                      //wait for 2 second tick
//if(0)
    {
    ticker=0;
    if(display_phase==1)          //cycle through the phases
      {
      display_readings(chan1_power, chan1_ratio);
      LCDM13|=0x80;                //turn cursor 1 on to indicate phase 1      
      }
    else if(display_phase==2)
      {
      display_readings(chan2_power, chan2_ratio); 
      LCDM14|=0x80;                //turn cursor 2 on to indicate phase 1             
      }
    else
      {
      display_readings(chan3_power, chan3_ratio);
      LCDM15|=0x80;                //turn cursor 3 on to indicate phase 1            
      }
     
    if(++display_stage>3) 
      {
      display_stage=0;
    #if !SINGLE_PHASE       
      if(++display_phase>3)
        display_phase=1;
    #endif         
      }
       
//each timer tick re-calculate the temp and Vexternal range      
    average_temperature=(average_temperature+instant_temperature)/2;
//    average_VeREF_range=(average_VeREF_range+(instant_VeREF_PLUS-instant_VeREF_MINUS))/2;             
//    if(master_stability_counter) --master_stability_counter;
    }
  }while(1);

}

//This program alternately display the 4 subreadings associated with measuring power
void display_readings(int power, double ratio)
{
double dd;
int i;

DISPLAY_1DIGIT(D_CLEAR,7);
DISPLAY_1DIGIT(D_CLEAR,6);
DISPLAY_1DIGIT(D_CLEAR,5);

if(display_stage==0)                      //display power factor
  {
  dd=calc_phase_shift(ratio);             //calculate phase shift from ratio
  dd=cos(dd);
  dd*=1000;                              //display power factor to 3 digits
  dd+=0.5;
  i = (int) dd;
  DISPLAY(i,0);
  DISPLAY_1DIGIT(D_P,6);
  DISPLAY_1DIGIT(0xf,5);
  LCDM12|=0x80;                          //decimal point
  }
else if(display_stage==1)                //display phase shift
  {
  dd=calc_phase_shift(ratio);
  dd=(dd/ 3.14159265359) * 180;          //display signed phase shift in degree
  dd*=10;
  dd+=0.5;
  i = (int) dd;
  if(i<0)
    {
    i=-i;
    DISPLAY(D_MINUS,7);
    }
  DISPLAY(i,0);
  DISPLAY_1DIGIT(D_P,6);
  DISPLAY_1DIGIT(D_S,5);
  LCDM10|=0x80;                          //decimal point    
  }
else if(display_stage==2)                //display active power
  {
  DISPLAY(power,0);
  DISPLAY_1DIGIT(D_a,6);
  DISPLAY_1DIGIT(D_P,5);
  LCDM10|=0x80;                          //decimal point  
  }
else if(display_stage==3)                //display reactive power
  {
  dd = (double) power;
                                         //reactive power = active power * tan(phase shift)
  dd = dd*tan(calc_phase_shift(ratio));
  if(dd<0) 
    {
    dd=-dd;
    DISPLAY(D_MINUS,7);
    }
  i=(int) dd;
  DISPLAY(i,0);
  DISPLAY_1DIGIT(D_r,6);
  DISPLAY_1DIGIT(D_P,5);  
  LCDM10|=0x80;                          //decimal point  
  }
}
      #if 0
    if(chanx_int_params[channel_status]&NEW_I_LOG)
      {
      i=chanx_long_params[logged_I_accum]/chanx_int_params[logged_I_count]; 
      chanx_int_params[I_bias]=(chanx_int_params[I_bias]+i)/2;  
      chanx_int_params[channel_status]&=~NEW_I_LOG;
      
//      DISPLAY(chanx_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(chanx_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
            chanx_int_params[channel_status]|=CURRENT_HIGH_GAIN;
            chanx_long_params[E_accum_threshold]=chanx_high_gain_threshold;
                        
            master_stability_counter=40;  //recalculate security
            }
          P1OUT&=~BIT0;
          }//EOF if(chanx_int_params[logged_I_above_threshold_counter]<I_GAIN_LO_HI_THRESHOLD)
        if(chanx_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                        
            chanx_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) chanx_high_gain_threshold;            
          power_gain=power_gain*(float) average_VeREF_range;
          power_gain = power_gain/4096;
          chanx_long_params[E_accum_threshold]= (long)power_gain;              
          P1OUT|=BIT0;            
          }//EOF if(chanx_int_params[logged_I_above_threshold_counter]>I_GAIN_HI_LO_THRESHOLD)
        }//EOF if(!master_stability_counter)
      #endif
      chanx_int_params[logged_I_above_threshold_counter]=0;          
      } //EOF if(chanx_int_params[channel_status]&NEW_I_LOG)
     #endif 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -