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

📄 eeprom.c

📁 硬件I2C通信程序。。。 大家参考一下
💻 C
📖 第 1 页 / 共 2 页
字号:
## description: write N byte datas to the 24c02
*************************************************************************************************************************/
UCHAR WriteNByteTo24c02(UCHAR Address,UCHAR *Data,UCHAR Num)
{
	UCHAR _i=0;
	I2C_Start();
	do
	{  
		_i++;
		if(_i>10)
			return (FAIL);
		I2C_SendByte(Write24c02); 	
	}while(bACK==0);            
	I2C_SendByte(Address);
   	if(bACK==0)
    	return(FAIL);	
	for(_i=0;_i<Num;_i++)
	{
		I2C_SendByte(*(Data+_i));
		if(bACK==0)
			return (FAIL);
	}
	I2C_Stop();
}
/************************************************************************************************************************
## function name: ReadNByteFrom24c02()	
## parameter :
			Address: the address to read ; 
			Data: the pointer to the received data ; 
			Num: the number of data to read
## return  : the state  1 : success
					   0 : fail
## description: read N byte datas to the 24c02
*************************************************************************************************************************/
UCHAR ReadNByteFrom24c02(UCHAR Address,UCHAR *Data,UCHAR Num)
{
	UCHAR _i=0;
	I2C_Start();
	do
	{  
		_i++;
		if(_i>10)
			return (FAIL);
		I2C_SendByte(Write24c02); 	
	}while(bACK==0);            
	I2C_SendByte(Address);
   	if(bACK==0)
    	return(FAIL);	
	I2C_Start();
	I2C_SendByte(Read24c02);
   	if(bACK==0)
    	return(FAIL);	
	for(_i=0;_i<Num-1;_i++)
	{
		*(Data+_i) = I2C_ReadByte();
		ACK_To_I2c(0);
	}
	*(Data+_i) = I2C_ReadByte();
	ACK_To_I2c(0);
	I2C_Stop();
	return (SUCCESS);
}
/************************************************************************************************************************
## function name: ReadParas()	
## parameter :			
## return  : 1:success    0:fail
## description: read parameters from eeprom
*************************************************************************************************************************/

UCHAR StorePara(UCHAR Addr,UCHAR Data)
{
	 UCHAR _temp,_readback,_i;

	WDTE=0xAC;

	for(_i=0;_i<10;_i++)
	{
		WriteByteTo24c02(Addr,Data);
		delayI(5000);
		ReadByteFrom24c02(Addr, &_readback);
		if(_readback == Data)
			break;
	}

	if(_i==10)
		return (FAIL);
	else
		return (SUCCESS);
}
/************************************************************************************************************************
## function name: ReadParas()	
## parameter :			
## return  : 1:success    0:fail
## description: read parameters from eeprom
*************************************************************************************************************************/

UCHAR ReadParas()
{
	UCHAR _i,_j;
	UCHAR _flag=1;
	UCHAR _temp[2];

	

	_flag &= ReadNByteFrom24c02(SPARA_START_ADDR, ucSParas, SPARANUM);
	_flag &= ReadNByteFrom24c02(RPARA_START_ADDR, ucRParas, RPARANUM);
	_flag &= ReadNByteFrom24c02(WPARA_START_ADDR, ucFParas, WPARANUM);
	_flag &= ReadNByteFrom24c02(FPARA_START_ADDR, ucWParas, FPARANUM);
	
	_flag &= ReadNByteFrom24c02(METER_NAME_ADDR, ucMeterName, NAME_LEN);

	_flag &= ReadNByteFrom24c02(SPEED_RATE ,_temp,2);
	uiPulsePerMile =  (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadByteFrom24c02(PULSE_PER_R,&ucPulsePerRotation);

	_flag &= ReadByteFrom24c02(VERSION,&ucversion);

	_flag &= ReadNByteFrom24c02(F_WARN_HIGH_ADDR,_temp,2);
	uiFuelWarnHigh = (_temp[1]<<8)&0xff00|_temp[0];
	_flag &= ReadNByteFrom24c02(F_WARN_LOW_ADDR,_temp,2);
	uiFuelWarnLow =  (_temp[1]<<8)&0xff00|_temp[0];
	_flag &= ReadNByteFrom24c02(W_WARN_HIGH_ADDR, _temp,2);
	uiWaterWarnHigh =  (_temp[1]<<8)&0xff00|_temp[0];
	_flag &= ReadNByteFrom24c02(W_WARN_LOW_ADDR,_temp,2);
	uiWaterWarnLow =  (_temp[1]<<8)&0xff00|_temp[0];

//   read the Fuel resistor 

	_flag &= ReadNByteFrom24c02(FUEL1_R,_temp,2);
	Fuel_resistor[0]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL2_R,_temp,2);
	Fuel_resistor[1]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL3_R,_temp,2);
	Fuel_resistor[2]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL4_R,_temp,2);
	Fuel_resistor[3]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL5_R,_temp,2);
	Fuel_resistor[4]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL6_R,_temp,2);
	Fuel_resistor[5]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(FUEL7_R,_temp,2);
	Fuel_resistor[6]= (_temp[1]<<8)&0xff00|_temp[0];

// read water resistor

	_flag &= ReadNByteFrom24c02(WATER1_R,_temp,2);
	Temp_resistor[0]= (_temp[1]<<8)&0xff00|_temp[0];	

	_flag &= ReadNByteFrom24c02(WATER2_R,_temp,2);
	Temp_resistor[1]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(WATER3_R,_temp,2);
	Temp_resistor[2]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(WATER4_R,_temp,2);
	Temp_resistor[3]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(WATER5_R,_temp,2);
	Temp_resistor[4]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(WATER6_R,_temp,2);
	Temp_resistor[5]= (_temp[1]<<8)&0xff00|_temp[0];

	_flag &= ReadNByteFrom24c02(WATER7_R,_temp,2);
	Temp_resistor[6]= (_temp[1]<<8)&0xff00|_temp[0];

	
//   caculate the speed motor step table;

	Speed_anglestep[0]=ucSParas[0];

	for(_i=1;_i<SPARANUM;_i++)
	{
		if(Speed_anglestep[_i]>0)
			Speed_anglestep[_i]=Speed_anglestep[_i-1]+ucSParas[_i]*180;
		else
		{
			_j=_i-1;
			while(_i<SPARANUM)
			{
				Speed_anglestep[_i]=Speed_anglestep[_j]+180*5;
				_i++;
			}
			break;
		}
	}

	for(_i=0;_i<SPARANUM;_i++)
	{
		Speed_Freq[_i]= uiPulsePerMile*_i*5/9;     // *_i*20*100/3600
	}

//   caculate the RPM motor step table;

	Rpm_anglestep[0] = ucRParas[0];
	for(_i=1;_i<RPARANUM;_i++)
	{
		if(Rpm_anglestep[_i]>0)
			Rpm_anglestep[_i]=Rpm_anglestep[_i-1]+ucRParas[_i]*180;
		else
		{
			_j=_i-1;
			while(_i<RPARANUM)
			{
				Rpm_anglestep[_i]=Rpm_anglestep[_j]+180*5;
				_i++;
			}
			break;
		}
	}

	for(_i=0;_i<RPARANUM;_i++)
	{
		Rpm_Freq[_i] = ucPulsePerRotation *_i* 500/ 3;
	}

//   caculate the water motor step table;

	Temp_anglestep[0] = ucWParas[0];
	
	for(_i=1;_i<FPARANUM;_i++)
	{
		if(Temp_anglestep[_i]>0)
			Temp_anglestep[_i]=Temp_anglestep[_i-1]+ucWParas[_i]*180;
		else
		{
			_j=_i-1;
			while(_i<FPARANUM)
			{
				Temp_anglestep[_i]=Temp_anglestep[_j]+1640;
				_i++;
			}
			break;
		}
	}

//   caculate the Fuel  motor step table;

	Fuel_anglestep[0] = ucFParas[0];
	
	for(_i=1;_i<FPARANUM;_i++)
	{
		if(Fuel_anglestep[_i]>0)
			Fuel_anglestep[_i]=Fuel_anglestep[_i-1]+ucFParas[_i]*180;
		else
		{
			_j=_i-1;
			while(_i<FPARANUM)
			{
				Fuel_anglestep[_i]=Fuel_anglestep[_j]+1640;
				_i++;
			}
			break;
		}
	}
	return _flag;
} 
/************************************************************************************************************************
## function name: InitEEPROM()	
## parameter :			
## return  : 
## description: initialize the EEPROM()
*************************************************************************************************************************/

void InitEEPROM()
{
	UCHAR _i,_k=0,_j=0,_data;
//	I2C_Init();
	for(_i=0;_i<5;_i++)
	{
		ReadByteFrom24c02(0xff, &_data);
		if(_data==0x55)
			_k++;
		ReadByteFrom24c02(0xfE, &_data);
		if(_data==0xAA)
			_j++;
		I2C_Delay();
	}
	if(_k<3)
	{

		for(_i=0;_i<250;_i++)
		{
			WriteByteTo24c02(_i, 0x00);
			delayI(5000);
		}

		WriteByteTo24c02(0xff, 0x55);
		delayI(5000);
	}
	if(_j>3)
		ReadParas();

}
/************************************************ End of File*****************************************************************************************/
/************************************************ End of File*****************************************************************************************/

⌨️ 快捷键说明

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