technoblogy / tiny-i2c

Minimal I2C master routines for all AVR microcontrollers.
161 stars 39 forks source link

Setting bus speed? #2

Closed sheffieldnikki closed 2 years ago

sheffieldnikki commented 5 years ago

How easy is it to change the I2C bus speed using this library to a non-standard speed, like 10kHz?

It is just a case of picking different DELAY_T2TWI and DELAY_T4TWI durations?

Thanks

DirtyEngineer commented 5 years ago

There is a "define" statement in the TinyI2CMaster.h file; I think you can adjust as needed.

// Defines
#define TWI_FAST_MODE

#ifdef TWI_FAST_MODE                 // TWI FAST mode timing limits. SCL = 100-400kHz
#define DELAY_T2TWI (_delay_us(2))   // >1.3us
#define DELAY_T4TWI (_delay_us(1))   // >0.6us
#else                                // TWI STANDARD mode timing limits. SCL <= 100kHz
#define DELAY_T2TWI (_delay_us(5))   // >4.7us
#define DELAY_T4TWI (_delay_us(4))   // >4.0us
#endif
sheffieldnikki commented 5 years ago

What to those constants refer to? The LOW period and HIGH period of SCL? and for speeds <= 400kHz should DELAY_T2TWI >= DELAY_T4TWI + 0.7 us ?

Would (roughly) 50kHz be set by:

define DELAY_T2TWI (_delay_us(9)) // >8.7us

define DELAY_T4TWI (_delay_us(8)) // >8.0us

Thanks