I2C Programming Examples
MOTOROLA
I2C Modules
18-13
MOVE.B (A7)+, MBCR;
MOVE.B CALLING,-(A7);Transmit the calling address, D0=R/W
MOVE.B (A7)+, MBDR;
IFREE MOVE.B MBSR,-(A7);Check the IBB bit of the MBSR.
;If it is clear, wait until it is set.
BTST.B #5, (A7)+;
BEQ.S IBFREE;
18.6.3
POST-TRANSFER SOFTWARE RESPONSE
Transmission or reception of a byte will set the data transferring bit (ICF) to 1, which indicates one byte
communication is finished. The interrupt bit (IIF) is also set. An interrupt will be generated if the interrupt
function is enabled during initialization by setting the IIEN bit. Software must clear the IIF bit in the interrupt
routine first. The ICF bit will be cleared by reading from the I2C Data I/O Register (MBDR) in receive mode
or writing to MBDR in transmit mode.
Software can service the I2C I/O in the main program by monitoring the IIF bit if the interrupt function is
disabled. Polling should monitor the IIF bit rather than the ICF bit because that operation is different when
arbitration is lost.
When an interrupt occurs at the end of the address cycle, the master will always be in transmit mode. For
example, the address is transmitted. If master receive mode is required, indicated by MBDR[R/W], then
the MTX bit should be toggled.
During slave-mode address cycles (IAAS=1), the SRW bit in the status register is read to determine the
direction of the subsequent transfer and the MTX bit is programmed accordingly. For slave-mode data
cycles (IAAS=0), the SRW bit is not valid. The MTX bit in the control register should be read to determine
the direction of the current transfer.
The following is an example of a software response by a “master transmitter’' in the interrupt routine (see
MBSR LEA.L MBSR,-(A7);Load effective address
BCLR.B #1,(A7)+;Clear the IIF flag
MOVE.B MBCR,-(A7);Push the address on stack,
BTST.B #5,(A7)+;check the MSTA flag
BEQ.S SLAVE;Branch if slave mode
MOVE.B MBCR,-(A7);Push the address on stack
BTST.B #4,(A7)+;check the mode flag
BEQ.S RECEIVE;Branch if in receive mode
MOVE.B MBSR,-(A7);Push the address on stack,
BTST.B #0,(A7)+;check ACK from receiver
BNE.B
END;If no ACK, end of transmission
TRANSMIT MOVE.B DATABUF,-(A7);Stack data byte
MOVE.B (A7)+, MBDR);Transmit next byte of data
Generation of STOP
A data transfer ends with a STOP signal generated by the “master’' device. A master transmitter can generate a STOP signal
after all the data has been transmitted. The following code is an example showing how a master transmitter generates a stop
condition.
MASTX
MOVE.B MBSR, -(A7); If no ACK, branch to end
F
re
e
sc
a
le
S
e
m
ic
o
n
d
u
c
to
r,
I
Freescale Semiconductor, Inc.
For More Information On This Product,
Go to: www.freescale.com
n
c
..
.