code:i2c_eeprom
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:i2c_eeprom [2008/11/22 07:18] – laurenceb | code:i2c_eeprom [2009/03/20 04:50] (current) – laurenceb | ||
---|---|---|---|
Line 2: | Line 2: | ||
- | <code c> | + | <code c># |
- | #include " | + | |
#ifdef EEPROM | #ifdef EEPROM | ||
Line 12: | Line 11: | ||
void init_i2c() | void init_i2c() | ||
{ | { | ||
- | TWBR=(u08)(((float)F_CPU/ | + | TWBR=(u08)(((float)F_CPU/ |
- | } //we dont need to enable the hardware - its done in write | + | } //we dont need to enable the hardware - its done in write |
void i2cstart() | void i2cstart() | ||
Line 19: | Line 18: | ||
u16 timeout=1; | u16 timeout=1; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
timeout++; | timeout++; | ||
if(!timeout) | if(!timeout) | ||
I2Cerr|=16; | I2Cerr|=16; | ||
- | TWCR = (1<< | + | TWCR = (1<< |
if (((TWSR & 0xF8) != TW_START)&& | if (((TWSR & 0xF8) != TW_START)&& | ||
I2Cerr|=1; | I2Cerr|=1; | ||
Line 32: | Line 31: | ||
u16 timeout=1; | u16 timeout=1; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
timeout++; | timeout++; | ||
if(!timeout) | if(!timeout) | ||
Line 43: | Line 42: | ||
TWDR = c; | TWDR = c; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
timeout++; | timeout++; | ||
if(!timeout) | if(!timeout) | ||
Line 51: | Line 50: | ||
} | } | ||
- | u08 i2cread(u08 | + | u08 i2cread(u08 |
{ | { | ||
u16 timeout=1; | u16 timeout=1; | ||
- | TWCR=AK; | + | TWCR=ak; |
while ((!(TWCR & (1<< | while ((!(TWCR & (1<< | ||
timeout++; | timeout++; | ||
Line 70: | Line 69: | ||
{ | { | ||
i2cstart(); | i2cstart(); | ||
- | i2cwrite(SLA_W|(((*address)>> | + | i2cwrite(SLA_W|(((*address)>> |
i2cwrite((u08)((*address)>> | i2cwrite((u08)((*address)>> | ||
- | i2cwrite((u08)(*address)); | + | i2cwrite((u08)(*address)); |
} | } | ||
void write_data(u08 * c, u08 datasize, u32 * address)// | void write_data(u08 * c, u08 datasize, u32 * address)// | ||
{ | { | ||
- | u08 n; //up to 256 bytes can be written at once | + | u08 n; //up to 256 bytes can be written at once |
for(n=0; | for(n=0; | ||
{ | { | ||
- | if(!((u08)*address)) //check the address for overflow for the 8 LSB (Atmel) | + | if(!((u08)*address)) //check the address for overflow for the 8 LSB (Atmel) |
{ | { | ||
- | i2cstop(); | + | i2cstop(); |
_delay_loop_2( (u16) ( (float)F_CPU*0.006/ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
- | set_address(address); | + | set_address(address); |
} | } | ||
- | i2cwrite(c[n]); | + | i2cwrite(c[n]); |
} | } | ||
} | } | ||
Line 95: | Line 94: | ||
datasize--; | datasize--; | ||
i2cstart(); | i2cstart(); | ||
- | i2cwrite(SLA_R|(((*address)>> | + | i2cwrite(SLA_R|((u08)((*address)>> |
- | for(n=0; | + | for(n=0; |
{ | { | ||
destination[n]=i2cread(_AK_); | destination[n]=i2cread(_AK_); | ||
- | (*address)++; | + | (*address)++; |
- | / | + | / |
{ | { | ||
i2cstop(); | i2cstop(); | ||
Line 106: | Line 105: | ||
}This isn't needed on the Atmel chips*/ | }This isn't needed on the Atmel chips*/ | ||
} | } | ||
- | destination[datasize+1]=i2cread(_NAK_); | + | destination[datasize]=i2cread(_NAK_); |
i2cstop(); | i2cstop(); | ||
+ | (*address)++; | ||
} | } | ||
- | u32 findtop() //Find the address of record number where we enter painted eeprom | + | u32 findtop() //Find the address of record number where we enter painted eeprom |
{ | { | ||
- | u32 top=12*((u32)(((u32)1<< | + | u32 top=12*((u32)1<< |
- | u08 n; | + | u08 n,endcond=TRUE; |
- | u08 endcond=TRUE; | + | u16 place=1<< |
- | for(n=16;endcond;) //start by probing half way through | + | for(n=14;endcond;) //start by probing half way through |
{ | { | ||
- | set_address(& | + | set_address(& |
if(n) | if(n) | ||
- | n--; //need to decr here to get 1 byte resolution | + | n--; //need to decr here to get 1 byte resolution |
else | else | ||
- | endcond=FALSE; | + | endcond=FALSE; |
+ | i2cstart(); | ||
+ | i2cwrite(SLA_R); | ||
if(i2cread(_NAK_)==MAGIC_NUMBER) // | if(i2cread(_NAK_)==MAGIC_NUMBER) // | ||
- | top-=1<< | + | place&=~(1<<n); //clear the nth bit in the place holder |
- | else | + | if(n) |
- | top+=1<< | + | place|=(1<<(n-1)); //set the next bit |
i2cstop(); | i2cstop(); | ||
- | top = 12*( (u32) (top/ | + | if(place> |
+ | place=10922; | ||
+ | top = 12*( (u32)place ); //place is in units of 12 | ||
} | } | ||
+ | set_address(& | ||
+ | i2cstart(); | ||
+ | i2cwrite(SLA_R); | ||
+ | if(i2cread(_NAK_)!=MAGIC_NUMBER) // | ||
+ | place++; | ||
+ | top = 12*( (u32)place ); | ||
return top; | return top; | ||
} | } | ||
- | void painteeprom() //Paints every 12th location with the magic number | + | void painteeprom() //Paints every 12th location with the magic number |
{ | { | ||
- | u08 magic=MAGIC_NUMBER; | + | u08 magic=MAGIC_NUMBER; |
- | u32 n; | + | u32 n=0; |
- | for(n=0; | + | for(; |
{ | { | ||
+ | /* | ||
set_address(& | set_address(& | ||
- | i2cwrite(magic); | + | i2cwrite(magic); |
- | if((u08)n> | + | if((u08)n> |
{ | { | ||
i2cstop(); | i2cstop(); | ||
_delay_loop_2( (u16) ( (float)F_CPU*0.006/ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
+ | }*/ | ||
+ | if(!((u08)n)) // | ||
+ | { | ||
+ | i2cstop(); | ||
+ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
+ | set_address(& | ||
} | } | ||
+ | i2cwrite(magic); | ||
wdt_reset(); | wdt_reset(); | ||
} | } | ||
+ | i2cstop(); | ||
} | } | ||
#endif | #endif | ||
- | |||
</ | </ | ||
code/i2c_eeprom.1227338300.txt.gz · Last modified: 2008/11/22 07:18 by laurenceb