===== I2Cmem.c =====
#include "i2cmem.h"
#ifdef EEPROM
//----------General I2C functions-------------------------------------------------------------------------
volatile u08 I2Cerr;
void init_i2c()
{
TWBR=(u08)(((float)F_CPU/(2.0*(float)SCL))-8.0);//sets the correct clock rate defined as SCL
} //we dont need to enable the hardware - its done in write
void i2cstart()
{
u16 timeout=1;
TWCR = (1<>15)&0x02)); //the block
i2cwrite((u08)((*address)>>8)); //address write MSB
i2cwrite((u08)(*address)); //address write LSB
}
void write_data(u08 * c, u08 datasize, u32 * address)//The address will be incremented as the data is written
{
u08 n; //up to 256 bytes can be written at once
for(n=0;n>15)&0x02)); //set read to the correct block
for(n=0;n10922)
place=10922;
top = 12*( (u32)place ); //place is in units of 12
}
set_address(&top);
i2cstart();
i2cwrite(SLA_R);
if(i2cread(_NAK_)!=MAGIC_NUMBER) //check the location to make sure its blank
place++;
top = 12*( (u32)place );
return top;
}
void painteeprom() //Paints every 12th location with the magic number
{
u08 magic=MAGIC_NUMBER; //magic is our painting value
u32 n=0;
for(;n<131072;n++/*n+=12*/) //this writes every (12th) byte
{
/*
set_address(&n);
i2cwrite(magic); //write the data
if((u08)n>243) //our next n will be across a page boundary
{
i2cstop();
_delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );//delay 6 ms for page write
}*/
if(!((u08)n)) //this code is for writing every byte
{
i2cstop();
_delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );//delay 6 ms for page write
set_address(&n);
}
i2cwrite(magic);
wdt_reset();
}
i2cstop();
}
#endif
===== I2Cmem.h =====
#include //I2C registers
#include //wait delay loop for page write
#include "avrlibtypes.h"
#define SLA_W 0b10100000 //microchip 24LC515
#define SLA_R 0b10100001
#define SCL 100000
#define _AK_ (1<