要用GPIO模拟I2C,那么首先就得弄清楚I2C的时序。
1.I2C总线通信时序
2.起始与结束时序
//起始信号 uint8_t I2c_StartCondition() { uint8_t rv = NO_ERROR; SDA_OUT(); SCL_OUT(); /* StartCondition(S): A high to low transition on the SDA line while SCL is high. _______ SCL: |___ _____ SDA: |_____ */ SDA_H(); delay_us(2); SCL_H(); delay_us(2); SDA_L();//先于SCL前拉低 delay_us(2); SCL_L();//拉低SCL便于之后的操作 delay_us(2); return rv; }
//终止信号 uint8_t I2c_StopCondition(void) { uint8_t rv = NO_ERROR; SDA_OUT(); /* StopCondition(P): A low to high transition on the SDA line while SCL is high. ———————————— SCL: — — — _____ SDA: ___| */ SCL_L(); SDA_L();