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

📄 energy_module.c

📁 一个电表的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*===========================================================================
* 			 2006年。三星仪表SOC芯片开发项目
*
*
* 文件名称:Energy_Module.c
* 文件标识:
* 摘    要:能量计量模块有如下参数读和写:
            1、设置参数:A)、电流、电压通道增益,有功、无功、视在等功率增益,有功、无功、视在等能量放大比例。
                         B)、有功、无功小信号偏移补偿,电流、电压小信号偏移补偿。
                         C)、相位补偿系数。
                         D)、CF1、CF2脉冲常数(分子、分母二个参数)
                         E)、设置半波能量累计的半波数。set number of half line cycles  for LWATTHR,LVARHR and LVAHR accumulators

            2、配置参数:A)、模式配置:MODE1,MODE2,WAVMODE,NLMODE,ACCMODE。
                         B)、中断使能配置:只配置CF1、CF2有无功能量计量脉冲中断。(MIRQENL,MIRQENM,MIRQENH)

            3、计量参数:A)、电流,电压,频率,有功、无功、视在等功率
                         B)、CF1、CF2脉冲中断计量有无功电能量

            4、状态监测:计量模块复位(MIRQSTH D.7)


*
* 当前版本:Ver 1.0
* 作    者:罗冬生
* 修改内容:
* 修改日期:
* 完成日期:



===============================================================================================*/
#ifndef   _Energy_Module_
#define   _Energy_Module_

//===============================================================================================

#include "includes.h"

//===============================================================================================
//This macro is used for getting a 3-byte value from energy SFRs,
//sfrname should be one of:VRMS,IRMS,EIRQEN,EIRQST,WAV1,WAV2
//the 3-byte value will be saved in AdeReg

    #define GetEnergySFR(sfrname,DataL,DataM,DataH) \
            {\
                DataH = sfrname##H;\
                DataM = sfrname##M;\
                DataL = sfrname##L;\
            }
//------------------------------------------------------------------------------
//This macro is used for getting a value from energy registers,
//the return value will be saved in AdeReg

 #define GetEnergyReg(regaddr,DataL,DataM,DataH) \
       {\
         MADDPT = regaddr;\
         DataL  = MDATL;\
         DataM  = MDATM;\
         DataH  = MDATH;\
       }

//----------------------------------------------------------------------------------------

void SomeNops(void)                //延时几个操作周期
{
 unsigned char i = 2;
 while(i--);
}
//----------------------------------------------------------------------------------------

//       读能量寄存器三字节

void ReadEnergyReg3(unsigned char regaddr)
{
  MADDPT= regaddr & 0x7F ;
  SomeNops();
  Temp8L =  MDATL;
  Temp8M =  MDATM;
  Temp8H =  MDATH;
}

//----------------------------------------------------------------------------------------

//       读能量寄存器二字节

void ReadEnergyReg2(unsigned char regaddr)
{
  MADDPT= regaddr & 0x7F ;
  SomeNops();
  Temp8L =  MDATL;
  Temp8M =  MDATM;
}

//----------------------------------------------------------------------------------------
//       读能量寄存器一字节
void ReadEnergyReg1(unsigned char regaddr)
{
  MADDPT= regaddr & 0x7F ;
  SomeNops();
  Temp8L =  MDATL;
}

//-----------------------------------------------------------------------------------------
//       写能量寄存器一字节

unsigned char WriteEnergyReg1(unsigned char adr,unsigned char DataL)
{
  MDATL = DataL;                                  //设置数据
  MADDPT= adr | 0x80;                             //写对应地址
  SomeNops();
  ReadEnergyReg3(WATTHR);                         //改变地址指针内容(虚操作)

  ReadEnergyReg1(adr);                            //读写入的数据
  if (Temp8L == DataL)                            //判断写入数据正确性
     return  ErrorFalse;                          //置写入正确
  else
     return ErrorTrue;                            //置写入错误
}

//-----------------------------------------------------------------------------------------
//       写能量寄存器二字节

unsigned char WriteEnergyReg2(unsigned char adr,unsigned char DataL,unsigned char DataM)
{
  MDATL = DataL;                                //设置数据
  MDATM = DataM;
  MADDPT= adr | 0x80;                           //写对应地址
  SomeNops();
  ReadEnergyReg3(WATTHR);                       //改变地址指针内容(虚操作)

  ReadEnergyReg2(adr);                          //读写入的数据

  if ((Temp8L == DataL) && (Temp8M == DataM))   //判断写入数据正确性
     return  ErrorFalse;                        //置写入正确
  else
     return ErrorTrue;                          //置写入错误
}

//======================================================================================================


/*******************************************************************************
                Definition of Energy Registers--MODE1
*******************************************************************************/
#define SWRST           0x80    //Reset all of the energy measurement registers to default
#define DISZXLPF        0x40    //Disable the zero-crossing LPF
#define INTE            0x20    //Enable the digital integrator
#define SWAPBITS        0x10    //Swap CH1&CH2 ADCs
#define PWRDN           0x08    //Power down ADCs.
#define DISCF2OUT       0x04    //Disable Frequency output CF2
#define DISCF1OUT       0x02    //Disable Frequency output CF1
#define DISHPF          0x01    //Disable the HPFs in voltage and current channels.

#define ENCF2OUT          0x00    //Enable Frequency output CF2
#define ENCF1OUT          0x00    //Enable Frequency output CF1

/*******************************************************************************
                Definition of Energy Registers--MODE2
*******************************************************************************/
//Configuration bits for CF2 output
#define CF2_WATT        0x00    //CF2 frequency is proportional to active power
#define CF2_VAR         0x40    //CF2 frequency is proportional to reactive power
#define CF2_VA_IRMS     0x80    //CF2 frequency is proportional to apparent power or IRMS
//Configuration bits for CF1 output
#define CF1_WATT        0x00    //CF1 frequency is proportional to active power
#define CF1_VAR         0x10    //CF1 frequency is proportional to reactive power
#define CF1_VA_IRMS     0x20    //CF1 frequency is proportional to apparent power or IRMS
//Configuration bits for apparent power or IRMS for CF1&CF2 outputs
#define CF_IRMS         0x08
#define CF_VA           0x00

#define ZXRMS           0x04    //Enable update of RMS values synchronously to voltage ZX
#define FREQSEL         0x02    //PER_FREQ register holds a frequency measurement

/*******************************************************************************
                Definition of Energy Registers--WAVMODE
*******************************************************************************/
#define WAV1_Current    0x00        //波形寄存器1记录电流
#define WAV2_Voltag     0x20        //波形寄存器2记录电压
#define WAV_Clk         0x03        // 波形寄存器记录周期3.2khz

/*******************************************************************************
                Definition of Energy Registers--NLDMODE
*******************************************************************************/
#define IRMSNOLOAD      0x40    //Enable IRMS no-load threshold detection.
                                //The level is defined by the setting of the VANOLOAD bits.
//Apparent power No-load threshold
#define VANOLOAD_OFF    0x00    //Disabled
#define VANOLOAD_030    0x10    //Enabled with threshold = 0.030% of Full scale
#define VANOLOAD_015    0x20    //Enabled with threshold = 0.015% of Full scale
#define VANOLOAD_0075   0x30    //Enabled with threshold = 0.0075% of Full scale
//Reactive power No-l oad threshold
#define VARNOLOAD_OFF   0x00    //Disabled
#define VARNOLOAD_015   0x04    //Enabled with threshold = 0.015% of Full scale
#define VARNOLOAD_0075  0x08    //Enabled with threshold = 0.0075% of Full scale
#define VARNOLOAD_0037  0x0c    //Enabled with threshold = 0.0037% of Full scale
//Reactive power No-l oad threshold
#define APNOLOAD_OFF    0x00    //Disabled
#define APNOLOAD_015    0x01    //Enabled with threshold = 0.015% of Full scale
#define APNOLOAD_0075   0x02    //Enabled with threshold = 0.0075% of Full scale
#define APNOLOAD_0037   0x03    //Enabled with threshold = 0.0037% of Full scale

/*******************************************************************************
                Definition of Energy Registers--CALMODE
*******************************************************************************/
//These bits define the current channel used for energy measurements.
#define SEL_ICH_AUTO    0x30    //Current channel automatically selected by the tampering condition
#define SEL_ICH_A       0x10    //Current channel connected to IA
#define SEL_ICH_B       0x20    //Current channel connected to IB
#define V_CH_SHORT      0x08    //short V channel to ground
#define I_CH_SHORT      0x04    //short I channel to ground

/*******************************************************************************
                Definition of Energy Registers--GAIN
*******************************************************************************/
//These bits define the voltage channel input gain
#define PGA2_X1         0x00    //Gain = 1
#define PGA2_X2         0x20    //Gain = 2
#define PGA2_X4         0x40    //Gain = 4
#define PGA2_X8         0x60    //Gain = 8
#define PGA2_X16        0x80    //Gain = 16

//These bits define the current channel input gain
#define PGA1_X1         0x00    //Gain = 1
#define PGA1_X2         0x01    //Gain = 2
#define PGA1_X4         0x02    //Gain = 4
#define PGA1_X8         0x03    //Gain = 8
#define PGA1_X16        0x04    //Gain = 16

/*******************************************************************************
                Definition of Energy Registers--ACCMODE
*******************************************************************************/
//This bit indicate the current channel used to measure energy in anti-tampering mode
//0-Channel A
//1-Channel B
#define ICHANNEL        0x80

//Configuration bit to select event that will trigger a Fault interrupt
//0-Fault interrupt occurs when part enters Fault mode.
//1-Fault interrupt occurs when part enters Normal mode.
#define FAULTSIGN_EXIT  0x40
#define FAULTSIGN_ENTER 0x00
//Configuration bit to select event that will trigger an reactive power sign interrupt
//0-VARSIGN interrupt occurs when reative power changes from positive to negative
//1-VARSIGN interrupt occurs when reative power changes from negative to positive
#define VARSIGN_N_P     0x20
#define VARSIGN_P_N     0x00

//Configuration bit to select event that will trigger an active power sign interrupt
//0-VARSIGN interrupt occurs when ative power changes from positive to negative
//1-VARSIGN interrupt occurs when ative power changes from negative to positive
#define APSIGN_N_P      0x10
#define APSIGN_P_N      0x00
//1-Enables absolute value accumulation of Reactive power in energy register and pulse output
#define ABSVARM         0x08

//1-Enables reactive power accumulation depending on the sign of active power

⌨️ 快捷键说明

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