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/19 13:51] – laurenceb | code:i2c_eeprom [2009/03/20 04:50] (current) – laurenceb | ||
---|---|---|---|
Line 16: | Line 16: | ||
void i2cstart() | void i2cstart() | ||
{ | { | ||
+ | u16 timeout=1; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
+ | timeout++; //wait for a start to be transmitted | ||
+ | if(!timeout) | ||
+ | I2Cerr|=16; | ||
TWCR = (1<< | TWCR = (1<< | ||
if (((TWSR & 0xF8) != TW_START)&& | if (((TWSR & 0xF8) != TW_START)&& | ||
Line 25: | Line 29: | ||
void i2cstop() | void i2cstop() | ||
{ | { | ||
+ | u16 timeout=1; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
+ | timeout++; | ||
+ | if(!timeout) | ||
+ | I2Cerr|=64; | ||
} | } | ||
void i2cwrite(u08 c) | void i2cwrite(u08 c) | ||
{ | { | ||
+ | u16 timeout=1; | ||
TWDR = c; | TWDR = c; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
+ | timeout++; | ||
+ | if(!timeout) | ||
+ | I2Cerr|=32; | ||
if ( ((TWSR& | if ( ((TWSR& | ||
I2Cerr|=2; | I2Cerr|=2; | ||
} | } | ||
- | u08 i2cread(u08 | + | u08 i2cread(u08 |
{ | { | ||
u16 timeout=1; | u16 timeout=1; | ||
- | TWCR=AK; | + | TWCR=ak; |
- | while (!(TWCR & (1<< | + | while |
timeout++; | timeout++; | ||
if(!timeout) | if(!timeout) | ||
- | I2Cerr|=8; | + | I2Cerr|=8; |
if(((TWSR & 0xF8) !=TW_MR_DATA_ACK) && ((TWSR & 0xF8) !=TW_MR_DATA_NACK)) | if(((TWSR & 0xF8) !=TW_MR_DATA_ACK) && ((TWSR & 0xF8) !=TW_MR_DATA_NACK)) | ||
I2Cerr|=4; | I2Cerr|=4; | ||
Line 57: | Line 69: | ||
{ | { | ||
i2cstart(); | i2cstart(); | ||
- | i2cwrite(SLA_W|(((*address)>> | + | i2cwrite(SLA_W|(((*address)>> |
i2cwrite((u08)((*address)>> | i2cwrite((u08)((*address)>> | ||
i2cwrite((u08)(*address)); | i2cwrite((u08)(*address)); | ||
Line 67: | Line 79: | ||
for(n=0; | for(n=0; | ||
{ | { | ||
- | if(!(((u08)*address)&0xEF)) //check the address for overflow for the 7 LSB | + | if(!((u08)*address)) //check the address for overflow for the 8 LSB (Atmel) |
{ | { | ||
i2cstop(); | i2cstop(); | ||
- | _delay_loop_2( (u16) ( (float)F_CPU*0.003/4.0 ) );// | + | _delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );// |
set_address(address); | set_address(address); | ||
} | } | ||
Line 82: | 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)++; | ||
- | if(!(u16)*address) //we reached the end of the block | + | /*if(!(u16)(*address)) //we reached the end of the block |
{ | { | ||
i2cstop(); | i2cstop(); | ||
set_address(address); | set_address(address); | ||
- | } | + | }This isn't needed on the Atmel chips*/ |
} | } | ||
- | destination[datasize+1]=i2cread(_NAK_); | + | destination[datasize]=i2cread(_NAK_); |
+ | i2cstop(); | ||
+ | (*address)++; | ||
} | } | ||
+ | |||
u32 findtop() // | u32 findtop() // | ||
{ | { | ||
- | u32 top=0; //half way through rounded to nearest | + | u32 top=12*((u32)1<< |
- | u08 n; | + | u08 n, |
- | for(n=17;n;) //start by probing half way through | + | u16 place=1<< |
+ | for(n=14;endcond;) //start by probing half way through | ||
{ | { | ||
- | n--; | ||
- | top+=1<< | ||
- | top = 12*( (u32) (top/12) ); | ||
set_address(& | set_address(& | ||
+ | if(n) | ||
+ | n--; | ||
+ | else | ||
+ | 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 |
+ | if(n) | ||
+ | place|=(1<< | ||
+ | i2cstop(); | ||
+ | if(place> | ||
+ | place=10922; | ||
+ | top = 12*( (u32)place ); | ||
} | } | ||
- | top =12*( (u32) (top/12) ); //make sure our answer is rounded correctly | + | set_address(& |
+ | i2cstart(); | ||
+ | i2cwrite(SLA_R); | ||
+ | if(i2cread(_NAK_)!=MAGIC_NUMBER) //check the location to make sure its blank | ||
+ | place++; | ||
+ | top = 12*( (u32)place ); | ||
return top; | return top; | ||
} | } | ||
Line 117: | Line 147: | ||
u08 magic=MAGIC_NUMBER; | u08 magic=MAGIC_NUMBER; | ||
u32 n=0; | u32 n=0; | ||
- | for(n=0; | + | for(; |
{ | { | ||
- | set_address(& | + | /* |
- | write_data(& | + | set_address(& |
+ | i2cwrite(magic); //write the data | ||
+ | if((u08)n> | ||
+ | { | ||
+ | i2cstop(); | ||
+ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );//delay 6 ms for page write | ||
+ | }*/ | ||
+ | if(!((u08)n)) // | ||
+ | { | ||
+ | i2cstop(); | ||
+ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
+ | set_address(& | ||
+ | } | ||
+ | i2cwrite(magic); | ||
+ | wdt_reset(); | ||
} | } | ||
+ | i2cstop(); | ||
} | } | ||
#endif | #endif |
code/i2c_eeprom.1227102708.txt.gz · Last modified: 2008/11/19 13:51 by laurenceb