#define KI 100.00 //151.954 //1.002*100
#define KU 1.00 //2.04 // 2.01*10
#define PhsOfst 0 //1.100
UDWORD spi_read_parameter(UBYTE Addr)
{
UDWORD tmpData=0;
UBYTE i;
SPI_PDIR |=( SPI_CS + SPI_CLK + SPI_DO);
SPI_PDIR &=~ SPI_DI;
SPI_POUT |= SPI_CS;
_NOP();
_NOP();
_NOP();
SPI_POUT &=~SPI_CS;
_NOP();
// write address to SPI device.
for(i=1;i<=8;i++)
{
SPI_POUT |= SPI_CLK;
_NOP();
_NOP();
if ((Addr&0x80)==0x80) SPI_POUT |= SPI_DO;
else SPI_POUT &=~SPI_DO;
Addr <<= 1;
_NOP();
_NOP();
SPI_POUT &=~SPI_CLK;
_NOP();
_NOP();
}
// read data from SPI device.
_NOP();
_NOP();
_NOP();
_NOP();
tmpData = 0;
for(i=1;i<=24;i++)
{
SPI_POUT |= SPI_CLK;
_NOP();
tmpData <<= 1;
if ((SPI_PIN&SPI_DI)==SPI_DI) tmpData |= 0x0001;
else tmpData &=~0x0001;
_NOP();
_NOP();
SPI_POUT &=~SPI_CLK;
}
SPI_POUT |= SPI_CS;
_NOP();
_NOP();
_NOP();
SPI_POUT &=~SPI_CS;
return tmpData;
}
void spi_write_parameter(UBYTE Addr, UDWORD Data)
{
UBYTE i;
SPI_PDIR |=( SPI_CS + SPI_CLK + SPI_DO);
SPI_PDIR &=~ SPI_DI;
SPI_POUT |= SPI_CS;
_NOP();
_NOP();
_NOP();
SPI_POUT &=~SPI_CS;
_NOP();
// write address to SPI device.
Addr |= 0x80;
for(i=1;i<=8;i++)
{
SPI_POUT |= SPI_CLK;
_NOP();
_NOP();
if ((Addr&0x80)==0x80) SPI_POUT |= SPI_DO;
else SPI_POUT &=~SPI_DO;
Addr <<= 1;
_NOP();
_NOP();
SPI_POUT &=~SPI_CLK;
_NOP();
_NOP();
}
// write data to SPI device.
_NOP();
_NOP();
_NOP();
_NOP();
for(i=1;i<=24;i++)
{
SPI_POUT |= SPI_CLK;
_NOP();
_NOP();
if ((Data&0x800000)==0x800000) SPI_POUT |= SPI_DO;
else SPI_POUT &=~SPI_DO;
Data <<=1;
_NOP();
_NOP();
SPI_POUT &=~SPI_CLK;
_NOP();
}
SPI_POUT |= SPI_CS;
_NOP();
_NOP();
_NOP();
SPI_POUT &=~SPI_CS;
}
void spi_init(UDWORD *tmpADJ)
{
UBYTE i;
//spi_write_parameter(CheckMeterAddr[30],tmpADJ[30]);
//spi_write_parameter(CheckMeterAddr[31],tmpADJ[31]);
for(i=0;i<=36;i++)
{
if(i==31) i++;
spi_write_parameter(CheckMeterAddr[i],tmpADJ[i]);
}
// spi_write_parameter(0x2A,1);
}
void ATT7022Reset(void)
{
SPI_POUT |= ARESET;
SPI_PDIR |= ARESET;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
SPI_POUT &=~ARESET;
spi_write_parameter(0xD3,0);
spi_write_parameter(0xC3,0);
}
void ReadPwr(void)
{
UBYTE i;
for(i=0; i<4; i++) DM.SPI.ActPwr[i]=spi_read_parameter(i+0x01);
for(i=0; i<4; i++) DM.SPI.ReaPwr[i]=spi_read_parameter(i+0x05);
for(i=0; i<4; i++) DM.SPI.AppPwr[i]=spi_read_parameter(i+0x09);
}
void ReadU( UBYTE ch )
{
float iii;
DM.SPI.UU[ch]=spi_read_parameter(ch+0x0D);
if(DM.SPI.UU[ch]<=_23_bit)
iii=(((float)DM.SPI.UU[ch]*_10_bit)/_23_bit)*KU;
else
iii=(((float)_24_bit-DM.SPI.UU[ch])*_10_bit)/_23_bit*KU;
Word2BCD( DM.SPI.U[ch], (UWORD)iii );
}
void ReadI( UBYTE ch )
{
float iii;
DM.SPI.II[ch]=spi_read_parameter(ch+0x10);
if(DM.SPI.II[ch]<=_23_bit)
iii=(((float)DM.SPI.II[ch]*_10_bit)/_23_bit)*KI;
else
iii=(((float)_24_bit-DM.SPI.II[ch])*_10_bit)/_23_bit*KI;
Word2BCD( DM.SPI.I[ch], (UWORD)iii );
}
void ReadFct( UBYTE ch )
{
float iii;
DM.SPI.Fct[ch]=spi_read_parameter(ch+0x14);
if(DM.SPI.Fct[ch]<=_23_bit)
iii=((float)DM.SPI.Fct[ch]/_23_bit)*1000;
else
iii=((float)_24_bit-DM.SPI.Fct[ch])/_23_bit*1000;
Word2BCD( DM.SPI.Factor[ch], (UWORD)iii );
}
void ReadFreq(void)
{
float iii;
DM.SPI.Freq=spi_read_parameter(0x1C);
iii=((float)DM.SPI.Freq*_10_bit)/_23_bit*10;
Word2BCD( DM.SPI.MainF, (UWORD)iii );
}
void ReadPhs( UBYTE ch )
{
float iii;
DM.SPI.Phs[ch]=spi_read_parameter(ch+0x18);
if(DM.SPI.Phs[ch]<=_23_bit)
iii=(((float)DM.SPI.Phs[ch]/_23_bit)*2*180/__PI)*10;
else
iii=((((float)DM.SPI.Phs[ch]-_24_bit)/_23_bit)*2*180/__PI)*10;
iii += PhsOfst;
Word2BCD(DM.SPI.Phss[ch], (iii >= 0) ? iii : (3600+iii) );
}
void ReadE(void)
{
UWORD tmpUWORD;
UWORD tmpPls[4];
char Tmp=BCD2Byte(SM.Cfg.MetConst[1]);
tmpPls[0]=spi_read_parameter(0x63);
tmpPls[1]=spi_read_parameter(0x67);
tmpPls[2]=spi_read_parameter(0x6B);
tmpPls[3]=spi_read_parameter(0x6F);
SM.Pules[0][0] += tmpPls[0];
SM.Pules[1][0] += tmpPls[1];
SM.Pules[2][0] += tmpPls[2];
SM.Pules[3][0] += tmpPls[3];
SM.Pules[0][SM.Cfg.FeeNo[0]+1] = SM.Pules[0][0];
SM.Pules[1][SM.Cfg.FeeNo[0]+1] = SM.Pules[1][0];
SM.Pules[2][SM.Cfg.FeeNo[0]+1] = SM.Pules[2][0];
SM.Pules[3][SM.Cfg.FeeNo[0]+1] = SM.Pules[3][0];
DM.MaxBuf[0][0][0] += tmpPls[0];
DM.MaxBuf[0][0][SM.Cfg.FeeNo[0]+1] += tmpPls[0];
DM.MaxBuf[0][1][0] += tmpPls[1];
DM.MaxBuf[0][1][SM.Cfg.FeeNo[0]+1] +=tmpPls[1];
// 正向有功
while(SM.Pules[0][0]>=Tmp)
{
SM.Pules[0][0] -= Tmp;
_BCD4INC(SM.CMon.KwhPp.Fee[0]);
if(SM.CMon.KwhPp.Fee[0][0]==0) DM.E2Flg |= F_E2A1;
}
while(SM.Pules[0][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[0][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhPp.Fee[SM.Cfg.FeeNo[0]+1]);
}
// 反向有功
while(SM.Pules[1][0]>=Tmp)
{
SM.Pules[1][0] -= Tmp;
_BCD4INC(SM.CMon.KwhPn.Fee[0]);
if(SM.CMon.KwhPn.Fee[0][0]==0) DM.E2Flg |= F_E2A2;
}
while(SM.Pules[1][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[1][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhPn.Fee[SM.Cfg.FeeNo[0]+1]);
}
// 第一象限无功(pL)
tmpUWORD = (DM.SPI.Phss[3][1]<<8)+DM.SPI.Phss[3][0];
if ( (tmpUWORD>0) && (tmpUWORD<=0x900) ) // 90 * 10
{
while(SM.Pules[2][0]>=Tmp)
{
SM.Pules[2][0] -= Tmp;
_BCD4INC(SM.CMon.KwhQpL.Fee[0]);
if(SM.CMon.KwhQpL.Fee[0][0]==0) DM.E2Flg |= F_E2pL;
}
while(SM.Pules[2][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[2][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhQpL.Fee[SM.Cfg.FeeNo[0]+1]);
}
}
// 第二象限无功(pC)
else if ( (tmpUWORD>0x900) && (tmpUWORD<=0x1800) )
{
while(SM.Pules[3][0]>=Tmp)
{
SM.Pules[3][0] -= Tmp;
_BCD4INC(SM.CMon.KwhQpC.Fee[0]);
if(SM.CMon.KwhQpC.Fee[0][0]==0) DM.E2Flg |= F_E2pC;
}
while(SM.Pules[3][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[3][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhQpC.Fee[SM.Cfg.FeeNo[0]+1]);
}
}
// 第三象限无功(nL)
else if ( (tmpUWORD>0x1800) && (tmpUWORD<=0x2700) )
{
while(SM.Pules[2][0]>=Tmp)
{
SM.Pules[2][0] -= Tmp;
_BCD4INC(SM.CMon.KwhQnL.Fee[0]);
if(SM.CMon.KwhQnL.Fee[0][0]==0) DM.E2Flg |= F_E2nL;
}
while(SM.Pules[2][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[2][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhQnL.Fee[SM.Cfg.FeeNo[0]+1]);
}
}
// 第四象限无功(nC)
else if ( (tmpUWORD>0x2700) && (tmpUWORD<=0x3600) )
{
while(SM.Pules[3][0]>=Tmp)
{
SM.Pules[3][0] -= Tmp;
_BCD4INC(SM.CMon.KwhQnC.Fee[0]);
if(SM.CMon.KwhQnC.Fee[0][0]==0) DM.E2Flg |= F_E2nC;
}
while(SM.Pules[3][SM.Cfg.FeeNo[0]+1]>=Tmp)
{
SM.Pules[3][SM.Cfg.FeeNo[0]+1] -= Tmp;
_BCD4INC(SM.CMon.KwhQnC.Fee[SM.Cfg.FeeNo[0]+1]);
}
}
MemCopy( SM.CMon.KwhQp.Fee[0], SM.CMon.KwhQpL.Fee[0], 4 );
_BCD4ADD( SM.CMon.KwhQp.Fee[0], SM.CMon.KwhQpC.Fee[0] );
MemCopy( SM.CMon.KwhQp.Fee[SM.Cfg.FeeNo[0]+1], SM.CMon.KwhQpL.Fee[SM.Cfg.FeeNo[0]+1], 4 );
_BCD4ADD( SM.CMon.KwhQp.Fee[SM.Cfg.FeeNo[0]+1], SM.CMon.KwhQpC.Fee[SM.Cfg.FeeNo[0]+1] );
MemCopy( SM.CMon.KwhQn.Fee[0], SM.CMon.KwhQnL.Fee[0], 4 );
_BCD4ADD( SM.CMon.KwhQn.Fee[0], SM.CMon.KwhQnC.Fee[0] );
MemCopy( SM.CMon.KwhQn.Fee[SM.Cfg.FeeNo[0]+1], SM.CMon.KwhQnL.Fee[SM.Cfg.FeeNo[0]+1], 4 );
_BCD4ADD( SM.CMon.KwhQn.Fee[SM.Cfg.FeeNo[0]+1], SM.CMon.KwhQnC.Fee[SM.Cfg.FeeNo[0]+1] );
SM.CMonChk=ChkSum((char *)&SM.CMon, sizeof(SM.CMon));
SM.PlsChk=ChkSum((char *)&SM.Pules, s