AT24CXX EEPROM


This example demonstrates how to read/write I2C EEPROM's such as AT24C04 using ARM microcontroller. The example uses ReadAT24C04() and WriteAT24C04() functions from BiPOM Library.

Toolkit:ARM Development System

Location:/bipom/devtools/GCC/LPC2000/examples/at24c04

Code Example


I2C_ERRCODE  EEPROM_Test(void)
{
	
	I2C_ERRCODE ec;
	I2C_DATA value;
	UINT ndx;
	tprintf("\n\rAT24C04 TEST");
	//

	tprintf("\n\rWriting");
	for (ndx =0; ndx < EEPROM_SIZE	; ndx++)
	{
		ec = WriteAT24C04 ( ndx, ndx & 0xFF );
		if(ec)
		{
			tprintf( "\n\rERROR: WR ADDRESS %04xh",ndx);
			goto func_end;			
		}
		else
		{
			if(!(ndx %32)) tprintf(".",ndx);
		}	
		
		delayMs(10);
		
	}
	tprintf("OK");
	//

	tprintf("\n\rVerifying");
	for (ndx =0; ndx < EEPROM_SIZE	; ndx++)
	{
		ec = ReadAT24C04 ( ndx, &value );
		if(ec)
		{
			tprintf( "\n\rERROR: RD ADDRESS %04xh",ndx);
			goto func_end;			
		}
		else
		{
			if((value&0xFF) != (ndx & 0xFF))
			{
				tprintf( "\n\rERROR: VF ADDRESS %04xh",ndx);
				ec = ERROR;
				goto func_end;						
			}
			else
			{
				if(!(ndx %32)) tprintf(".",ndx);
			}	
		}	
	}
	tprintf("OK");
	//	

func_end:
	tprintf("\n\rAT24C04 TEST ");
	if (ec) tprintf("FAILED");
	else	tprintf("PASSED");	
	return ec;	
}

int main( void )
{
	char string[20];
	I2C_ERRCODE ec;
	I2C_DATA dataByte;	
	/* Initialize the system */
	Initialize();
	/* Repair I2C bus, software implementation */
	Soft_I2C_Repair();
	/* Initialize 100 KHz I2C bus, hardware implementation */
	I2C_Config(100000);
	
	IOCLR0 |=1<<31;
	IODIR0 |=1<<31;

	/* EEPROM Test */
	EEPROM_Test();
	/* Loop forever */
	for (;;){}
}