Go Down

Topic: Set I2C speed (Read 989 times) previous topic - next topic

lodo2609

Dear all,

how can set the speed of I2C communication using the wire library? In wire.begin() there is no parameter to set it.

Cheers Lodo2609

MysteriousAges

#1
Mar 06, 2008, 08:10 am Last Edit: Mar 06, 2008, 08:25 am by MysteriousAges Reason: 1
You can't set I2C speed through the Wire library. If you want to change the speed to something other than 100kHz, you'll need to twiddle with registers. Check the data sheet for these guys.


EDIT:
Oh, bugger. I hate searching for problems and not getting an answer, so I'll post more to help you, and those to come after out. (:

The frequency of the SCL is controlled by two registers: TWI Bit rate Register (TWBR) and the prescaler bits in the TWI Status Register (TWSR). The end frequency of SCL is calculated as follows:

SCL frequency = CPU Clock frequency / (16 + 2 * (TWBR) * (Prescaler)

TWBR is at 0xB8
TWSR is at 0xB9 and the prescaler bits are 0 and 1. The prescaler value is as follows:







TWSR 1TWSR 0Prescaler value
001
014
1016
1164



So. Assuming a 16 MHz clock, if you wanted to have the TWI run at 100kHz, you could use the prescaler value of 4, and a bit rate register value of 18. If you wanted to jack that up to 400kHz, you could quadruple the value of TWBR, or you could use the next prescaler value. Just make sure that whatever you're talking to supports the speed you're wanting to run it at.

... But don't just take this info and run. It's still a very good idea to check the data sheet anyway, in case I've screwed something up.

Go Up