⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom.c

📁 基于单片机的多费率电能表源程序
💻 C
字号:
#define EEPROM_GLOBALS
#include "includes.h"
const struct
{
	INT16U EepromAddress;
	INT8U DataLength;
	INT8U Property;	//b7~b6:00:表示初始化数据是单一数据 01:表示初始化数据非单一数据,但初始化数据简单10:表示初始化数据非单一数据,但初始化数据复杂
	INT8U InitValue;//如果初始化值是单一数据,则本身就是初始化数据,否则为初始化数据在初始化常数表中的位置。
	INT8U RamInitProduceID;//处理过程标识
}EepromDataTable[]=
{
	{EEPROM_INIT_SIGN0			,14,0x00,0x55,0x00	},
	{EEPROM_INIT_SIGN1			,14,0x00,0x55,0x00	},
	{EEPROM_INIT_SIGN2			,14,0x00,0x55,0x00	},
	{METER_NUMBER            	,6,0x00,0x00,0x00	},
	{USER_NUMBER         		,6,0x00,0x00,0x01	},
	{LAST_PROGRAM_TIME       	,4,0x00,0x00,0x00	},
	{TOTAL_PROGRAM_TIMES     	,2,0x00,0x00,0x0c	},
	{P_METER_CONST				,3,0x40,0,0x00		},
	{LAST_POWER_REVERSE_TIME	,4,0x00,0x00,0x00	},
	{POWER_REVERSE_TOTALTIME	,3,0x00,0x00,0x00	},
	{DISPLAY_SELECT				,3,0x40,3,0x02		},
	{MONTH_STORE_DATE			,2,0x40,6,0x03		},
	{CLEAR_ZERO_PASSWORD		,4,0x40,8,0x04		},
	{PROGRAM_PASSWORD_ADDRESS	,4,0x40,12,0x05		},	
	{REVERSE_TOTAL_ENERGY		,4,0x00,0x00,0x06	},
	{LAST_POWERDOWN_TIME		,4,0x00,0x00,0x00	},	
	{TIMESLICETABLE 			,37,0x80,16,0x08	},
	{CURRENT_ENERGY_POINTER		,1,0x00,0x00,0x09	},
	{ACTIVE_ENERGY0				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY1				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY2				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY3				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY4				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY5				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY6				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY7				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY8				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY9				,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY10			,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY11			,12,0x00,0x00,0x0a	},
	{ACTIVE_ENERGY12			,12,0x00,0x00,0x0a	},
	{BROAD_CALIBRATE_DATE		,4,0x00,0x00,0x07	},
	{ENERGY_PULSE				,4,0x00,0x00,0x0b	}		
};                      
const INT8U DataOfInit[]={0x00,0x16,0x00,0x10,0xff,0xff,0x00,0x01,0x00,0x56,0x34,0x12,0x01,0x56,0x34,0x12,0x02,0x03,0x00,0x07,0x04,0x00,0x22}; 
void InitEepromData(INT8U DataId)
{
	INT8U i;
	INT8U TempLength;
	TempLength=EepromDataTable[DataId].DataLength;
	if((EepromDataTable[DataId].Property&0xc0)==0x40)
	{
		for(i=0;i<TempLength;i++)
			EepromReadWriteBuf[i]=DataOfInit[EepromDataTable[DataId].InitValue+i];		
	}
	else	if((EepromDataTable[DataId].Property&0xc0)==0x00)
			{
				for(i=0;i<TempLength;i++)
					EepromReadWriteBuf[i]=EepromDataTable[DataId].InitValue;	
			}
			else
			{
				for(i=0;i<7;i++)
					EepromReadWriteBuf[i]=DataOfInit[EepromDataTable[DataId].InitValue+i];	
				for(i=7;i<TempLength;i++)
					EepromReadWriteBuf[i]=0x00;
			}	
	EepromReadWriteBuf[TempLength]=AddCRC(EepromReadWriteBuf,TempLength);
	TempLength++;
	EepromReadWriteBuf[TempLength]=CalculateXOR(EepromReadWriteBuf,TempLength);
	TempLength++;
	WriteEeprom(EepromReadWriteBuf,EepromDataTable[DataId].EepromAddress,TempLength,16);
	WriteEeprom(EepromReadWriteBuf,EepromDataTable[DataId].EepromAddress+DATA_BAK_OFF,TempLength,16);
}
INT8U EepromTest(void)
{
	INT8U i,j,Sign;
	for(i=0;i<16;i++)
		TempBufOfMain[i]=i;
	for(i=0;i<3;i++)
	{
		WriteEeprom(TempBufOfMain,EEPROM_TEST,16,16);
		ReadEeprom(EEPROM_TEST,TempBufOfMain,16);
		for(j=0;j<16;j++)
		{
			Sign=1;
			if(TempBufOfMain[j]!=j)
			{
				Sign=0;
				break;
			}
		}
		if(Sign)
		{
			return 1;
		}
	}
	return 0;
} 
void InitRamData(INT8U DataId)
{
	INT16U TempVar;
	INT8U *Pnt;
	INT8U i;
	switch(EepromDataTable[DataId].RamInitProduceID)
	{
		case 0:			
			break;
		case 1:
			for(i=0;i<6;i++)
				UserNumber[i]=EepromReadWriteBuf[i];
			break;
		case 2:
			for(i=0;i<3;i++)
				DisplaySelect[i]=EepromReadWriteBuf[i];		
			break;
		case 3:
			MonthStoreDate[0]=EepromReadWriteBuf[0];
			MonthStoreDate[1]=EepromReadWriteBuf[1];
			break;	
		case 4:
			for(i=0;i<4;i++)
				Clear0PassWord[i]=EepromReadWriteBuf[i];
			break;
		case 5:
			for(i=0;i<4;i++)
				ProgramPassWord[i]=EepromReadWriteBuf[i];
			break;
		case 6:
			for(i=0;i<4;i++)
				EnergyData.ActiveReverseEnergy[i]=EepromReadWriteBuf[i];
		case 7://必须处理广播校时允许
			if(IsBroadCalibrateTimePermit())
				_BroadCalibrateTimeAllow_=1;
			else
				_BroadCalibrateTimeAllow_=0;
			break;
		case 8:
			Pnt=&TimeSliceInfo.TimeSliceNum;
			for(i=0;i<37;i++)
			{
				*Pnt=EepromReadWriteBuf[i];	
				Pnt++;
			}	
			break;
		case 9:
			EnergyData.EnergyPointer=EepromReadWriteBuf[0];
			break;
		case 0x0a:
			if(EnergyData.EnergyPointer==(DataId-ACTIVE_ENERGY_ID0))
			{
				Pnt=&EnergyData.ActiveRate1Energy[0];
				for(i=0;i<12;i++)
				{
					*Pnt=EepromReadWriteBuf[i];	
					Pnt++;
				}
			}
			break;
		case 0x0b:			
			PulseSample.PulseNum[0]=EepromReadWriteBuf[0];
			PulseSample.PulseNum[1]=EepromReadWriteBuf[1];
			PulseSample.PulseNum[2]=EepromReadWriteBuf[2];
			PulseSample.PulseNum[3]=EepromReadWriteBuf[3];
			break;
		case 0x0c:
			ProgramTimes[0]=EepromReadWriteBuf[0];
			ProgramTimes[1]=EepromReadWriteBuf[1];
			break;
		case 0x0d:
			ReverseTotalTime[0]=EepromReadWriteBuf[0];
			ReverseTotalTime[1]=EepromReadWriteBuf[1];
			ReverseTotalTime[2]=EepromReadWriteBuf[2];
			break;
		default:
			break;	
	}
}
/***********************返回值为0表示读数据失败*****************************/
INT8U ReadEepromData(INT8U DataId,INT8U *DestinationAddress)   
{
	INT8U i;
	INT8U TempLength;
	TempLength=EepromDataTable[DataId].DataLength+2;
	ReadI2cDevice(EEPROM_DEVICE_ADDR,EepromDataTable[DataId].EepromAddress,EepromReadWriteBuf,TempLength);
	if((EepromReadWriteBuf[TempLength-2]!=AddCRC(EepromReadWriteBuf,TempLength-2))||(CalculateXOR(EepromReadWriteBuf,TempLength)))
	{
		ReadI2cDevice(EEPROM_DEVICE_ADDR,EepromDataTable[DataId].EepromAddress+DATA_BAK_OFF,EepromReadWriteBuf,TempLength);
		if((EepromReadWriteBuf[TempLength-2]!=AddCRC(EepromReadWriteBuf,TempLength-2))||(CalculateXOR(EepromReadWriteBuf,TempLength)))
			return 0;
		else
		{
			WriteEeprom(EepromReadWriteBuf,EepromDataTable[DataId].EepromAddress,TempLength,16);			
		}
	}
	for(i=0;i<TempLength;i++)
	{
		*DestinationAddress=EepromReadWriteBuf[i];
		DestinationAddress++;
	}
	return 1;	
}   

void WriteEepromData(INT8U DataId,INT8U *SourceAddress)   
{
	INT8U i;
	INT8U TempLength;
	TempLength=EepromDataTable[DataId].DataLength;
	for(i=0;i<TempLength;i++)
	{
		EepromReadWriteBuf[i]=*SourceAddress;
		SourceAddress++;
	}
	EepromReadWriteBuf[TempLength]=AddCRC(EepromReadWriteBuf,TempLength);
	TempLength++;
	EepromReadWriteBuf[TempLength]=CalculateXOR(EepromReadWriteBuf,TempLength);
	TempLength++;
	WriteEeprom(EepromReadWriteBuf,EepromDataTable[DataId].EepromAddress,TempLength,16);
	WriteEeprom(EepromReadWriteBuf,EepromDataTable[DataId].EepromAddress+DATA_BAK_OFF,TempLength,16);		
}     
void WriteEeprom(INT8U *SourceAddress,INT16U DestinationAddress,INT8U Length,INT8U PageByteNum)
{       
	
	EEPROM_WP_ENABLE;
	nop();
	nop();	
	nop();
	nop();
	nop();
	nop();
	while(Length>PageByteNum)
	{
		WriteI2cDevice(EEPROM_DEVICE_ADDR,SourceAddress,DestinationAddress,PageByteNum);
		Length-=PageByteNum;
		SourceAddress+=PageByteNum;
		DestinationAddress+=PageByteNum;
		DelayXms(10);
	}
	WriteI2cDevice(EEPROM_DEVICE_ADDR,SourceAddress,DestinationAddress,Length);
	DelayXms(10);
	nop();
	nop();
	nop();
	nop();
	nop();
	nop();
	EEPROM_WP_DISABLE;		
}
void ReadEeprom(INT16U SourceAddress,INT8U *DestinationAddress,INT8U Length)
{
	ReadI2cDevice(EEPROM_DEVICE_ADDR,SourceAddress,DestinationAddress,Length);	
}
void InitNewEeprom(void)
{
	INT8U i;
	for(i=METER_NUMBER_ID;i<ENERGY_PULSE_ID+1;i++)
	{
		ClearWatchDog;
		InitEepromData(i);
		InitRamData(i);
	}	
	for(i=EEPROM_INIT_SIGN_ID0;i<EEPROM_INIT_SIGN_ID2+1;i++)
	{
		ClearWatchDog;
		InitEepromData(i);
		InitRamData(i);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -