uint8_t i2c_recv_byte(bool ack) { uint8_t value = 0; for(int8_t i = 7; i >= 0; i--) { i2c_scl_low(); i2c_sda_high(); // allow slave to read i2c_sleep_halfclock(); i2c_scl_high(); if(i2c_sda_value()) { value |= (1 << i); } i2c_sleep_halfclock(); } // ACK i2c_scl_low(); if(ack) { i2c_sda_low(); } else { i2c_sda_high(); } i2c_sleep_halfclock(); i2c_scl_high(); i2c_sleep_halfclock(); return value; }
boolean i2c_get_ack(Port port) { i2c_sda_high(port); i2c_scl_high(port); bool ret =!digitalRead(port.sda); i2c_scl_low(port); return ret; }
void i2c_stop(void) { i2c_scl_low(); i2c_sda_low(); i2c_sleep_halfclock(); i2c_scl_high(); i2c_sleep_halfclock(); i2c_sda_high(); i2c_sleep_halfclock(); }
bool i2c_send_byte(const uint8_t value) { for(int8_t i = 7; i >= 0; i--) { i2c_scl_low(); if((value >> i) & 1) { i2c_sda_high(); } else { i2c_sda_low(); } i2c_sleep_halfclock(); i2c_scl_high(); i2c_sleep_halfclock(); }
void i2c_shift_out(Port port, uint8 val) { int i; for (i=0;i<8;i++) { if((val & 0x80) != 0) { i2c_sda_high(port); } else { i2c_sda_low(port); } val <<= 1; i2c_scl_high(port); i2c_scl_low(port); } }
uint8 i2c_shift_in(Port port) { uint8 data = 0; i2c_sda_high(port); int i; for (i=0; i<8; i++) { data <<= 1; i2c_scl_high(port); if(digitalRead(port.sda)) { data |= 1; } i2c_scl_low(port); } return data; }
void i2c_shift_out(Port port, uint8 val) { int i; for (i=0;i<8;i++) { if((val & 0x80) != 0) { i2c_sda_high(port); } else { i2c_sda_low(port); } val <<= 1; i2c_scl_high(port); i2c_scl_low(port); } /* i2c_sda_high(port); i2c_scl_high(port); bool nack = digitalRead(port.sda); i2c_scl_low(port); */ }
void i2c_send_nack(Port port) { i2c_sda_high(port); i2c_scl_high(port); i2c_scl_low(port); }
void i2c_stop(Port port) { i2c_sda_low(port); i2c_scl_high(port); i2c_sda_high(port); }