Documente Academic
Documente Profesional
Documente Cultură
2: Inter-integrated Circuit
Interface (I2C)
Hardware Configuration
SDA and SCL lines have two possible states:
float high and driven low.
Both the SDA and SCL lines are pulled up to VDD
by pull-up resistors at Idle state
When the switch of the master or slave turns
on, the signal is pulled down because there is
current flowing from the I2C bus to the switch.
Baud Rate
TBRG = (4 TOSC /2) x (SSPADD<0:6>+1)
Baud Rate = 1/(2 TBRG)
= FOSC / [4 (SSPADD<0:6>+1)]
where TOSC = Oscillation period of clock
FOSC = 1/TOSC = Clock frequency
10
SCL
Figure 11.2 I 2C Start condition
11
Stop Condition
SDA
SCL
13
Stop Condition
Assert the stop condition by setting the
PEN bit (SSPCON2<2>):
bsf
SSPCON2, PEN
Wait for SSPIF to get asserted,
indicating completion:
bra
I2C_WaitFinish
14
Stop
Start
data transfer
direction (i.e., write to
read or vice versa)
Slave address
Need to send a
START signal to
make that change.
15
16
17
Transmission
Transmission of a data byte or an
address is achieved by writing to the
SSPBUF register. BF flag (SSPSTAT<0>)
is set.
Data will be shifted out at the falling
edge of SCL
18
Transmission
To write data/address to I2C bus:
First write to WREG register
Then move the value of WREG to SSPBUF
movwf SSPBUF
Finally, write for the SSPIF signal to
get asserted:
bra I2C_WaitFinish
19
Reception
20
Reception
To start reception:
bsf
SSPCON2, RCEN
Wait for SSPIF to get asserted:
bra
I2C_WaitFinish
Write SSPBUF into a register for further
processing:
movff SSPBUF, I2C_BYTE
21
Acknowledge Reception
Clear the ACKDT bit (SSPCON2<5>) to
acknowledge reception. Set ACKDT to
send a negative acknowledge
Set ACKEN to activate the
acknowledgement process
22
Acknowledge Reception
Set/Clear ACKDT:
bsf (bcf) SSPCON2, ACKDT
Set ACKEN to activate acknowledgement:
bsf SSPCON2, ACKEN
Wait for SSPIF to get asserted, indicating
completion:
bra I2C_WaitFinish
23
A2
A1
A0
24
Byte Write
1.
2.
3.
4.
5.
6.
7.
25
Byte Write
Recap what we defined
Write to EEPROM
26
Acknowledgement Polling
Once the stop condition has been issued
after the write operation, the EEPROM
enters the internal write cycle.
The duration of this cycle depends on the
device.
Acknowledgement polling involves
sending a start condition followed by the
control byte.
If the internal write cycle has not been
completed, the EEPROM would response
with a NAK; otherwise it would response
27
with an ACK.
Page Write
Instead of sending a STOP condition
after the transmission of the 1st byte,
send 7 more bytes.
If more than 8 bytes are written, the data
word address would roll over.
28
Byte Read
1. Perform Steps 1 to 5 of Byte Write to define the
device address and word address to read from.
2. The master asserts the repeated start condition
3. The master sends the control byte with last bit
equal to 1 indicating a read operation
4. AT24C02 acknowledges the reception of the
control byte
5. The master asserts the receive condition
6. The master receives the data byte and asserts
NACK to the EEPROM
7. The master asserts the stop condition
29
Byte Read
Conditions previously defined
30
Sequential Read
Instead of sending a NACK after the first byte
is read, the master sends out an ACK.
The operation terminates when the master
sends out a NACK bit and assert the stop
condition
Sequential read is not limited to 8 bytes.
31