TWI Wire.setClock() on non arduino AVR MCU issue Using an Atmega168P - programmed by means of avrdude with the .hex file generated by Arduino IDE.

The following code sets the 8 MHz Atmega's TWI interface to just 15.625 kHz:

  Wire.begin(); // Initiate the Wire library
  Wire.setClock(100* 1000);
  delay(10);


While this one doubles it to 31.25 kHz:

  Wire.begin(); // Initiate the Wire library
  TWBR = (F_CPU/(100*1000) - 16)/2;
  TWSR &amp;= 0b11111100;
  delay(10);


Looking to get 100 kHz clock. What am I missing ?
Looking to get 100 kHz clock. What am I missing?


Calculations are int (16-bits) by default in AVR GCC. Try 100*1000L. Or even better:

#define F_TWI 100000L 


and use: 

TWBR = ((F_CPU/F_TWI)-16)/2;


or:

Wire.setClock(F_TWI);


Cheers!