unsigned char i2c_read_bit(void) { unsigned char bit; read_SDA(); I2C_delay(); while (read_SCL() == 0); // Clock stretching bit = read_SDA(); I2C_delay(); clear_SCL(); I2C_delay(); return bit; }
void i2c_stop(void) { // set SDA to 0 clear_SDA(); I2C_delay(); while (read_SCL() == 0); // Clock stretching I2C_delay(); if (read_SDA() == 0) { arbitration_lost(); } I2C_delay(); started = 0; }
// Read a bit from I2C bus bool i2c_read_bit(void) { bool bit; // Let the slave drive data read_SDA(); I2C_delay(); while (read_SCL() == 0) { // Clock stretching // You should add timeout to this loop } // SCL is high, now data is valid bit = read_SDA(); I2C_delay(); clear_SCL(); return bit; }
void i2c_write_bit(unsigned char bit) { if (bit) { read_SDA(); } else { clear_SDA(); } I2C_delay(); while (read_SCL() == 0); // Clock stretching if (bit && read_SDA() == 0) { //Houve problema de comunicação } I2C_delay(); clear_SCL(); I2C_delay(); }
void i2c_stop_cond(void){ // set SDA to 0 clear_SDA(); I2C_delay(); // Clock stretching while (read_SCL() == 0) { // add timeout to this loop. } // Stop bit setup time, minimum 4us I2C_delay(); // SCL is high, set SDA from 0 to 1 if (read_SDA() == 0) { arbitration_lost(); } I2C_delay(); started = false; }
// Write a bit to I2C bus void i2c_write_bit(bool bit) { if (bit) { read_SDA(); } else { clear_SDA(); } I2C_delay(); while (read_SCL() == 0) { // Clock stretching // You should add timeout to this loop } // SCL is high, now data is valid // If SDA is high, check that nobody else is driving SDA if (bit && read_SDA() == 0) { arbitration_lost(); } I2C_delay(); clear_SCL(); }
void i2c_start(void) { //se já estiver iniciado, prepara para reenviar o bit de start if (started) { read_SDA(); I2C_delay(); while (read_SCL() == 0); // Clock stretching // Repeated start setup time, minimum 4.7us I2C_delay(); } if (read_SDA() == 0) { //Houve problema de comunicação } // SCL is high, set SDA from 1 to 0. clear_SDA(); I2C_delay(); clear_SCL(); started = 1; }
void i2c_start_cond(void) { if (started) { // if started, do a restart cond // set SDA to 1 read_SDA(); I2C_delay(); while (read_SCL() == 0) { // Clock stretching // You should add timeout to this loop } // Repeated start setup time, minimum 4.7us I2C_delay(); } if (read_SDA() == 0) { arbitration_lost(); } // SCL is high, set SDA from 1 to 0. clear_SDA(); I2C_delay(); clear_SCL(); started = true; }