## TWI clock calculation

7 posts / 0 new
Author
Message

I am trying to calculate the SCL clock rate given the prescaler and TWBR

The datasheet says SCL = F_CPU/(16 + 2*TWBR*Prescaler)

I am looking for 400KHz clock rate on an M644PA running at 18.432MHz. It seems as if I have to use rather low values of TWBR even at a prescaler of 1.

For a prescaler of 1, I calculate a TWBR of 15 to give a clock rate of 400,696Hz. Is this correct?

I guess I expected a larger value for TWBR and/or a larger prescaler on a CPU running this fast. It appears that there is a lot of room to slow down the TWI clock, but not much room to increase the clock especially on slower CPUs!

-Tony

Isn't 400kHz the maximum speed for vanilla I2C?

Quebracho seems to be the hardest wood.

John_A_Brown wrote:
Isn't 400kHz the maximum speed for vanilla I2C?

It is, but my gut feeling was telling me that if a 1 or 2 MHz device is going to get to 400KHz, then I would be using a larger prescaler or a TWBR greater than just 15 or 16 on a 18.432MHz device.

Is my TWBR calculation correct?

-Tony

```SCL = F_CPU/(16 + 2*TWBR*Prescaler)
SCL = 18432000/(16 + 2*15*1) = 400696 Hz
16 + 2*TWBR = F_CPU/SCL
TWBR = 0.5((F_CPU/SCL) - 16)
TWBR = 0.5((1843200/400000) - 16) = 15.04
```

I can never understand what the prescaler bits are for. You effectively end up with very slow bus for any values of the prescaler.

Just out of interest, measure your clock speed with a scope or logic analyser. You will get a surprise!

David.

Another off-the-side comment: Just because your TWI devices say they can do 400K does not mean that they actually can.

One of our designs uses the Philips PCA9555 I/O Expander. It's rated at 400 KHz, right there in the datasheet. In actual use we found the thing never works reliably over 100 KHz.

Caveat Emptor! (or, "Them's that don't look sometimes get took.")

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

John_A_Brown wrote:
Isn't 400kHz the maximum speed for vanilla I2C?

Seems I was wrong. ATW the standard speed is 100kHz.

Quebracho seems to be the hardest wood.