Professional Documents
Culture Documents
Goodi2c PDF
Goodi2c PDF
Goodi2c PDF
e
from Philips and the PIC16C6x from
Microchip). An all-software imple-
xternal devices mentation of I2C protocol is also pos-
that are used to sible. Thanks to this method, any
expand a microcon- microcontroller can communicate
Thanks to the possibil- troller’s internal with I2C devices. In this article, I’d
resources are generally available with like to present such an implementa-
ity of all-software a parallel interface but a serial inter- tion for the popular 8031 microcon-
implementation of the face is becoming more popular. There troller.
is a wide range of such devices,
I2C protocol, micro- including EEPROMs, display con- THE BUS
trollers, real-time clocks, A/D and The I2C interface is modest in its
controllers can com- D/A converters, and I/O expanders. hardware resource requirements,
municate with I2C Serial thermal sensors, directly con- because only a single pair of signal
nectable to a microcontroller, are lines is needed: serial data (SDA) and
devices. With this arti- offered as well. Serially accessed serial clock (SCL) (see Figure 1). Both
devices require less wiring and space lines are bidirectional and must be
cle, Dariusz presents on printed circuit boards than parallel connected to a positive supply voltage
us with an example ones. Thus, printed circuit boards and
connectors can be simpler and small-
via pull-up resistors. The SDA and
SCL pins of each device also must
implementation of the er. Reduced wiring also increases reli- have an open drain or open collector
ability of a system. In some cases, in order to perform the wired AND
Standard mode of the such microcontrollers without an function. Data can be transferred at a
external address and data bus, the rate of up to 100 kbps in Standard
I2C protocol for the application of serial devices enables mode, up to 400 kbps in Fast mode,
popular 8031 micro- interaction between the microcon- and up to 3.4 Mbps in High-Speed
troller and the outside world. mode. Each slave on the bus is identi-
controller. Most serial devices are equipped fied by a unique address.
with a synchronous interface for In Standard mode, 7-bit addressing
which they require separate signal is used. In other modes, slaves can
lines for transferring data and clock have 7- or 10-bit addresses. The num-
THE PROCESS
ber of devices that can be connected condition. A 0-to-1 transition signals The I2C protocol does not have to
to the same bus is limited by the the end of a transfer and is termed as be implemented in hardware.
maximum bus capacitance of 400 pF. a stop condition. The data is trans- Software implementation is also pos-
The I2C bus can be controlled by ferred in bytes, with the most signifi- sible, because the protocol is forgiving
more than one master. If two or more cant bit sent first. Note that the byte with regard to timing accuracy. And,
masters simultaneously initiate data transfer requires nine clock pulses. any of the microcontroller’s general-
transfer, collision is detected and an The transfer of a byte’s bits takes purpose I/O lines can be used as I2C
arbitration procedure is performed. eight pulses, and the ninth is used for lines. This approach is useful when a
The arbitration doesn’t cause data cor- acknowledgment. Between start and system design includes only a single
ruption, however, most system stop conditions, an unrestricted num- master.
designs include only one master. ber of bytes can be transferred. As stated earlier, such designs are
Only the master generates the After a start condition, the byte most frequent. I have implemented
clock, but transmission speed can be containing the slave address (or part the I2C protocol for the 8031 micro-
adjusted to the internal operating rate of the address, when 10-bit addressing controller, using only Standard mode.
of the addressed slave. This adjust- is used) and a data direction bit is The source code was written in C-51.
ment is made by clock stretching, in always sent first (see Figure 3). A start The use of a high-level programming
which the slave keeps the SCL pulled condition can be repeated without language shortened development time
low until it is ready to continue. first generating a stop condition. This considerably. It also simplifies
is used to change transfer direction or changes and adaptation of the code to
THE PROTOCOL to address another slave. If there is no microcontrollers with different archi-
The I2C protocol is tectures.
level-sensitive. The data Listing 1 presents
must be stable when SCL Change of functions perform-
data allowed
is high. Except for two ing basic operations
situations, the state of the SDA .......
of the I2C protocol.
MSB LSB
.......
SDA line can only change SCL The functions given
1 2 ....... 8 9
when SCL is low. The ....... require 135 bytes of
exceptions have special START Data line Clock pulse for STOP code memory and
condition stable; acknowledgement condition
meanings (see Figure 2). A data valid only a few bytes of
1-to-0 transition signals internal data mem-
the beginning of a transfer Figure 2—This diagram shows the issuing of the start and stop conditions and how the byte trans- ory, if a compact
fer on the I2C bus is performed.
and is termed as a start memory model is
bit EEPROM_acknowledge_polling(void)
{
bit status;
start();
status = write(EEPROM);
stop();
return(status); /* if 1, the write cycle is in progress */
}
void EEPROM_busy(void)
{
while(EEPROM_acknowledge_polling())
delay(1,164); /* about 1 msec */
}
Listing 5—This example shows how to restore float variables within the contents of the EEPROM.
/****************************************************
Global variables.
****************************************************/
/****************************************************
Initialization of the system.
****************************************************/
void initialization(void)
{
:
EEPROM_sequential_read(&t1_setp,T1_SETP_ADDR,4);
EEPROM_sequential_read(&t2_setp,T2_SETP_ADDR,4);
:
}