
18-14
MCF5249UM
MOTOROLA
I2C Programming Examples
BTST.B #0,(A7)+
BNE.B END
MOVE.B TXCNT,D0;Get value from the transmitting counter
BEQ.S END;If no more data, branch to end
MOVE.B DATABUF,-(A7);Transmit next byte of data
MOVE.B (A7)+,MBDR
MOVE.B TXCNT,D0;Decrease the TXCNT
SUBQ.L #1,D0
MOVE.B D0,TXCNT
BRA.S EMASTX;Exit
END LEA.L MBCR,-(A7);Generate a STOP condition
BCLR.B #5,(A7)+
EMASTX RTE; Return from interrupt
If a master receiver wants to terminate a data transfer, it must inform the slave transmitter by not
acknowledging the last byte of data, which can be done by setting the transmit acknowledge bit (TXAK)
before reading the next-to-last byte of data. Before reading the last byte of data, a STOP signal must first
be generated. The following code is an example showing how a master receiver generates a STOP signal.
MASR MOVE.B RXCNT,D0;Decrease RXCNT
SUBQ.L #1,D0
MOVE.B D0,RXCNT
BEQ.S ENMASR;Last byte to be read
MOVE.B RXCNT,D1;Check second-to-last byte to be read
EXTB.L D1
SUBI.L #1,D1;
BNE.S NXMAR; Not last one or second last
LAMAR BSET.B #3,MBCR;Disable ACK
BRA NXMAR
ENMASR BCLR.B #5,MBCR; Last one, generate 'STOP'signal
NXMAR MOVE.B MBDR,RXBUF; Read data and store RTE
Generation of Repeated START
At the end of data transfer, if the master still wants to communicate on the bus, it can generate another START signal followed
by another slave address without first generating a STOP signal. A program example follows.
RESTART MOVE.B MBCR,-(A7); Another START (RESTART)
BSET.B #2, (A7)
MOVE.B (A7)+, MBCR
MOVE.B CALLING,-(A7);Transmit the calling address, D0=R/W-
MOVE.B CALLING,-(A7);
MOVE.B (A7)+, MBDR
18.6.4
SLAVE MODE
In the slave interrupt service routine, the module that is addressed as slave bit (IAAS), should be tested to
check if a calling of its own address was received. If IAAS is set, software should set the transmit/receive
mode select bit (MTX bit of MBCR) according to the R/W command bit (SRW). Writing to the MBCR clears
the IAAS automatically. The only time IAAS is read as set is from the interrupt at the end of the address
cycle where an address match occurred; interrupts resulting from subsequent data transfers will have
IAAS cleared. A data transfer can now be initiated by writing information to MBDR, for slave transmits, or
read from MBDR, in slave-receive mode. A dummy read of the MBDR in slave/receive mode will release
SCL, allowing the master to transmit data.
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
..
.