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

📄 emeter.c

📁 430系列开发之MSP430FE42x开发代码实例
💻 C
📖 第 1 页 / 共 2 页
字号:
//#define WFS

/** \file 
  * ESP430CE1 Application Program Example
  *
  * This file shows exemplarily the usage of the ESP430CE1 module for
  * a single-phase emeter with two current sensors (one shunt and one
  * current transformer).
  *
  * \author Volker Rzehak
  * \date   04/08/2003
  *
  * \Modified Stefan Schauer
  * \date   04/29/2003
  */
//====================================================================

#include "device.h"
#include "emeter.h"
#include "parameter.h"
#include "display.h"


#if __VER__ < 200
sfrb    SD16CONF0          = 0xB7;
sfrb    SD16CONF1          = 0xBF;
#else
#define SD16CONF0_                (0xB7)  
DEFC(   SD16CONF0               , SD16CONF0_)
#define SD16CONF1_                (0xBF)  
DEFC(   SD16CONF1               , SD16CONF1_)
#endif

// const and var definition

unsigned char ZXLDFGedge = 0;

/// Saves firmware version during initialization.
unsigned int firmware_version; 
/// Cumulated active energy.
float total_energy;
/// Last temperature measurement result.
unsigned int temperature;

// buffer to save RET values
unsigned int savedRET[(RET31_ - RET0_)/2];

//union tu_ulong_word intlevel = {pSET_INTRPTLEVL_LO, pSET_INTRPTLEVL_HI};
struct ts_parameters s_parameters;

#ifdef __IAR_SYSTEMS_ICC__
#if __VER__ < 200
#pragma memory=constseg(INFOA)
#else
#pragma constseg=INFOA
#endif
#endif

#ifdef __CROSSWORKS__
#pragma constseg("INFOA")
#endif

const struct ts_parameters s_parameters_flash = 
           {
              defVRatio,
              defIRatio,
              defEnergieRatio,
              defSET_PHASECORR1,
              defSET_PHASECORR2,
              defSET_V1OFFSET,
              defSET_I1OFFSET,
              defSET_I2OFFSET,
              defSET_STARTCURR_INT,
              defSET_STARTCURR_FRAC,
              {defSET_INTRPTLEVL_LO,
               defSET_INTRPTLEVL_HI},
              def_togglelevel,
           };
#ifdef __IAR_SYSTEMS_ICC__
#if __VER__ < 200
#pragma memory=default
#else
#pragma constseg=default
#endif
#endif

#ifdef __CROSSWORKS__
#pragma dataseg(default)
#endif

unsigned int CalCyclCnt = 0;     // Register for Cycle calculation of Calibration
unsigned int wfs1;
unsigned int wfs2;
unsigned int wfs3;
union tu_long_word energy;
union ts_long_word tempenergy;

signed long sumenergy= 0;
signed long maxenergy= 0;

void display_error(void) 
{;}


//====================================================================
/**
  * Analog front-end initialization routine.
  *
  * Configures the sigma-delta ADC module as analog front-end for
  * a tamper-resistant meter using a current transformer and a
  * shunt as current sensors (see configuration of channel 0 and 1).
  */
void init_analog_front_end(void)
{
/**
  * First it makes sure that the Embedded Signal Processing is 
  * disabled, otherwise it wouldn't be possible to modify the 
  * SD16 registers.
  */
  ESPCTL &= ~ESPEN;

/**
  * Then the general configurations of the analog front-end are done
  * that apply to all channels: clock selection (SMCLK) and divider
  * settings (depending on SMCLK frequency) and reference voltage
  * selections.
  */
  
//  SD16CTL= SD16SSEL_1  // Clock selection: SMCLK
  SD16CTL = 0x800 + SD16SSEL_1  // Clock selection: SMCLK + (Amp: )
#if (MCLK_FREQ == 2)
         | SD16DIV_1   // divide by 2 => ADC clock: 1.094MHz
#endif         
#if (MCLK_FREQ == 4)
         | SD16DIV_2   // divide by 4 => ADC clock: 1.094MHz
#endif         
#if (MCLK_FREQ == 8)
         | SD16DIV_3   // divide by 8 => ADC clock: 1.094MHz
#endif         
         | SD16REFON;  // Use internal reference


  SD16CCTL0 = INCH_0;  // I1
  SD16CCTL1 = INCH_0;  // I2
  SD16CCTL2 = INCH_0;  // V

  SD16CONF1 |= 0x40;   // Delay of ADC clock = 40ns
  

// -------------------------------------------------------------------
/** 
  * - Selection of ADC gain:
  *   - VIN,MAX(GAIN = 1) = 0.5V  > VCT(Peak)
  *   - VIN,MAX(GAIN = 2) = 0.25V < VCT(Peak)
  *   - VIN,MAX(GAIN = 16) = 0.031V  > VShunt(Peak)
  *   - VIN,MAX(GAIN = 32) = 0.015V  < VShunt(Peak)
  */
  // -----------------------------------------------------------
  // Configure analog front-end channel 2 - Current 1
   SD16INCTL0= I1_Gain; // Set gain for channel 0 (I1)
//  SD16INCTL0= GAIN_1; // Set gain for channel 0 (I1) to 1
//  SD16INCTL0= GAIN_2; // Set gain for channel 0 (I1) to 2
//  SD16INCTL0= GAIN_32; // Set gain for channel 0 (I1) to 32
  SD16CCTL0 |= OSR_0;  // Set oversampling ratio to 256 (default)

  // -----------------------------------------------------------
  // Configure analog front-end channel 1 - Current 2
  SD16INCTL1= I2_Gain; // Set gain for channel 1 (I2)
//  SD16INCTL1= GAIN_1; // Set gain for channel 1 (I2) to 1
//  SD16INCTL1= GAIN_2; // Set gain for channel 1 (I2) to 2
//  SD16INCTL1= GAIN_16; // Set gain for channel 1 (I2) to 16
  SD16CCTL1 |= OSR_0;  // Set oversampling ratio to 256 (default)

  // -----------------------------------------------------------
  // Configure analog front-end channel 2 - Voltage
  SD16INCTL2= V_Gain; // Set gain for channel 2 (V)
//  SD16INCTL2= GAIN_1; // Set gain for channel 2 (V) to 1
//  SD16INCTL2= GAIN_2; // Set gain for channel 2 (V) to 2
//  SD16INCTL2= GAIN_32; // Set gain for channel 2 (V) to 32
  SD16CCTL2 |= OSR_0;  // Set oversampling ratio to 256 (default)

/**
  * \note 
  * Please note, that the oversampling ratio should be identical 
  * for all channels. Default is 256.
  */       

} // End of init_analog_front_end()

//====================================================================
/**
  * Sets one parameter of the ESP430CE1 module.
  *
  * The parameter \a param is loaded with \a data.
  *
  * \param param Parameter to be set.
  * \param data Data to be loaded into parameter.
  */
void set_parameter(unsigned int param, unsigned int data)
{
  volatile unsigned int timeout= 0xffff;  
  //  /\  Prevend variable from being "optimized".
  MBOUT1= data;
  MBOUT0= param;
  
  do
  { 
    while (((MBCTL & IN0IFG) == 0) && ((--timeout) > 0)) ;
    if (timeout == 0) { display_error(); return; }
  } while ((MBIN0 != mPARAMSET) || (MBIN1 != param));
}

//====================================================================
/**
  * Initializes ESP430CE1.
  *
  */
void init_esp_parameter(unsigned char flashvars)
{
  volatile unsigned int timeout;
  //  /\  Prevend variable from being "optimized".

  // copy predevined init values to RAM
  if (flashvars) s_parameters = s_parameters_flash;
  
/**
  * Makes sure that the Embedded Signal Processing 
  * is enabled, 
  */
  ESPCTL |= ESPEN;
  MBCTL = 0;
    
/**
  * that it is not in measurement or calibration mode,
  */
  if ((RET0 & 0x8000) != 0) 
  { 
    // Set Embedded Signal Processing into "Idle" mode
    MBOUT1= modeIDLE; // ESP_IDLE;
    MBOUT0= mSET_MODE;
    timeout= 0xffff;
    while (((RET0 & 0x8000) != 0) && (timeout-- > 0)) ;
  }

/**
  * and that it is ready to receive messages by requesting the
  * firmware version.
  */
  MBOUT0= mSWVERSION;
  timeout= 0xffff;
  do
  { 
    while (((MBCTL & IN0IFG) == 0) && (timeout-- > 0)) ;
    if (timeout == 0) { display_error(); return; }
  } while (MBIN0 != mSWRDY);
  firmware_version= MBIN1; // Save firmware version.
  
/**
  * Then the parameters are initialized.
  *
  * \par Control 0: make settings for :
  * - Use current channel I2 - tamper-detection
  * - Count absolute active energy 
  *   (negative energy is considered as tampering)
  * - Switch DC removal alorithm on for I1
  * - Switch DC removal alorithm on for I2
  */
  set_parameter(mSET_CTRL0, defSET_CTRL0);

/**
  * \Set Number of Measurement: 
  *    e.g.  4096 * 50Hz. => int after 1sec
  */
  set_parameter(mSET_INTRPTLEVL_LO, s_parameters.pSET_INTRPTLEVL.w[0]);
  set_parameter(mSET_INTRPTLEVL_HI, s_parameters.pSET_INTRPTLEVL.w[1]);


/**
  * \par Nominal Mains Frequency: 
  *    e.g.  50Hz.
  */
  set_parameter(mSET_NOMFREQ, defSET_NOMFREQ);
  
/**
  * \par Phase Error Correction:
  * Sets phase error for current 1/2 at nominal mains frequency for 
  * current transformer according to its specification
  * The phase error of the shunt is zero.
  */
  set_parameter(mSET_PHASECORR1, (int)s_parameters.pSET_PHASECORR1);
  set_parameter(mSET_PHASECORR2, (int)s_parameters.pSET_PHASECORR2);
  
/** \par Adjustment parameters for the two currents:
  * Current Transformer:
  * \par
  * There are two possibilties to adjust the two current
  * values:
  * -# Only one current value is scaled with a factor > 1
  *    to fit the others current's value. The factor
  *    for the later is set to 1.\n
  *    In this example current I1 would be scaled by 0.32/0.30 = 1.06667.
  *    (The RMS values at the maximum current are used to calculate the
  *     factor.)
  *    The current transformer's value is scaled to fit with 
  *    shunt's value, because the shunt's values are bigger, 

⌨️ 快捷键说明

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