Go Down

Topic: i2c and A/D (Read 4 times) previous topic - next topic

Nick Gammon

The compiler gets confused by the number 0, which might be a pointer or it might be a number. Putting (byte) there is a typecast which tells it which "version" of zero to use.

kzxm300


The compiler gets confused by the number 0, which might be a pointer or it might be a number. Putting (byte) there is a typecast which tells it which "version" of zero to use.

i see
thanks a lot.
greetings.

cappy2112

Quote from: Nick Gammon

To change the speed all you have to do is this:

Code: [Select]
Wire.begin ();
TWBR = 12;




Nick- where is this documented?
I've recently posted a question in another forum about how to change the I2C bus speed.

Nick Gammon

Here:

http://www.gammon.com.au/i2c

But I suppose you mean the "real" documentation?

Datasheet, page 241:

Quote
TWBR - TWI Bit Rate Register

TWBR selects the division factor for the bit rate generator. The bit rate generator is a frequency divider which generates the SCL clock frequency in the Master modes.


In twi.c is this:

Code: [Select]
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;


Where in twi.h is:

Code: [Select]
  #define TWI_FREQ 100000L

Now normally for a CPU speed of 16000000 that will give:

Code: [Select]
TWBR =  ((16000000 / 100000) - 16) / 2 = 72

And from the datasheet:

Quote
SCL frequency = CPU_FREQUENCY / (16 + 2(TWBR) * (PrescalerValue))


Where the prescaler happens to be 1 in this case.

So substituting, we get the SCL frequency:

Code: [Select]
16000000 / (16 + 2 * 72) = 100000

But if we change TWBR to 12 we get:

Code: [Select]
16000000 / (16 + 2 * 12) = 400000

Thus,  TWBR of 12 gives a clock four times as fast (400000 / 100000) compared to the default.

cappy2112


Here:

http://www.gammon.com.au/i2c

But I suppose you mean the "real" documentation?

Datasheet, page 241:

Quote
TWBR - TWI Bit Rate Register

TWBR selects the division factor for the bit rate generator. The bit rate generator is a frequency divider which generates the SCL clock frequency in the Master modes.


In twi.c is this:

Code: [Select]
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;


Where in twi.h is:

Code: [Select]
  #define TWI_FREQ 100000L

Now normally for a CPU speed of 16000000 that will give:

Code: [Select]
TWBR =  ((16000000 / 100000) - 16) / 2 = 72

And from the datasheet:

Quote
SCL frequency = CPU_FREQUENCY / (16 + 2(TWBR) * (PrescalerValue))


Where the prescaler happens to be 1 in this case.

So substituting, we get the SCL frequency:

Code: [Select]
16000000 / (16 + 2 * 72) = 100000

But if we change TWBR to 12 we get:

Code: [Select]
16000000 / (16 + 2 * 12) = 400000

Thus,  TWBR of 12 gives a clock four times as fast (400000 / 100000) compared to the default.


Thank you for all of that info.

What I am looking for is- what values need to be written to TBR to select the three I2C transfer modes.
You've answered that.

I wonder why the Wire library wasn't written with a simple method to select the transfer mode.
If I wanted to modify Wire to add such a method, where would I go to find out what the procedure is,
such that my changes will be evaluated to be included on upcoming releases?

Who is/are the maintainer(s) of Wire?


Go Up