Software I2C library implementation for Arduino
This dummy sample demonstrates basic usage of the library:
#include <XantoI2C.h>
const uint8_t PIN_SCL = 5;
const uint8_t PIN_SDA = 6;
const uint8_t SOME_CHIP_COMMAND = 0x77;
XantoI2C i2c(PIN_SCL, PIN_SDA);
void sendSomeCommand() {
i2c.start();
i2c.writeByte(SOME_CHIP_COMMAND);
if (i2c.readAck()) {
return;
}
i2c.stop();
}
void setup() {
sendSomeCommand();
}
void loop() {
}
Simple software I2C-master implementation. Like any other I2C it uses two wires to communicate with slave devices: Serial Data Line (SDA) and Serial Clock Line (SCL). But it is not limited to the use of Arduino’s special SDA/SCL pins, you can use any pins.
Features:
This pictures demonstrates some I2C cases. Also it shows signal timings (pulse time, delay time).
XantoI2C uses delays with a microsecond precision to generate clock and data signals.
List of supported speeds:
… delays more than 10 microsecond are also possible.
Actually, many chips don’t require strict speed for I2C communucation. They declare maximal or nominal speed, but can operate on much lower speeds.
/**
* Create a new I2C bus with the given speed
*
* clock_pin - any digital pin for SCL line
* data_pin - any digital pin for SDA line
* delay_time_us - time of signal propagation in microseconds
* actually it is a quarter of the clock time, see Bus Speed section
*/
XantoI2C(uint8_t clock_pin, uint8_t data_pin, uint16_t delay_time_us = 2);
/**
* Start Condition (or Repeated Start Condition)
* First pulls the SDA line low, and next pulls the SCL line low.
*/
void start();
/**
* I2C stop condition
* The Bus Master first releases the SCL and then the SDA line.
*/
void stop();
/**
* For each clock pulse one bit of data is transferred.
* The SDA signal can only change when the SCL signal is low
* when the clock is high the data should be stable.
*/
void writeByte(uint8_t data_byte);
/**
* Read one byte of data from a slave
*/
uint8_t readByte();
/**
* Read Acknowledge from a slave
* Return 0 if ACK was received, else 1
*/
uint8_t readAck();
/**
* Read No Acknowledge from a slave
* Return 0 if NACK was received, else 1
*/
uint8_t readNack();
Also XantoI2C includes some helper methods for frequently used scenarios:
/**
* Execute scenario: start, write, ack and stop
*/
uint8_t doStartWriteAckStop(uint8_t data_byte);
/**
* Execute scenario: start, multiple writes with acks and stop
*/
uint8_t doStartWriteAckStop(uint8_t data_bytes[], uint8_t data_length);
XantoI2C i2c(PIN_SCL, PIN_SDA);
i2c.doStartWriteAckStop(0x77);
i2c.start();
i2c.writeByte(KT0803_CMD_WRITE);
if (i2c.readAck()) {
return 0;
}
i2c.writeByte(register_address);
if (i2c.readAck()) {
return 0;
}
i2c.start();
i2c.writeByte(KT0803_CMD_READ);
if (i2c.readAck()) {
return 0;
}
uint8_t register_value = i2c.readByte();
if (i2c.readNack()) {
return 0;
}
i2c.stop();
XantoI2C_TM1637_Counter - example how to drive a 4-digit LED display module using XantoI2C
XantoI2C_KT0803L_Registers - example how to read all registers from the KT0803L chip (FM transmitter module)
XantoTM1637 - Arduino library for TM1637-based 4-digit display module. XantoTM1637 uses XantoI2C library for I2C communications with a TM1637 chip.
XantoKT0803 - Arduino library for KT0803-based FM radio transmitter. XantoKT0803 uses XantoI2C library for I2C communications with a KT0803 chip.
Xantorohara xantorohara@gmail.com