RobTillaart / AGS02MA

Arduino library for AGS02MA TVOC sensor
MIT License
14 stars 3 forks source link

redo low I2C clock speed for AVR #18

Closed RobTillaart closed 2 years ago

RobTillaart commented 2 years ago

See: https://github.com/adafruit/Adafruit_BusIO/pull/104

If the prescaler was not 0 just setting the TWBR register low is not always enough. Need to explicitly set prescaler too.

And set the prescaler back too.

RobTillaart commented 2 years ago

From ATMEL328 datasheet

Page 223 image

page 244 - 21.9.3 - TWSR – TWI Status Register image

TWI.C

  // initialize twi prescaler and bit rate
  cbi(TWSR, TWPS0);
  cbi(TWSR, TWPS1);
  TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;

In AVR the prescaler is therefor always 1 (hence the formula) As we need to set the SCL to 30 Kbit or even 25 or 20 Kbit we can calculate if this is easier with changed TWSR

TWBR = ((F_CPU / TWI_FREQ) - 16) /  (2 * prescaler);

Spreadsheet image

|