📄 powermeasure.c
字号:
#include "includes.h"
unsigned short OverLoadTimeCnt = 0;
unsigned char Tariff = 0;
unsigned char CurrentPotNum;
unsigned char MinPwrInE2prom;
unsigned char EvtTimerCounterflag = 0;
unsigned char DisplayQuadrantFlag = LCD_SY_INFO_M2;
void ZoneAttributeDetect(void)
{
unsigned char zoneinfo[2],zonecnt,zonenum,i;
ReadBcdCountInfo(ADDR_OF_TIMEZONE_COUNT,&zonecnt,15,1);
zonenum=zonecnt-1;
for(i=0;i<zonecnt;i++)
{
FramRead(BASE_ZTSTA+i*3+ZT_OFFSET_DAY,zoneinfo,2);
if(SystemTime[SPACE_OF_MONTH]>zoneinfo[1])
{
zonenum=i;
}
else if(SystemTime[SPACE_OF_MONTH]==zoneinfo[1])
{
if(SystemTime[SPACE_OF_DAY]>=zoneinfo[0])
{
zonenum=i;
}
}
}
_SysPotInfo.zonenum=zonenum;
}
const unsigned char _SpringFestivalDate[50]=
{
36,// {0x02,0x06},
24,// {0x01,0x24},
43,// {0x02,0x12},
32,// {0x02,0x01},
22,// {0x01,0x22},
40,// {0x02,0x09},
29,// {0x01,0x29},
49,// {0x02,0x18},
38,// {0x01,0x07},
26,// {0x01,0x26},
45,// {0x02,0x14},
34,// {0x02,0x03},
23,// {0x01,0x23},
41,// {0x02,0x10},
31,// {0x01,0x31},
50,// {0x02,0x19},
39,// {0x02,0x08},
28,// {0x01,0x28},
47,// {0x02,0x16},
36,// {0x02,0x05},
25,// {0x01,0x25},
43,// {0x02,0x12},
32,// {0x02,0x01},
22,// {0x01,0x22},
41,// {0x02,0x10},
29,// {0x01,0x29},
48,// {0x02,0x17},
37,// {0x02,0x06},
26,// {0x01,0x26},
44,// {0x02,0x13},
34,// {0x02,0x03},
23,// {0x01,0x23},
42,// {0x02,0x11},
31,// {0x01,0x31},
50,// {0x02,0x19},
39,// {0x02,0x08},
28,// {0x01,0x28},
46,// {0x02,0x15},
35,// {0x02,0x04},
24,// {0x01,0x24},
43,// {0x02,0x12},
32,// {0x02,0x01},
22,// {0x01,0x22},
41,// {0x02,0x10},
30,// {0x01,0x30},
48,// {0x02,0x17},
37,// {0x02,0x06},
26,// {0x01,0x26},
45,// {0x02,0x14},
33// {0x02,0x02},
};
unsigned short PublicVacationDetect(void)
{
unsigned short addr=0,addr1;
unsigned char pvinfo[2],temp=0,pvcnt,i;
FramRead(ADDR_OF_WEEKEND_STATUS,&pvcnt,1);
if(!(pvcnt&(0x01<<SystemTime[SPACE_OF_WEEKDAY])))
{
temp|=0x01;
addr=ADDR_OF_WEEKEND;
}
ReadBcdCountInfo(ADDR_OF_PUB_VACATION_COUNT,&pvcnt,14,1);
for(i=0;i<pvcnt;i++)
{
addr1=BASE_PUBV+i*3+PUBV_OFFSET_DAY;
FramRead(addr1,pvinfo,2);
if((SystemTime[SPACE_OF_MONTH]==pvinfo[1])&&(SystemTime[SPACE_OF_DAY]==pvinfo[0]))
{
temp|=0x10;
addr=addr1-1;
break;
}
}
if(SystemTime_MonthHex<2)
{
pvcnt=Bcd2HexChar(SystemTime[SPACE_OF_YEAR]);
if(pvcnt<50)
{
pvinfo[0]=SystemTime_MonthHex*31+Bcd2HexChar(SystemTime[SPACE_OF_DAY]);
pvinfo[1]=_SpringFestivalDate[pvcnt];
pvinfo[0]=(unsigned char)((unsigned char)pvinfo[0]-(unsigned char)pvinfo[1]);
if(pvinfo[0]<=2)
{
temp|=0x20;
FramRead(ADDR_OF_SPRING_FEST,&pvcnt,1);
if((unsigned char)(pvcnt-1)<12)
{
addr=ADDR_OF_SPRING_FEST;
}
}
}
}
_SysPotInfo.pubvacstate=temp;
return(addr);
}
void GetPeriodOfTimeTableNum(void)
{
unsigned short addr_pottbl;
unsigned char pottblcnt,temp;
addr_pottbl=PublicVacationDetect();
ZoneAttributeDetect();
if(addr_pottbl==0)
{
addr_pottbl=BASE_ZTSTA+_SysPotInfo.zonenum*3+ZT_OFFSET_POTNUM;
}
ReadBcdCountInfo(ADDR_OF_POT_COUNT,&pottblcnt,13,1);
FramRead(addr_pottbl,&temp,1);
temp=Bcd2HexChar(temp);
if((temp==0)||(temp>pottblcnt))
{
temp=1;
}
_SysPotInfo.pottblnum=temp;
}
void EnsurePeriodOfTime(void)
{
unsigned short addr, addr_pottbl = 0;
unsigned char ii, exescnt;
ii = _SysPotInfo.day;
exescnt = _SysPotInfo.hour;
MemSetZero( (unsigned char *)&_SysPotInfo.chksum, sizeof(_sys_potinfo_s) );
_SysPotInfo.day = ii;
_SysPotInfo.hour = exescnt;
GetPeriodOfTimeTableNum();
ReadBcdCountInfo( ADDR_OF_POT_SWITCH_COUNT, &_SysPotInfo.potcnt, 15, 1 );
addr = BASE_POT_T + (ushort)(_SysPotInfo.pottblnum-1) * BASE_POT_T_OFFSET;
for( ii = 0; ii < _SysPotInfo.potcnt; ii++ )
{
FramRead( addr, (unsigned char *)&_SysPotInfo.potinfo[ii*3], 3 );
if( _SysPotInfo.potinfo[ii*3+POT_OFFSET_EXES] > 4 )
{
_SysPotInfo.potinfo[ii*3+POT_OFFSET_EXES] = 2;
}
addr += 3;
}
memcpy( (unsigned char *)&_SysDisplayRam.TimePot[0], (unsigned char *)&_SysPotInfo.potinfo[0], _SysPotInfo.potcnt * 3 );
_SysDisplayRam.Csum = SysDoCheckSum( (unsigned char *)&_SysDisplayRam.ActivePosPwr[0], sizeof(_disp_ram)-4 );
_SysPotInfo.day = SystemTime[SPACE_OF_DAY];
_SysPotInfo.hour = SystemTime[SPACE_OF_HOUR];
_SysPotInfo.mon = SystemTime[SPACE_OF_MONTH];
_SysPotInfo.year = SystemTime[SPACE_OF_YEAR];
FramRead( ADDR_OF_AUTO_RDDATA_DATE, (unsigned char *)&_SysPotInfo.balance_hour, 3 );
FramRead( ADDR_OF_SETTLEMENT_RECODE_MON, (unsigned char *)&_SysPotInfo.last_balance_mon, 2 );
ReadBcdCountInfo( ADDR_OF_REQ_CYCLE, &_SysPotInfo.reqtime, 60, 15 );
ReadBcdCountInfo( ADDR_OF_SLIP_TIME, &_SysPotInfo.reqtime_huacha, 60, 5 );
if( _SysPotInfo.reqtime == _SysPotInfo.reqtime_huacha )
{
MeterRunningStateWord |= BIT1;
}
else
{
MeterRunningStateWord &=~ BIT1;
}
DisplayContentReinit();
FramRead( ADDR_OF_PULSE_WIDTH, (unsigned char *)&addr_pottbl, 2 );
_SysPotInfo.pulse_wide = (unsigned char)Bcd2HexShort(addr_pottbl);
for( ii = 0; ii < 2; ii++ )
{
FramRead( ADDR_OF_METER_CONSTANT_A + 3 * ii + 1, (unsigned char *)&addr_pottbl, 2 );
addr_pottbl = Bcd2HexShort(addr_pottbl);
if( addr_pottbl == 0 )
{
addr_pottbl = 1;
}
if( ii == 0 )
{
_SysPotInfo.meter_const = (unsigned char)addr_pottbl;
}
else
{
_SysPotInfo.pulse_const = _SysPotInfo.meter_const/( (unsigned char)addr_pottbl );
if( _SysPotInfo.pulse_const == 0 )
{
_SysPotInfo.pulse_const = 1;
}
}
}
FramRead( ADDR_OF_SECURITY_CHK_IMAX, (unsigned char *)&_SysPotInfo.security_check_imax, 2 );
_SysPotInfo.security_check_imax = Bcd2HexShort( _SysPotInfo.security_check_imax );
for( ii = 0; ii < 6; ii++ )
{
FramRead( ADDR_OF_SUM_APOWER_FORMULA, (unsigned char *)&_SysPotInfo.measure_type[0], 6 );
}
for( ii = 0; ii < 4; ii++ )
{
_SysPotInfo.regv_con[ii] = 0;
FramRead( ADDR_OF_OV_UPVAL + ii * 3, (unsigned char *)&_SysPotInfo.regv_con[ii], 3 );
_SysPotInfo.regv_con[ii] = Bcd2HexLong( _SysPotInfo.regv_con[ii] );
}
for( ii = 0; ii < 7; ii++ )
{
_SysPotInfo.lvi_con[ii] = 0;
FramRead( ADDR_OF_LI_IVAL + ii * 3, (unsigned char *)&_SysPotInfo.lvi_con[ii], 3 );
_SysPotInfo.lvi_con[ii] = Bcd2HexLong( _SysPotInfo.lvi_con[ii] );
}
FramRead( ADDR_OF_LI_KEEP_LONG, (unsigned char *)&_SysPotInfo.timer_count[0], 11 );
for( ii = 0; ii < 11; ii++ )
{
_SysPotInfo.timer_count[ii] = Bcd2HexChar( _SysPotInfo.timer_count[ii] );
}
FramRead( ADDR_OF_ALARM_RAYCTL_WORD1, (unsigned char *)&_SysPotInfo.alarm_light1, 9 );
FramRead( ADDR_OF_REMOTE_FLAG, (unsigned char *)&_SysPotInfo.remote_flag, 1 );
FramRead( ADDR_OF_RATED_UB, (unsigned char *)&_SysPotInfo.rated_ub, 4 );
_SysPotInfo.rated_ub = Bcd2HexShort( _SysPotInfo.rated_ub );
_SysPotInfo.rated_ib = Bcd2HexShort( _SysPotInfo.rated_ib );
exescnt = 0;
for( ii = 0; ii < ATT_REGISTER_CAL_NUMBER; ii++ )
{
addr = ADDR_OF_ADE_BASE+ii * 3;
FramRead( addr, (unsigned char *)&_SysPotInfo.att702x_info[exescnt], ATT_REGISTER_LEN );
exescnt += ATT_REGISTER_LEN;
}
_SysPotInfo.chksum = SysDoCheckSum( (unsigned char *)&_SysPotInfo.regv_con[0], sizeof(_sys_potinfo_s)-4 );
}
void ClearMinPwrPreReqCycle(void)
{
unsigned short ii, loop;
memset( (unsigned char *)&pub_data.pub_data_buff3[0], 0, MIN_PWR_REC_SIZE );
if( ( _SysPotInfo.reqtime >= MIN_PWR_REC_IN_E2PROM ) ||( MinPwrInE2prom == 0 ) )
{
for( ii = 0; ii < MIN_PWR_REC_IN_E2PROM; ii++ )
{
FramWrite( ADDR_OF_MIN_BASE+ii*MIN_PWR_REC_SIZE, (unsigned char *)&pub_data.pub_data_buff3[0], MIN_PWR_REC_SIZE );
}
}
else if( _SysPotInfo.reqtime > MinPwrInE2prom )
{
for( ii = 0; ii < MinPwrInE2prom; ii++ )
{
FramWrite( ADDR_OF_MIN_BASE+(unsigned short)(MinPwrInE2prom-ii-1)*MIN_PWR_REC_SIZE, (unsigned char *)&pub_data.pub_data_buff3[0], MIN_PWR_REC_SIZE );
}
loop = _SysPotInfo.reqtime - MinPwrInE2prom;
for( ii = 1; ii <= loop; ii++ )
{
FramWrite( ADDR_OF_MIN_END-(unsigned short)ii*MIN_PWR_REC_SIZE, (unsigned char *)&pub_data.pub_data_buff3[0], MIN_PWR_REC_SIZE );
}
}
else
{
for( ii = 0; ii < _SysPotInfo.reqtime; ii++ )
{
FramWrite( ADDR_OF_MIN_BASE+(unsigned short)(MinPwrInE2prom-ii-1)*MIN_PWR_REC_SIZE, (unsigned char *)&pub_data.pub_data_buff3[0], MIN_PWR_REC_SIZE );
}
}
}
void TariffDetect(void)
{
unsigned char i,exes,potnum=_SysPotInfo.potcnt;
exes=_SysPotInfo.potinfo[(_SysPotInfo.potcnt-1)*3+POT_OFFSET_EXES];
for(i=0;i<_SysPotInfo.potcnt;i++)
{
if(SystemTime[SPACE_OF_HOUR]>_SysPotInfo.potinfo[i*3+POT_OFFSET_HOUR])
{
exes=_SysPotInfo.potinfo[i*3+POT_OFFSET_EXES];
potnum=i+1;
}
else if(SystemTime[SPACE_OF_HOUR]==_SysPotInfo.potinfo[i*3+POT_OFFSET_HOUR])
{
if(SystemTime[SPACE_OF_MINUTE]>=_SysPotInfo.potinfo[i*3+POT_OFFSET_MINUTE])
{
exes=_SysPotInfo.potinfo[i*3+POT_OFFSET_EXES];
potnum=i+1;
}
}
}
exes--;
if(exes>7)
{
exes=0x02;
}
if(Tariff!=exes)
{
SignalOutPutFlag |= BIT0;
if(RequirePowerTime)
{
CalcMaxDemand();
}
Tariff=exes;
ClearMinPwrPreReqCycle();
}
CurrentPotNum=potnum;
}
void StoreTodayEnergy(unsigned long addr)
{
unsigned long sum=0;
unsigned long *pTodayPower = (unsigned long*)(&_energy);
unsigned long *pPhasePower = (unsigned long*)(&CurrentPhasePower);
unsigned char ii;
addr /= DF_PAGE_LEN;
if( DataflashMainToBuffer1((ushort)addr) == OK )
{
for( ii = 1; ii < 51; ii++ )
{
WriteAddLongToBbuffer1( ii*4, pTodayPower[ii] );
}
for( ii = 0; ii < 12; ii++ )
{
WriteAddLongToBbuffer1( (unsigned short)(MON_ENERGY_PHASE+ii*4), pPhasePower[ii+1] );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -