TWI clock calculation

Last post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

Act now, think later.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

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

Act now, think later.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The specific device I am going to work with is the MCP4725. It is rated for 100KHz and 400KHZ, and with a special command it will do 3.4MHz. I can't get the TWI up to 3.4 MHz, but as long as the AVR's clock exceeds 6.4MHz, I can get 400MHZ on the TWI bus.

I will soon find out if the ratings are real or hype. I will not have particularly long leads to the DAC chip, less than 25mm.

-Tony