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

📄 eeprom.c

📁 M37546产品群智能水表的解决方案,请需要的朋友下载
💻 C
字号:
/************************************************************************/
/* Renesas System Solutions (Beijing) Co., LTD.							*/
/* All rights reserved.													*/
/*																		*/
/* File Name		: EEPROM.c											*/
/* Description		: 													*/
/*																		*/
/* Current Version	: 1.0												*/
/* Designer			: Feng Jun											*/
/* Start Date		: 2006-11-3											*/
/* Complete Date	: 2006-11-22										*/
/*																		*/
/* Lastest Version	:													*/
/* Designer			:													*/
/* Start Date		: 													*/
/* Complete Date	: 													*/
/************************************************************************/
#include "EEPROM.h"
#include "main.h"

char eeprombuffer[32];


#define DELAY_TIME 2 /* Delay time */
#define TRUE 1
#define FALSE 0


/*************************************************************************
Name: 			EEPROMCoding
Parameters: 	None
Returns: 		None
Description: 	EEPROMCoding
*************************************************************************/
char EEPROMCoding(char *buffer, char n)
{
	int i;
	for (i=0; i<n; i++)
	{
		buffer[i] ^= 0xa5;
	}
}

/*************************************************************************
Name: 			EEPROMDecode
Parameters: 	None
Returns: 		None
Description: 	EEPROMDecode
*************************************************************************/
char EEPROMDecode(char *buffer, char n)
{
	int i;
	for (i=0; i<n; i++)
	{
		buffer[i] ^= 0xa5;
	}
}

/*************************************************************************
Name: 			DELAY
Parameters: 	None
Returns: 		None
Description: 	Delay time
*************************************************************************/
void DELAY(unsigned int t) /* Delay */
{
	while(t != 0)
	t--;
}

/*************************************************************************
Name: 			EEPROMI2CStart
Parameters: 	None
Returns: 		None
Description: 	START CONDITION: A high-to-low transition of io_EEPSDA with 
				io_EEPSCL high is a start condition which must precede any
				other command.
*************************************************************************/
void EEPROMI2CStart(void)
{
	io_EEPSDA = 1;
	io_EEPSCL = 1;
	DELAY(DELAY_TIME);
	io_EEPSDA = 0;
	DELAY(DELAY_TIME);
	io_EEPSCL = 0;
	DELAY(DELAY_TIME);
}

/*************************************************************************
Name: 			EEPROMI2CStop
Parameters: 	None
Returns: 		None
Description: 	STOP CONDITION: A low-to-high transition of io_EEPSDA with
				io_EEPSCL high is a stop condition. After a read sequence,
				the	stop command will place the E2PROM in a standby power mode.
*************************************************************************/
void EEPROMI2CStop(void)
{
	io_EEPSDA = 0;
	io_EEPSCL = 1;
	DELAY(DELAY_TIME);
	io_EEPSDA = 1;
	DELAY(DELAY_TIME);
	io_EEPSCL = 0;
	DELAY(DELAY_TIME);
}

/*************************************************************************
Name: 			EEPROMSend0
Parameters: 	None
Returns: 		None
Description: 	Send 0 to EEPROM, 
*************************************************************************/
void EEPROMSend0(void)   /* SEND ACK */
{
	io_EEPSDA = 0;
	io_EEPSCL = 1;
	DELAY(DELAY_TIME);
	io_EEPSCL = 0;
	DELAY(DELAY_TIME);
}

/*************************************************************************
Name: 			EEPROMSend1
Parameters: 	None
Returns: 		None
Description: 	Send 1 to EEPROM
*************************************************************************/
void EEPROMSend1(void)
{
	io_EEPSDA = 1;
	io_EEPSCL = 1;
	DELAY(DELAY_TIME);
	io_EEPSCL = 0;
	DELAY(DELAY_TIME);
}

/*************************************************************************
Name: 			EEPROMCheckAck
Parameters: 	None
Returns: 		None
Description: 	ACKNOWLEDGE: All addresses and data words are serially
				transmitted to and from the E2PROM in 8 bit words.
				The E2PROM sends a zero to acknowledge that it has received
				each word. This happens during the ninth clock cycle.
*************************************************************************/
char EEPROMCheckAck(void)
{
	bit F0;
	io_EEPSDA = 1;
	io_EEPSCL = 1;
	ioD_EEPSDA = 0;			/* Change io_EEPSDA input */
	DELAY(DELAY_TIME/2);
	F0 = io_EEPSDA;
	DELAY(DELAY_TIME/2);
	io_EEPSCL = 0;
	DELAY(DELAY_TIME);
	ioD_EEPSDA = 1;			/* Change io_EEPSDA output */

	if(F0 == 1)
	{
		return FALSE;
	}
	else return TRUE;
}

/*************************************************************************
Name: 			EEPROMWriteI2CByte
Parameters: 	b
Returns: 		none
Description: 	Write a byte to I2C
*************************************************************************/
void EEPROMWriteI2CByte(char b)
{
/*向I2C总线写一个字节*/
	char i;
	for(i=0; i<8; i++)
	if((b<<i) & 0x80)
	{
		EEPROMSend1();
	}
	else EEPROMSend0();
}

 

/*************************************************************************
Name: 			EEPROMReadI2CByte
Parameters: 	none
Returns: 		b
Description: 	Write a byte to I2C
*************************************************************************/
char EEPROMReadI2CByte(void)
{
/*从I2C总线读一个字节*/
	char b = 0;
	char i;
	
	ioD_EEPSDA = 0;		/* Change io_EEPSDA input */
	for(i=0; i<8; i++)
	{
		bit F0;
		io_EEPSCL = 1;		/* Get data */
		DELAY(DELAY_TIME);
		F0 = io_EEPSDA;
		DELAY(DELAY_TIME);
		io_EEPSCL = 0;
		if(F0 == 1)
		{
			b = b<<1;
			b = b|0x01;
		}
		else b = b<<1;
	}
	ioD_EEPSDA = 1;		/* Change io_EEPSDA output */
	return b;
}


/*************************************************************************
Name: 			EEPROMWriteOneByte
Parameters: 	addr:EEPROM address, data
Returns: 		none
Description: 	Write one byte to EEPROM
*************************************************************************/
//void EEPROMWriteOneByte(char addr, char data)
//{
//	unsigned char acktemp = 1;
//	char addr_bak = addr;						/* bakup addr */
//	char data_bak = data;					/* bakup data */
//	
//	io_EEPWP = 0;
//	
//	EEPROMCoding(&data, 1);					/* Coding */
/*write a byte to mem*/
//	EEPROMI2CStart();
//	EEPROMWriteI2CByte(0xa0);
//	acktemp = EEPROMCheckAck();
//	EEPROMWriteI2CByte(addr);					/* address */
//	acktemp = EEPROMCheckAck();
//	EEPROMWriteI2CByte(data);				/* data */
//	acktemp = EEPROMCheckAck();
//	EEPROMI2CStop();
//	
//	io_EEPWP = 1;
//}

/*************************************************************************
Name: 			EEPROMWriteNBytes
Parameters: 	addr:EEPROM address, n:quantity of data, buffer
Returns: 		none
Description: 	Write one byte to EEPROM
*************************************************************************/
void EEPROMWriteNBytes(char addr, char n, char *buffer)
{
	char addrbak = addr;				/* Bakup parameters */
	char nbak = n;

	char i;
	char itimes = 0;					/* read times */
	char acktemp = 0;
	
	EEPROMCoding(buffer, n);			/* Coding */
	io_EEPWP = 0;

	while ( itimes<3 && acktemp==0 )	/* if there is an error, read 3 times */
	{
		addr = addrbak;					/* Review parameters */
		n = nbak;
		
		for (i=0; i<n; i++)
		{
			EEPROMI2CStart();
	
			EEPROMWriteI2CByte( 0xa0 );		/* Send command */
			acktemp = EEPROMCheckAck();
			EEPROMWriteI2CByte( addr );		/* Send address */
			acktemp = EEPROMCheckAck(); 
			EEPROMWriteI2CByte( buffer[i] );/* Send data */
			acktemp = EEPROMCheckAck();
		
			EEPROMI2CStop();
			DELAY(DELAY_TIME);
			addr++;
		}
		itimes++;
	}

	io_EEPWP = 1; 
	
	if (acktemp == 0)
	{
		B_erroreeprom = 1;				/* EEPROM error */
	}	
	
}

/*************************************************************************
Name: 			EEPROMWriteAPage
Parameters: 	none
Returns: 		none
Description: 	
*************************************************************************/
/*void EEPROMWriteAPage(char *buffer, char addr)
{
	char acktemp = 1;
	bit wrtmp;
	int i;
	
	EEPROMI2CStart();
	EEPROMWriteI2CByte(0xa0);
	acktemp = EEPROMCheckAck();
	EEPROMWriteI2CByte(addr);
	acktemp = EEPROMCheckAck();
	for(i=0; i<7; i++)
	{
		EEPROMWriteI2CByte(buffer[i]);
		if(!EEPROMCheckAck())
		{
			EEPROMI2CStop();
		}
	 }


	EEPROMI2CStop();
}
*/
/*************************************************************************
Name: 			EEPROMReadOneByte
Parameters: 	addr:EEPROM address, data
Returns: 		none
Description: 	Read one byte from EEPROM
*************************************************************************/
//char EEPROMReadOneByte(char addr)
//{
//	char acktemp = 1;
//	char mydata;
/*read a byte from mem*/
//	EEPROMI2CStart();
//	EEPROMWriteI2CByte(0xa0);
//	acktemp = EEPROMCheckAck();
//	EEPROMWriteI2CByte(addr);			/* address */
//	acktemp = EEPROMCheckAck();
//	EEPROMI2CStart();
//	EEPROMWriteI2CByte(0xa1);
//	acktemp = EEPROMCheckAck();
//
//
//	mydata = EEPROMReadI2CByte();
//	acktemp = EEPROMCheckAck();
//
//	EEPROMDecode(&mydata, 1);
//	EEPROMI2CStop();
//	return mydata;
//}

/*************************************************************************
Name: 			EEPROMReadNBytes
Parameters: 	none
Returns: 		none
Description: 	
*************************************************************************/
void EEPROMReadNBytes(char *buffer, char n, char addr)
{
	char nbak = n;
	char addrbak = addr;
	char acktemp = 0;
	char i;
	char itimes = 0;
	
	io_EEPWP = 0;

	while ( itimes<3 && acktemp==0 )	/* if there is an error, read 3 times */
	{
		addr = addrbak;					/* Review parameters */
		n = nbak;

		EEPROMI2CStart();
		EEPROMWriteI2CByte(0xa0);
		acktemp = EEPROMCheckAck();
		EEPROMWriteI2CByte(addr);			/* address */
		acktemp = EEPROMCheckAck();
		EEPROMI2CStart();
		EEPROMWriteI2CByte(0xa1);

		acktemp = EEPROMCheckAck();

		for(i=0; i<n; i++)
		{
			buffer[i] = EEPROMReadI2CByte();
			if(i != n-1)
			EEPROMSend0();			/* Send Acknowledge */
			else
			EEPROMSend1();			/* Send no acknowledge */
		}
		
		EEPROMI2CStop();
		itimes++;
	}

	EEPROMDecode(buffer, n);

	io_EEPWP = 1; 

	if (acktemp == 0)
	{
		B_erroreeprom = 1;				/* EEPROM error */
	}	
}

 


/*************************************************************************
Name: 			ReadInfoEEPROM
Parameters: 	None
Returns: 		None
Description: 	Read information of water meter in the EEPROM
*************************************************************************/
void ReadInfoEEPROM( void )
{
	int i;
/* test */
//	eeprombuffer[0] = 0x22;
//	eeprombuffer[1] = 0x11;
//	EEPROMWriteNBytes(0, 2, eeprombuffer);
/* test end */
	EEPROMReadNBytes(eeprombuffer, 1, 0);	/* Read status */
	
	if (eeprombuffer[0] != 0x68)
	{							/* The water meter was not set */
								/* The water meter was powered up for the first time */
		eeprombuffer[0] = 0x68;
		for (i=1; i<0x1c; i++)
		{
			eeprombuffer[i] = 0;
		}
		eeprombuffer[8] = 2;		/* Threshold of display warning */
		eeprombuffer[10] = 2;		/* Threshold of closing valve warning */
		eeprombuffer[12] = 3;		/* Limit of purchase */
		eeprombuffer[13] = 0xe8;
		
		EEPROMWriteNBytes(0, 0x1c, eeprombuffer);	/* Water meter initial */
	}
	
	else
	{
		EEPROMReadNBytes(eeprombuffer, 3, 0x13);	/* Read Surplus */
		if (io_valveopen != 0)
		{
			if (eeprombuffer[0]!=0 || eeprombuffer[1]!=0 || eeprombuffer[2]!=0)
			{						/* Surplus is not zero, but the valve is closed */
				B_openvalve = 1;	/* Open valve */
			}
		}
		
		if (io_valveclosed != 0)
		{
			if (eeprombuffer[0]==0 && eeprombuffer[1]==0 && eeprombuffer[2]==0)
			{						/* Sruplus is zero */
				B_openvalve = 1;	/* Close valve */
			}
		}	
	}

}

⌨️ 快捷键说明

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