#include "../main/SystemInclude.h" u16 parameterState; /******************************************************************************/ void ReadTimeBase(void) { //=========================================================================== #ifndef ENABLE_EXT_RTC #pragma message("[undefined] ENABLE_EXT_RTC") #elif(ENABLE_EXT_RTC) if(TestExtRTCReady()) ExtRTCTimeRead(); else ReadCurrentTime(); #else // ReadCurrentTime(); #endif MBBuf.RxPointer[MBBuf.Index++] = timer[YEAR]; MBBuf.RxPointer[MBBuf.Index++] = timer[MONTH]; MBBuf.RxPointer[MBBuf.Index++] = timer[DATE]; MBBuf.RxPointer[MBBuf.Index++] = timer[HOUR]; MBBuf.RxPointer[MBBuf.Index++] = timer[MINUTE]; MBBuf.RxPointer[MBBuf.Index++] = timer[SECOND]; MBBuf.DataByte = 6; ModbusVariablePointerDec(); } /******************************************************************************/ void WriteTimeBase(void) { MBBuf.DataByte = 6; if(MBBuf.ByteNumber < 6) { MBBuf.ByteNumber = 0; MBBuf.BusError = ILLEGAL_DATA_VALUE; return; } timer[YEAR] = MBBuf.RxPointer[MBBuf.Index++]; timer[MONTH] = MBBuf.RxPointer[MBBuf.Index++]; timer[DATE] = MBBuf.RxPointer[MBBuf.Index++]; timer[HOUR] = MBBuf.RxPointer[MBBuf.Index++]; timer[MINUTE] = MBBuf.RxPointer[MBBuf.Index++]; timer[SECOND] = MBBuf.RxPointer[MBBuf.Index++]; //=========================================================================== #ifndef ENABLE_EXT_RTC #pragma message("[undefined] ENABLE_EXT_RTC") #elif(ENABLE_EXT_RTC) if(TestExtRTCReady()) ExtRTCTimeSet(); #endif //FY // RTCCTL0 = 0xA500 + RTCTEVIE; // RTCCTL13 = RTCHOLD; // RTCYEAR = (u16)timer[YEAR]+2000; // RTCMON = timer[MONTH]; // RTCDAY = timer[DATE]; // RTCHOUR = timer[HOUR]; // RTCMIN = timer[MINUTE]; // RTCSEC = timer[SECOND]; // RTCCTL0 = 0xA500 + RTCTEVIE; // RTCCTL13 = RTCMODE; // Minute changed // RTCCTL0 = 0; } /******************************************************************************/ u16 ReadSecond(void) { // u16 newSecond = RTCHOUR; // // if(newSecond > 11) newSecond -= 12; // newSecond *= 60; // newSecond += (u16)RTCMIN; // newSecond *= 60; // newSecond += (u16)RTCSEC; // return newSecond; return 1; } /******************************************************************************/ void SaveFactorySetting(void) // Save the factory setting { u16 I; u8 temp[64]; for(I=0; I< SAVE_PARA_MAX; I++) { ReadMultiByteFromEEPROM(I*64, temp, 64, PARA_EEPROM); WriteMultiByteToEEPROM(I*64, temp, 64, DATA_EEPROM); } WriteaAnAlarmData(0); } /******************************************************************************/ void RestoreFactorySetting(void) { u16 I; unsigned char temp[64]; for(I=0; I< SAVE_PARA_MAX; I++) { ReadMultiByteFromEEPROM(I*64, temp, 64, DATA_EEPROM); WriteMultiByteToEEPROM(I*64, temp, 64, PARA_EEPROM); } SystemParameterInit(); } /******************************************************************************/ void WriteMEMSRatio(void) { // tmpLA = (u32)(NRHHigh - NRHLow); // tmpLA *= (u32)RRRHRatio; // tmpLA /= (u32)(RHHighTa - RHLowTa); // tempL.Word[0] = (u16)tmpLA; // // MEMSRatio = WriteShortParameterToEEPROM(MEMS_RATIO, MEMS_RATIO_WIDTH); //FY } /******************************************************************************/ //仪表信息 void ReadCalibFAC(void) { tempL.Word[0] = 0x8000;//FAC大于0x8000 表示校准数据存储方式为连续存储【FL1 FM1 FH1 】【FL2 FM2 FH2 】...... //------------------------------------------------------------------------------ #ifndef ENABLE_ONE_CURVE #pragma message("[undefined] ENABLE_ONE_CURVE") #elif(ENABLE_ONE_CURVE) calibType = INTCMD_CTYPE_CURVE; #endif //------------------------------------------------------------------------------ if(calibType == INTCMD_CTYPE_CURVE) tempL.Word[0] |= CALIB_COMP_FAC;//DATA_COMP_FAC //------------------------------------------------------------------------------ #ifndef ENABLE_2ND_CURVE #pragma message("[undefined] ENABLE_2ND_CURVE") #elif(ENABLE_2ND_CURVE) else if(calibType == INT_2ND_CURVE_CMD) tempL.Word[0] |= DATA_COMP_FAC2; #endif //------------------------------------------------------------------------------ #ifndef ENABLE_3RD_CURVE #pragma message("[undefined] ENABLE_3RD_CURVE") #elif(ENABLE_3RD_CURVE) else if(calibType == INT_3RD_CURVE_CMD) tempL.Word[0] |= DATA_COMP_FAC3; #endif //------------------------------------------------------------------------------ else tempL.Word[0] = 0; curveMode |= 0x0005; } /******************************************************************************/ void ReadCalibDepth(void) { //------------------------------------------------------------------------------ #ifndef ENABLE_ONE_CURVE #pragma message("[undefined] ENABLE_ONE_CURVE") #elif(ENABLE_ONE_CURVE) calibType = INTCMD_CTYPE_CURVE; #endif //------------------------------------------------------------------------------ if(calibType == INTCMD_CTYPE_CURVE) { tempL.Word[0] = CALIB_DEPTH; } //------------------------------------------------------------------------------ #ifndef ENABLE_2ND_CURVE #pragma message("[undefined] ENABLE_2ND_CURVE") #elif(ENABLE_2ND_CURVE) else if(calibType == INT_2ND_CURVE_CMD) tempL.Word[0] = CALIB_DEPTH2; #endif //------------------------------------------------------------------------------ #ifndef ENABLE_3RD_CURVE #pragma message("[undefined] ENABLE_3RD_CURVE") #elif(ENABLE_3RD_CURVE) else if(calibType == INT_3RD_CURVE_CMD) tempL.Word[0] = CALIB_DEPTH3; #endif //------------------------------------------------------------------------------ else tempL.Word[0] = 0; curveMode |= 0x00a0; } /******************************************************************************/ void ReadCalibWidth(void) { tempL.Word[0] = CALIB_WIDTH; curveMode |= 0xa500; } /******************************************************************************/ void ReadCalbrationDataByCom(u16 calibAddr, u16 calibDepth) { MBBuf.StartAddr &= 0x0fff; if((MBBuf.StartAddr >= MAX_OPERATE_DEPTH) || (MBBuf.ByteNumber > CALIB_WIDTH)) { MBBuf.ByteNumber = 0; MBBuf.BusError = ILLEGAL_DATA_ADDRESS; return; } MBBuf.StartAddr *= CALIB_WIDTH; MBBuf.StartAddr += calibAddr; disable_interrupts(); ReadMultiByteFromEEPROM(MBBuf.StartAddr, &MBBuf.RxPointer[3], MBBuf.ByteNumber, PARA_EEPROM); enable_interrupts(); //systemProcessing |= WRITE_DATA_HANDLE; MBBuf.ByteNumber = 0; } /******************************************************************************/ void WriteCalbrationDataByCom(u16 calibAddr, u16 calibDepth) { //--------------------------------------------------------------------------- if((securityID != (u32)COM_PASSWORD1) && (securityID != (u32)COM_PASSWORD2)) return; IDvalidTime = 0; //--------------------------------------------------------------------------- MBBuf.StartAddr &= 0x0fff; if((MBBuf.StartAddr >= calibDepth) || (MBBuf.ByteNumber > CALIB_WIDTH)) { MBBuf.ByteNumber = 0; MBBuf.BusError = ILLEGAL_DATA_ADDRESS; return; } disable_interrupts(); if(MBBuf.StartAddr == 0) WriteMeterBasicData(); MBBuf.StartAddr *= CALIB_WIDTH; MBBuf.StartAddr += calibAddr; WriteMultiByteToEEPROM(MBBuf.StartAddr, &MBBuf.RxPointer[MBBuf.Index], MBBuf.ByteNumber, PARA_EEPROM); enable_interrupts(); //systemProcessing |= WRITE_DATA_HANDLE; MBBuf.ByteNumber = 0; } /******************************************************************************/ void ReadEEPROMByCom(u16 RDAdr, u16 RDLen) { ReadMultiByteFromEEPROM(RDAdr, &MBBuf.RxPointer[MBBuf.Index], MBBuf.ByteNumber, PARA_EEPROM); MBBuf.DataByte = RDLen; MBBuf.Index += RDLen; ModbusVariablePointerDec(); } /******************************************************************************/ void ReadFlashByCom(const unsigned char *RDAdr, u16 RDLen) { u16 I; for(I=0; I WRMax) return; else if(tempL.Word[0] < WRMin) return; // MBBuf.BusError = 0; V2004 I = *WRSource; if(tempL.Word[0] == I) return; parameterState = NEED_OPERATION; //I = WRLen - 1; //tempL.Byte[I] = CRC8(tempL.Byte, I); //WriteMultiByteToEEPROM(WRAddr, tempL.Byte, WRLen, PARA_EEPROM); //ReadMultiByteFromEEPROM(WRAddr, tempL.Byte, WRLen, PARA_EEPROM); //tempL.Byte[I] = 0; //*WRSource = tempL.Word[0]; *WRSource = WriteShortParameterToEEPROM(WRAddr, WRLen); } /******************************************************************************/ void FreeWriteWordByCom(u16 *WRSource, u16 WRAddr, u16 WRLen, u16 WRMax, u16 WRMin ) { //--------------------------------------------------------------------------- parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; //--------------------------------------------------------------------------- MBBuf.DataByte = 2; if(MBBuf.ByteNumber < MBBuf.DataByte) { MBBuf.ByteNumber = 0; return; } //--------------------------------------------------------------------------- MBBuf.BusError = 0; //V2004 u16 I; tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; if(tempL.Word[0] > WRMax) return; else if(tempL.Word[0] < WRMin) return; //MBBuf.BusError = 0; V2004 I = *WRSource; if(tempL.Word[0] == I) return; //--------------------------------------------------------------------------- parameterState = NEED_OPERATION; //--------------------------------------------------------------------------- *WRSource = WriteShortParameterToEEPROM(WRAddr, WRLen); } /******************************************************************************/ void WriteDWordByCom(u32 *WRSource, u16 WRAddr,u16 WRLen, u32 WRMax, u32 WRMin ) { //--------------------------------------------------------------------------- parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; //--------------------------------------------------------------------------- MBBuf.DataByte = 4; if(MBBuf.ByteNumber < MBBuf.DataByte) { MBBuf.ByteNumber = 0; return; } //--------------------------------------------------------------------------- tempDev.Byte[3] = MBBuf.RxPointer[MBBuf.Index++]; tempDev.Byte[2] = MBBuf.RxPointer[MBBuf.Index++]; tempDev.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempDev.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; //--------------------------------------------------------------------------- MBBuf.BusError = ACKNOWLEDGE; if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return; MBBuf.BusError = 0; //V2004 IDvalidTime = 0; //--------------------------------------------------------------------------- if(tempDev.DWord[0] > WRMax) return; else if(tempDev.DWord[0] < WRMin) return; //MBBuf.BusError = 0; V2004 tempDev.DWord[1] = *WRSource; if(tempDev.DWord[0] == tempDev.DWord[1]) return; //--------------------------------------------------------------------------- parameterState = NEED_OPERATION; //--------------------------------------------------------------------------- *WRSource = WriteParameterToEEPROM(WRAddr, WRLen); } /******************************************************************************/ void SetWordCmdByCom(u16 *WRSource) { //--------------------------------------------------------------------------- parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; //--------------------------------------------------------------------------- MBBuf.DataByte = 2; if(MBBuf.ByteNumber < 2) { MBBuf.ByteNumber = 0; return; } //--------------------------------------------------------------------------- MBBuf.BusError = ACKNOWLEDGE; if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return; MBBuf.BusError = 0; //V2004 IDvalidTime = 0; //--------------------------------------------------------------------------- parameterState = NEED_OPERATION; //MBBuf.BusError = 0; //--------------------------------------------------------------------------- tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; *WRSource = tempL.Word[0]; } /******************************************************************************/ void FreeSetWordCmdByCom(u16 *WRSource) { MBBuf.DataByte = 2; if(MBBuf.ByteNumber < 2) { MBBuf.ByteNumber = 0; parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; return; } parameterState = NEED_OPERATION; MBBuf.BusError = 0; tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; *WRSource = tempL.Word[0]; } /******************************************************************************/ void SetDWordCmdByCom(u32 *WRSource) { //--------------------------------------------------------------------------- parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; MBBuf.DataByte = 4; if(MBBuf.ByteNumber < MBBuf.DataByte) { MBBuf.ByteNumber = 0; return; } //--------------------------------------------------------------------------- MBBuf.BusError = ACKNOWLEDGE; if((securityID != 0x0000AA55) && (securityID != COM_PASSWORD1) && (securityID != COM_PASSWORD2)) return; MBBuf.BusError = 0; //V2004 IDvalidTime = 0; //--------------------------------------------------------------------------- parameterState = NEED_OPERATION; //MBBuf.BusError = 0; //--------------------------------------------------------------------------- tempL.Byte[3] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[2] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; *WRSource = tempL.DWord; } /******************************************************************************/ void FreeSetDWordCmdByCom(u32 *WRSource) { MBBuf.DataByte = 4; if(MBBuf.ByteNumber < MBBuf.DataByte) { MBBuf.ByteNumber = 0; //--------------------------------------------------------------------------- parameterState = ABORD_OPERATION; MBBuf.BusError = ILLEGAL_DATA_VALUE; //--------------------------------------------------------------------------- return; } //--------------------------------------------------------------------------- parameterState = NEED_OPERATION; MBBuf.BusError = 0; //--------------------------------------------------------------------------- tempL.Byte[3] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[2] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; *WRSource = tempL.DWord; return; } /******************************************************************************/ //保存内部采样数据 void WriteMeterBasicData(void) { u16 I,J=0; unsigned char temp[ANX*2]; for(I=0; I> 8; } WriteMultiByteToEEPROM(BASIC_DATA_BASE, temp, BASIC_DATA_BYTE_MAX, PARA_EEPROM); } /**************************************************************************************************************************/ void WriteWordByComToFlash(u16* word,u8 WriteParams) { tempL.Byte[1] = MBBuf.RxPointer[MBBuf.Index++]; tempL.Byte[0] = MBBuf.RxPointer[MBBuf.Index++]; *word = tempL.Word[0]; MBBuf.BusError = 0; munWriteParams = WriteParams; //指定将要写入的Flash区域 if(mucSaveParams != PARAMS_ING) mucSaveParams = PARAMS_READY;//word 将会在更新Flash时被装填 }