📄 powermeasure.c
字号:
instp += pub_data.pub_data_buff1[24+i];
}
}
else
{
if( temp < 3 )
{
instp -= pub_data.pub_data_buff1[28+i];
}
else
{
instp -= pub_data.pub_data_buff1[24+i];
}
}
}
pa <<= 2;
flg <<= 2;
}
if( instp >= 0x8000 )
{
instp -= 0x8000;
}
else
{
instp = 0;
}
PwrData.WattHourLeft[temp] += instp;
PwrData.req_pwr[temp] += instp;
}
pa = Tariff%4;
flg = 0;
for( i= 0; i < COUNT_E_ELM; i++ )
{
for( j = 0; j < 5; j++ )
{
if( PwrData.WattHourLeft[i] >= PER_10WH )
{
temp = i*5+1;
pTodayPower[temp] = __bcd_add_long( pTodayPower[temp], 1 );
temp = i*5+pa+2;
pTodayPower[temp] = __bcd_add_long( pTodayPower[temp], 1 );
PwrData.WattHourLeft[i] -= PER_10WH;
flg |= BIT0;
}
else
{
break;
}
}
}
// 计算谐波电量
for( i = 0; i < 8; i++ )
{
for( j = 0; j < 5; j++ )
{
if( PwrData.LinePower[i] >= PER_10WH )
{
TempLinePower[i+1] = __bcd_add_long(TempLinePower[i+1], 1 );
PwrData.LinePower[i] -= PER_10WH;
flg |= BIT3;
}
else
{
break;
}
}
}
// 计算各分相总电量
for( i = 1; i <= 12; i++ )
{
for( j = 0; j < 5; j++ )
{
if( CurrentPhaseInstPower[i] >= PER_10WH )
{
CurrentPhasePower[i] = __bcd_add_long( CurrentPhasePower[i], 1 );
CurrentPhaseInstPower[i] -= PER_10WH;
flg |= BIT1;
}
else
{
break;
}
}
}
if( flg & BIT0 )
{
pTodayPower[0] = SysDoCheckSum( (unsigned char *)&pTodayPower[1], sizeof(_energy_s)-4 );
FramWrite( ADDR_OF_TODAY_PWR_START, (unsigned char*)(&pTodayPower[0]), sizeof(_energy_s) );
}
if( flg & BIT1 )
{
CurrentPhasePower[0] = SysDoCheckSum( (unsigned char *)&CurrentPhasePower[1], 48 );
FramWrite( ADDR_OF_PHASEPWR_START, (unsigned char *)&CurrentPhasePower[0], 52);
}
if( flg & BIT3 )
{
TempLinePower[0] = SysDoCheckSum( (unsigned char *)&TempLinePower[1], 32 );
FramWrite( ADDR_OF_LINEPWR_START, (unsigned char *)&TempLinePower[0], 36 );
}
CurrentPhaseInstPower[0] = SysDoCheckSum( (unsigned char *)&CurrentPhaseInstPower[1], 48 );
FramWrite( ADDR_OF_PHASEINSTPWR_START, (unsigned char *)&CurrentPhaseInstPower[0], 52 );
PwrData.chksum = SysDoCheckSum( (unsigned char *)&PwrData.WattHourLeft[0], sizeof(_sys_wat_data)-4 );
FramWrite( ADDR_OF_RAM, (unsigned char *)&PwrData.chksum, sizeof(_sys_wat_data) );
}
void SaveEnergyMin(void)
{
MinPwrInE2prom = MinPwrInE2prom % (uchar)MIN_PWR_REC_IN_E2PROM;
MinPwrInE2prom++;
FramWrite( ADDR_OF_PWR_IN_FRAM_COUNT_START, &MinPwrInE2prom, 1 );
FramWrite( ADDR_OF_MIN_BASE+(ushort)(MinPwrInE2prom-1)*MIN_PWR_REC_SIZE, (unsigned char *)&PwrData.req_pwr[0], MIN_PWR_REC_SIZE );
MemSetZero( (unsigned char *)&PwrData.req_pwr[0], MIN_PWR_REC_SIZE );
CalcAverageEnergy( &PwrData.instant_energy[0], 1 );
PwrData.chksum = SysDoCheckSum( (unsigned char *)&PwrData.WattHourLeft[0], sizeof(_sys_wat_data)-4 );
FramWrite( ADDR_OF_RAM, (unsigned char *)&PwrData.chksum, sizeof(_sys_wat_data) );
}
void ClearLoadRespDayData(void)
{
unsigned long addr;
addr = LOAD_RESPRESET_DAY_BASE + (ulong)SystemTime_MonthHex * DF_PAGE_LEN;
DataflashClearPages( addr, 0, DF_PAGE_LEN, 2 );
}
void CalcAverageEnergy(unsigned long *data,unsigned char items)
{
unsigned long temp[COUNT_E_ELM],pwr[COUNT_E_ELM];
unsigned short i, j;
unsigned char loop;
MemSetZero( (unsigned char *)&pwr[0], MIN_PWR_REC_SIZE );
if( items > MIN_PWR_REC_IN_E2PROM )
{
return;
}
if( items <= MinPwrInE2prom )
{
for(i=0;i<items;i++)
{
FramRead( ADDR_OF_MIN_BASE+(ushort)(MinPwrInE2prom-i-1)*MIN_PWR_REC_SIZE,(unsigned char *)&temp[0], MIN_PWR_REC_SIZE );
for( j = 0; j < COUNT_E_ELM; j++ )
{
pwr[j] += temp[j];
}
}
}
else
{
for(i=0;i<MinPwrInE2prom;i++)
{
FramRead(ADDR_OF_MIN_BASE+(ushort)(MinPwrInE2prom-i-1)*MIN_PWR_REC_SIZE,(unsigned char *)&temp[0], MIN_PWR_REC_SIZE);
for( j = 0; j < COUNT_E_ELM; j++ )
{
pwr[j] += temp[j];
}
}
loop = items - MinPwrInE2prom;
for( i = 1; i <= loop; i++ )
{
FramRead( ADDR_OF_MIN_END-i*MIN_PWR_REC_SIZE, (unsigned char *)&temp[0], MIN_PWR_REC_SIZE );
for( j = 0; j < COUNT_E_ELM; j++ )
{
pwr[j] += temp[j];
}
}
}
for( i = 0; i < COUNT_E_ELM; i++ )
{
pwr[i]=(pwr[i]*6000)/items;
data[i]=pwr[i]/PER_10WH;
data[i]=Hex2BcdLong(data[i]);
}
}
void SaveMaxDemand(unsigned long addr,unsigned char *data)
{
unsigned char time[4];
unsigned char *t=&time[0];
FramWrite( (ushort)addr, data, 4 );
time[0]=SystemTime[1];
time[1]=SystemTime[2];
time[2]=SystemTime[4];
time[3]=SystemTime[5];
FramWrite((ushort)(addr+4), t, 4 );
}
void CalcMaxDemand(void)
{
unsigned long demand[COUNT_E_ELM],demand_huacha[COUNT_E_ELM];
unsigned short addr,i;
unsigned char huacha_time,zhouqi;
huacha_time = _SysPotInfo.reqtime_huacha;
zhouqi = _SysPotInfo.reqtime;
CalcAverageEnergy(demand_huacha,zhouqi);
huacha_time = Tariff%4;
for( i = 0; i < COUNT_E_ELM; i++ )
{
addr = ADDR_OF_CUR_MD_START+i*SIZE_PER_MD_REC*5;
FramRead( addr, (unsigned char *)&demand[i],4 );
if( demand_huacha[i] > demand[i] )
{
SaveMaxDemand(addr,(unsigned char *)&demand_huacha[i]);
}
addr += (huacha_time+1)*SIZE_PER_MD_REC;
FramRead( addr, (unsigned char *)&demand[i], 4 );
if( demand_huacha[i] > demand[i] )
{
SaveMaxDemand( addr, (unsigned char *)&demand_huacha[i] );
}
}
RequirePowerTime = 0;
}
void CalcInstDemand(unsigned char *data,unsigned char item)
{
unsigned long temp=0,pwr=0;
unsigned short i;
unsigned char loop;
if( _SysPotInfo.reqtime > MIN_PWR_REC_IN_E2PROM ) return;
if( _SysPotInfo.reqtime <= MinPwrInE2prom )
{
for( i = 0; i < _SysPotInfo.reqtime; i++ )
{
FramRead( ADDR_OF_MIN_BASE+(ushort)(MinPwrInE2prom-i-1)*MIN_PWR_REC_SIZE+item*4, (unsigned char *)&temp, 4 );
pwr += temp;
}
}
else
{
for( i = 0; i < MinPwrInE2prom; i++ )
{
FramRead( ADDR_OF_MIN_BASE+(ushort)(MinPwrInE2prom-i-1)*MIN_PWR_REC_SIZE+item*4, (unsigned char *)&temp, 4 );
pwr += temp;
}
loop = _SysPotInfo.reqtime - MinPwrInE2prom;
for( i = 1; i <= loop; i++ )
{
FramRead( ADDR_OF_MIN_END-i*MIN_PWR_REC_SIZE+item*4, (unsigned char *)&temp, 4 );
pwr += temp;
}
}
pwr = (pwr*6000)/_SysPotInfo.reqtime;
pwr = pwr/PER_10WH;
pwr = Hex2BcdLong(pwr);
memcpy( (unsigned char *)&data[0], (unsigned char *)&pwr, 4 );
}
void BillingDetect(void)
{
unsigned char systime[7],temp,moninfo,yearinfo;
memcpy( systime, SystemTime, 7 );
yearinfo = Bcd2HexChar( systime[SPACE_OF_YEAR] );
moninfo = SystemTime_MonthHex;
temp = (100 + yearinfo - _SysPotInfo.last_balance_year ) % 100;
if( temp == 0 )
{
temp = ( 12 + SystemTime_MonthHex - _SysPotInfo.last_balance_mon ) % 12;
if( temp > 1 )
{
if( moninfo > _SysPotInfo.last_balance_mon )
{
moninfo = ( _SysPotInfo.last_balance_mon + 1 ) % 12;
}
else
{
moninfo = ( moninfo + 11) % 12;
if( moninfo == 11 )
{
yearinfo = ( yearinfo + 99 ) % 100;
}
}
goto SureToBalance;
}
else if( temp == 1 )
{
if( systime[SPACE_OF_DAY] > _SysPotInfo.balance_day )
{
goto SureToBalance;
}
else if( systime[SPACE_OF_DAY] == _SysPotInfo.balance_day )
{
if( systime[SPACE_OF_HOUR] >= _SysPotInfo.balance_hour )
{
goto SureToBalance;
}
}
}
}
else if( temp == 1 )
{
if( yearinfo > _SysPotInfo.last_balance_year )
{
moninfo = ( _SysPotInfo.last_balance_mon + 1 ) % 12;
if( moninfo < _SysPotInfo.last_balance_mon )
{
yearinfo = ( _SysPotInfo.last_balance_year + 1 ) % 12;
}
else
{
yearinfo = _SysPotInfo.last_balance_year;
}
goto SureToBalance;
}
else
{
if( moninfo != 0 )
{
moninfo = ( moninfo + 11 ) % 12;
goto SureToBalance;
}
}
}
else
{
moninfo=(moninfo+11)%12;
if(moninfo==11)
{
yearinfo=(yearinfo+99)%100;
}
goto SureToBalance;
}
return;
SureToBalance:
SaveEvtDataShift();
SaveEvtClrReqPwrData(5,0);
SavePgmInfoToFram( ADDR_OF_REQ_CLR_REC, 0 );
PowerDataStoreRuleDay();
ClearTodayData();
SaveCurrMd2Flash();
PowerDataShift(yearinfo,moninfo);
MonthDataShift(yearinfo, moninfo);
ClearCurrentMonthData(BASE_CUR);
ClearMinPwrPreReqCycle();
ClearLoadRespDayData();
FramWrite(ADDR_OF_SETTLEMENT_RECODE_MON,&moninfo,1);
FramWrite(ADDR_OF_SETTLEMENT_RECODE_MON+1,&yearinfo,1);
yearinfo=0x0;
FramWrite(ADDR_OF_SUCCESSSETTIME_FLAG,&yearinfo,1);
EnsurePeriodOfTime();
}
unsigned short GetPowerFactor(unsigned char item)
{
unsigned long ulTmp;
unsigned short pcoe;
if( PowerStateFlag & PS_BREAKPOWER_FLAG )
{
return(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -