Forum 2005-2010 (read only)
Set I2C speed
Topic: Set I2C speed
(Read 1 time)
Set I2C speed
Mar 03, 2008, 06:41 pm
how can set the speed of I2C communication using the wire library? In wire.begin() there is no parameter to set it.
Re: Set I2C speed
Mar 06, 2008, 08:10 am
: Mar 06, 2008, 08:25 am by MysteriousAges
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.
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:
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.