Software Implementation of I2C™ Bus Master
Microchip Technology Inc.
This application note describes the software
implementation of I2C interface routines for the
PIC16CXXX family of devices. Only the master mode of
I2C interface is implemented in this application note.
This implementation is for a single master communica-
tion to multiple slave I2C devices.
Some PIC16CXXX devices, such as the PIC16C64 and
PIC16C74, have on-chip hardware which implements
the I2C slave interface, while other PIC16CXXX
devices, such as the PIC16C71 and PIC16C84, do not
have the same on-chip hardware.
This application note does not describe the I2C Bus
speciﬁcations and the user is assumed to have an
understanding of the I2C Bus. For detailed information
on the bus, the user is advised to read the I2C Bus
Speciﬁcation document from Philips/Signetics (order
number 98-8080-575). The I2C Bus is a two-wire serial
bus with multiple possible masters and multiple possi-
ble slaves connected to each other through two wires.
The two wires consists of a clock line (SCL) and a data
line (SDA) with both lines being bi-directional. Bi-direc-
tional communication is facilitated through the use of
wire and connection (the lines are either active-low or
passive high). The I2C Bus protocol also allows collision
detection, clock synchronization and hand-shaking for
multi-master systems. The clock is always generated by
the master, but the slave may hold it low to generate a
In most systems the microcontroller is the master and
the external peripheral devices are slaves. In these
cases this application note can be used to attach I2C
slaves to the PIC16CXXX (the master) microcontroller.
The multi-master system is not implemented because it
is extremely difﬁcult to meet all the I2C Bus timing spec-
iﬁcations using software. For a true slave or multi-mas-
ter system, some interface hardware is necessary (like
START & STOP bit detection).
In addition to the low level single master I2C routines, a
collection of high level routines with various message
structures is given. These high level macros/routines
can be used as canned routines to interface to most I2C
slave devices. As an example, the test program talks to
two Serial EEPROMs (Microchip’s 24LC04
Two levels of software routines are provided. The
low-level routines “i2c_low.asm” are provided in
Appendix A and the high level routines
“i2c_high.asm” are provided in Appendix B.
The messages passed (communicated on the two wire
network) are abbreviated and certain notation is used
to represent Start, Stop and other conditions. These
abbreviations are described in Table 1.
DESCRIPTION OF ABBREVIATIONS USED
Slave Address (for read operation)
Slave Address (for write operation)
Acknowledge condition (positive ACK)
Negative Acknowledge condition (NACK)
Data byte, D represents byte 0, D represents second byte
© 1997 Microchip Technology Inc.