📄 initsystem.c
字号:
// ****************************************************************************
// 初始化时钟
// ****************************************************************************
void Init_FLL(void)
{
FLL_CTL0 |= XCAP18PF; // 配置电容
SCFI0 = FLLD_1; // Freq = 1.024MHz
SCFQCTL = 32 - 1;
} // End of fll_init()
// ****************************************************************************
// 初始化定时器A和TIMERA CCR0
//
// 说明:定时器时钟为ACLK, 定时器工作模式,连续计数模式
// 作用:定时检查能量累计,判断是否输出校表脉冲。
// ****************************************************************************
void Init_TA(void)
{
TACTL = TACLR | TASSEL_1;
TACTL |= MC_2;
//TACCR0 = SAMPLE_PERIOD;
//TACCTL0 = CCIE;
} //End of init_TA()
// ****************************************************************************
// 提升主频到 8MHz
// ****************************************************************************
void Goto_High_Freq(void)
{
// 提升主频前,应确认电源电压是合适跑高频
SVSCTL |= (SVSON | 0x60);
// 等待电源电压到适合跑高频
while ((SVSCTL & SVSOP));
// 电源电压已适合CPU跑高频,SVS进入低电压检测复位状态
SVSCTL |= PORON;
// 提升主频到 8MHz
//SCFI0 = FN_4 | FLLD_4;
//SCFQCTL = 64 - 1;
SCFI0 = FN_4 | FLLD_2;
SCFQCTL = 128 - 1;
FLL_CTL0 |= DCOPLUS;
for(unsigned int i=0;i<10000;i++)
{
_NOP();
}
} // End of goto_high_freq()
void Init_Sys_HardWare(void)
{
#if PCB_VERSION == 0
P1OUT = 0x00;
P1DIR = ~(0xFF&(KEY_UP_BIT + KEY_DOWN_BIT + KEY_REMOVE_BIT + BATT_TEST_BIT));
P1IES = (KEY_UP_BIT + KEY_DOWN_BIT + KEY_REMOVE_BIT + BATT_TEST_BIT);
P1IFG = 0x00;
P1IE = (KEY_UP_BIT + KEY_DOWN_BIT + KEY_REMOVE_BIT + BATT_TEST_BIT);
P1SEL = 0x00;
P2OUT = 0x00;
P2DIR = ~(0xFF&(EE_SCL_BIT + EE_SDA_BIT + POWER_TEST_BIT)); //P2DIR = 0x1F;
P2IES = POWER_TEST_BIT;
P2IFG = 0x00;
//P2IE = POWER_TEST_BIT;
P2IE = 0;
P2SEL = COMM_RXD0_BIT | COMM_TXD0_BIT;
Update_HC595_Port(0x00);
#elif PCB_VERSION==1
P1OUT = EE_WP_BIT;
P1DIR = LED_ACTIVE_BIT + LED_REACTIVE_BIT + COMM_ACLK_BIT + COMM_CON485_BIT
+ EE_WP_BIT + BIT4;
P1IES = (KEY_UP_BIT + KEY_DOWN_BIT);
P1IFG = 0x00;
P1IE = (KEY_UP_BIT + KEY_DOWN_BIT);
P1SEL = 0x00;
//P1SEL = COMM_ACLK_BIT;
P2OUT = EE_SCL_BIT + EE_SDA_BIT;
P2DIR = EE_SCL_BIT + EE_SDA_BIT + BIT2;
//P2DIR = ~(0xFF&(EE_SCL_BIT + EE_SDA_BIT + POWER_TEST_BIT)); //P2DIR = 0x1F;
//P2IES = POWER_TEST_BIT;
P2IES = 0x00;
P2IFG = 0x00;
//P2IE = POWER_TEST_BIT;
P2IE = 0;
P2SEL = COMM_RXD0_BIT | COMM_TXD0_BIT;
#endif
} //End of init_sys()
void Init_Sys_Data(void)
{
#if DATA_SAVE_TYPE==0
if( SM.CMonChk!=ChkSum((unsigned char *)&SM.CMon, sizeof(SM.CMon)))
{
DM.tmpPtr[0] = *(unsigned char *)0x10C0;
if(DM.tmpPtr[0]!=0x55)
{
if(*(unsigned char *)0x1080!=0x55)
{
MemFill((unsigned char *)&SM.CMon, sizeof(SM.CMon), 0);
SM.CMonChk=ChkSum((unsigned char *)&SM.CMon, sizeof(SM.CMon));
}
else
{
MemCopy( (unsigned char *)&SM.CMon, (unsigned char *)0x1082, sizeof(SM.CMon)+2 );
}
}
else
{
MemCopy( (unsigned char *)&SM.CMon, (unsigned char *)0x10C2, sizeof(SM.CMon)+2 );
}
}
#else
#endif
#if PULSE_MEASURE_MODE == 2
if(SM.uiActIntrptLevlCounter> 1000){SM.uiActIntrptLevlCounter=0;}
#else
if(SM.Acc.ullAccKwhPp>= ENERGY_PULSE_THRESHOLD_100000){SM.Acc.ullAccKwhPp=0;}
#ifndef NO_NEG_ENGER
if(SM.Acc.ullAccKwhPn>= ENERGY_PULSE_THRESHOLD_100000){SM.Acc.ullAccKwhPn=0;}
#endif
if(SM.Acc.ullAccKwhQp>= ENERGY_PULSE_THRESHOLD_100000){SM.Acc.ullAccKwhQp=0;}
#ifndef NO_NEG_ENGER
if(SM.Acc.ullAccKwhQn>= ENERGY_PULSE_THRESHOLD_100000){SM.Acc.ullAccKwhQn=0;}
#endif
#endif
}
void ESPParamSetting(void)
{
if(*((unsigned char *)(0x1000))!=0x55 && *((unsigned char *)(0x1001))!=0x55)
{
SM.Cfg.ucFlashOperated[0] = 0x55;
SM.Cfg.ucFlashOperated[1] = 0x55;
SM.Cfg.EspPar.iPhaseCorr1 = PHASECORR1_INIT;
#ifdef TAMPER_DETECTION
SM.Cfg.EspPar.iPhaseCorr2 = PHASECORR2_INIT;
#endif
//SM.Cfg.EspPar.iV1Offset = V1OFFSET_INIT;
//SM.Cfg.EspPar.iI1Offset = I1OFFSET_INIT;
#ifdef TAMPER_DETECTION
//SM.Cfg.EspPar.iI2Offset = I2OFFSET_INIT;
SM.Cfg.EspPar.uiAdaptI1 = ADAPTI1_INIT;
SM.Cfg.EspPar.uiAdaptI2 = ADAPTI2_INIT;
#endif
SM.Cfg.EspPar.uiGainCorr1 = GAINCORR1_INIT;
SM.Cfg.EspPar.lPowerOffset1 = POWEROFFSET1_INIT;
#ifdef TAMPER_DETECTION
SM.Cfg.EspPar.uiGainCorr2 = GAINCORR2_INIT;
SM.Cfg.EspPar.lPowerOffset2 = POWEROFFSET2_INIT;
#endif
#if PULSE_MEASURE_MODE==2
SM.Cfg.EspPar.ulIntrptLevl = INTRPTLEVL_INIT;
#endif
//SM.Cfg.EspPar.uiCalCyclCnt = CALCYCLCNT_INIT;
SM.Cfg.EspPar.ulStartCurrent = STARTCURRENT_INIT;
SM.Cfg.EspPar.uiNomFreq = NORMALFREQUENCE;
//SM.Cfg.EspPar.ucVDropCycls = VDROPCYCLS_INIT;
#ifdef TAMPER_DETECTION
SM.Cfg.EspPar.uiRatioTamp = RATIOTAMP_INIT;
SM.Cfg.EspPar.uiITamp = ITAMP_INIT;
#endif
SM.Cfg.EspPar.uiVDropLevel = VDROPLEVEL_INIT;
SM.Cfg.EspPar.uiVPeakLevel = VPEAKLEVEL_INIT;
SM.Cfg.EspPar.uiIPeakLevel = IPEAKLEVEL_INIT;
SM.Cfg.EspPar.ucDcRemPer = DCREMPER_INIT;
SM.Cfg.EspPar.uiV1Gain = VGAIN_INIT;
SM.Cfg.EspPar.ulIGain = IGAIN_INIT;
SM.Cfg.EspPar.iTempSampleOffset = 0;
SM.Cfg.MetID[0] = 0x00;
SM.Cfg.MetID[1] = 0x00;
SM.Cfg.MetID[2] = 0x00;
SM.Cfg.MetID[3] = 0x00;
SM.Cfg.MetID[4] = 0x00;
SM.Cfg.MetID[5] = 0x00;
SM.CfgChk=ChkSum((unsigned char *)&SM.Cfg, sizeof(SM.Cfg));
}
else
{
MemCopy( (unsigned char *)&SM.Cfg, (unsigned char *)0x1002, sizeof(SM.Cfg) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -