Philips Semiconductors
12C Specific information Programming the 12C Interface
polling of the SCL line that gives rise I
an importanc feature of 12C: automatit
bit-by-bit baud-rate adjustment. Any df
vice on the IzC bus may hoid the clot
line low m order to stall the bus fc
more rime (a serial wait state). The otl
er devices on the bus are then force
to poll the SCL line uric11 the slow d<
vice releases control of the clock.
The %Get_SDA_&t macro also fal
under the category of “watching.” 11
function is simply to return the state c
the SDA line without waiting for a trar
sition. %Get_SDA_Bit LS used pnmanl
to pull the serial data off the bus whe
the clock is valid.
The .‘domg” macros control the stat
of the clock and data lines. As with th
polling macros. there are four types-
one for each transition of the XL c
SDA lines. The .‘domg ’ macros ar
named to reflect the physical operation
they perform. For example, %Drzve
SCL_Low always drives the SCL line t
a low state. %Relea.se_SCL_Higb, on th
other hand, relinquishes control of th
SCL line, which may then be pulIed hig
or driven low by another device on th
bus. A read-modify-write operation I
used for the bit manipulation so mat th
other 6 bits of Port 2 are not affecte
by the 12C operations.
Getting on the Bus
Three procedures were created usm
the macro framework. I’ll describe or
ly the master transmit (Listing One, pag
I I
stop
Figure 2: Flowcba??jx- PC transmrt
procedure.
106~ and master receive functions (List-
ing Two, page 1081, as they represent
the needs of most 12C users. The slave
procedure is long and intricate and will
not be described here.
An 12C master transmission proceeds
a.5 follows:
I. The master polls the bus to see if it
is in use.
2. The master generates a start condi-
tion on the bus.
3. The master broadcasts the slave ad-
dress and expects an acknowledge
(ACK) from the addressed slave.
4. The master transrmts 0 or more bytes
of data, expecting an ACK following
each byte.
5. The master generates a stop condi-
tion and releases the bus.
The stack frame for the master trans-
mit procedure, I~CXA.A~~, includes a
far pointer to the message for transrms-
sion, the byte count for the message,
and the slave address. Far pointers and
far procedure calls are used in all the
procedures. No attempt was made to
conform to a specific high-level lan-
guage caUing convention, although such
a conversion would be trivial. The pro-
cedures save only the state of the mcxd-
shed segment registers.
The master transmit procedure per-
forms error checking on the passed pa-
rameters before attempting to send the
message. The maximum message length
is set at 64 Kbytes by the segmentation
of the 80186 memory space. This re-
strictton could be removed by mciud-
ing code to handle segment boundanes.
The transmit procedure also checks the
direction bit tn the slave address to en-
sure that a reception was not erro-
neously indicated. Errors are reported
back to the calling procedure through
the AK regrster. (The exact code is in
Listing One.)
The first step in sending a message is
getting on the 12C bus. The macro
oM~ec~_~or_Br&+-ee simply polls the
bus to determme if any transactions are
in progress. If so, the transmit proce-
dure aborts with the appropriate error
code. If the bus is free, a start condition
IS generated, The start condition is de-
fined as a high-to-low transition of SDA
with SCL high followed by a 4.7_uS
pause. These waveforms are easily gen-
erated with the %Dnve_SDA_Low and
% WaU_4_ 7-d macros.
All communication on the Ik bus be-
tween the stop and start condiuons, in-
cluding addresstng and data, takes place
as an &bit data value followed by an
acknowledge bit. This lead to the nat-
ural nested loop structure for the body
of the procedure: see Figure 2.
71
The inner loop IS responsible for
transmitung the 8 bits of each data byte.
Each transmitted bit generates the ap-
propriate data (SDA) and clock (SCL)
waveforms whiIe checking for both se-
rial wait states and potential bus colli-
sions. A bus collision occurs when two
masters attempt to gain control of the
lh-ee distinct tasks
are involved in
implementing the
watching the bus,
waiting for a specijiic
amount of time, and
driving the bus
bus simultaneously. The 12C protocol
handles collisions with the simple rule:
“He who transmits the first 0 on the SDA
line wins the bus.” To ensure that we
(the master transmit procedure) own the
bus, the SDA line is checked whenev-
er transmitting a 1. If a 0 is present, then
a collision has occurred (because an-
other master is pulling the line low),
and the transfer must be aborted.
Control is turned over to the outer
loop after the 8 bits of data (or address)
have been transmitted. The outer loop
immediately checks for an acknowledge
from the addressed slave. The transfer
is aborted if an acknowledge is not re-
ceived. At the end of the ACK bit the
message length counter IS decremented.
Control is returned to the inner loop if
more data remains, otherwrse a stop con-
dition is generated and the master mu-is-
rmt procedure terminates.
Registers are used for tntermediate re-
sult storage throughout the body of the
procedure. For example, the AH reg-
ister is used to hold the current value
(either address or data) bemg shifted
onto the SDA line. This elimmates the
need for local data storage within the
procedure.
On the Receiving End
The steps involved in an I’C master re-
ceive transaction are almost identical to
those in transmission:
1. The
is in use.
2. The master
master the bus to see tf it
generates a start condi-
L3 Dobbk Journal June 1992