[8] Programming

Submitted by rgcustodio on June 10, 2007 - 1:53pm.

[Theoretically its as easy as 1-2-3]

In its simplest form, the operations on the I2C bus is described below.


MASTER writes to a SLAVE

1) Send a START sequence

2) Send the I2C address of the SLAVE with the R/W bit low (even address)

3) Send the device register you want to write to

4) Send the data byte

5) [Optionally, send any further data bytes]

6) Send the STOP sequence.

 

MASTER reads from a SLAVE

1) Send a START sequence

2) Send I2C address of the SLAVE with the R/W bit low (even address)

3) Send device register you want to read from

4) Send a START sequence again (a repeated start)

5) Send I2C address of the SLAVE with the R/W bit high (odd address)

6) Read data byte from SLAVE

7) Send the STOP sequence.

 

The write operation is very straightforward, the MASTER addresses the SLAVE, sends the register it wants to access, then sends the data that will be written to the register.

 

I2C devices are, relatively, simple and dumb devices, it has no way of knowing what you want from it and this complicates the read operation a little bit. A write operation is performed to specify the register that the MASTER wants to read. Then, another START (known as a repeated start) is given to the SLAVE. The SLAVE is addressed again, and at this point the SLAVE has control the over the SDA line, in which it can transfer the necessary data to the MASTER. Even if there were 2 STARTs, only one STOP event is required to be sent.

 

[The Easy Way]

 

Programming I2C can be, relatively, easy. First time users are encouraged to use freely available code libraries on the Net, and refer to data sheets and sample applications from your chip manufacturer. Procyon armlib and avrlib has several APIs for I2C communications.

 

[SAMPLES]

 

1. I2C Compass sample application

 

2. I2C Real time clock (DS1307) sample application

 

3. I2C Servo controller (Mindsensors I2C-SC8) sample application

The valid commands are:

f : generate 1500ms pulse moving servo forward

c : generete 2500ms pulse moving servo back to center

h : generate 500ms pulse moving servo in a reverse direction

v : get motor controller version

b : get battery charge

 

4. I2C Servo controller (Mindsensors I2C-SC8) and PCF8575 (Sparkfun breakout board) sample application

The valid commands are:

f : generate 1500ms pulse moving servo forward

c : generete 2500ms pulse moving servo back to center

h : generate 500ms pulse moving servo in a reverse direction

s : stop motor, valid for continous rotation servos only

v : get motor controller version

b : get battery charge

6 : toggle LED 6

7 : toggle LED 7

 

 

Serial connection settings is 9600 8-n-1.

 

[The Hard Way]

 

If you have an MCU that has no hardware support for I2C, you can bit-bang two unused ports. Procyon avrlib users are fortunate because avrlib contains an a bit-banged I2C implementation, look for i2csw.c and i2csw.h.

 

Here's some pseudo code that should be able to help you ease the pain. Additional information related to the Microchip's PIC. Also here.

AttachmentSize
i2ccompass.zip3.81 KB
i2crtc.zip3.61 KB
i2csc8.zip3.89 KB
i2c_complex_test.zip56.9 KB
cp_pcf8575.jpg207.29 KB