标签:eeprom i2c driver
EEprom有个特点写数据的时候要注意扇区的划分,读数据时就没有这个问题。
1. 1st :write bytes
/************************************** * send one byte to I2C for eep **************************************/ static int I2CByteTX(uint8_t byte) { uint8_t j; int ret = 0; SDA_OUTPUT(); usleep(4); for ( j = 0 ; j < 8 ; j++) { usleep(4); CLR_SCL(); usleep(2); if(byte & 0x80) { SET_SDA(); } else { CLR_SDA(); } usleep(2); SET_SCL(); byte <<= 1; } usleep(4); CLR_SCL(); usleep(2); SDA_INPUT(); usleep(2); SET_SCL(); usleep(2); if(TST_SDA()) { ret = -1; SET_SDA(); } else { CLR_SDA(); } SDA_OUTPUT(); usleep(2); CLR_SCL(); usleep(6); return(ret); }
2. 2nd read byte:
/************************************** * get one byte from I2C for eep **************************************/ static uint8_t I2CByteRX(uint8_t ack) { uint8_t byte = 0; uint8_t j; CLR_SCL(); usleep(4); SDA_INPUT(); usleep(4); for (j = 0 ; j < 8 ; j++) { byte <<= 1; CLR_SCL(); usleep(4); SET_SCL(); usleep(2); if(TST_SDA()) { byte |= 1; } usleep(2); } I2C_ACK(ack); return(byte); }
标签:eeprom i2c driver
原文地址:http://blog.csdn.net/linglongqiongge/article/details/46309635