18-12
MCF5249UM
MOTOROLA
I2C Programming Examples
18.6
I2C
PROGRAMMING EXAMPLES
18.6.1
INITIALIZATION SEQUENCE
A reset places the I2C Control Register into default status. Before the interface can transfer serial data,
users must perform an initialization procedure as follows:
1. Update the Frequency Divider Register (MFDR) and select the required division ratio to obtain SCL
frequency from the system bus clock.
2. Update the I2C Address Register (MADR) to define its slave address.
3. Set the IEN bit of the I2C Control Register (MBCR) to enable the I2C bus interface system.
4. Modify the MBCR to select master/slave mode, transmit/receive mode, and interrupt-enable or not.
Note: During the initialization of the I2C bus module, the user should check the IBB bit of
the MBSR register. If the IBB bit is set when the I2C module is enabled, then the
following code sequence should be executed before proceeding with the normal
initialization code. This issues a STOP command to the slave device, which places
it into the idle state as if it were recently power cycled.
MBCR = $0
MBCR = $A0
dummy read of MBDR
MBSR = $0
MBCR = $0
18.6.2
GENERATION OF START
After completion of the initialization procedure, users can transmit serial data by selecting the “master
transmitter'’ mode. If the MCF5249 is connected to a multimaster bus system, users must test the state of
the I2C Busy Bit (IBB) to check whether the serial bus is free.
If the bus is free (IBB=0), the start condition and the first byte (the slave address) can be sent. The data
written to the data register comprises the address of the desired slave and the LSB is set to indicate the
direction of transfer required.
The bus free time (i.e., the time between a STOP condition and the following START condition) is built into
the hardware that generates the START cycle. Depending on the relative frequencies of the system clock
and the SCL period, users may have to wait until the I2C is busy after writing the calling address to the
MBDR before proceeding with the following instructions.
An example of a program that generates the START signal and transmits the first byte of data (slave
address) is shown as follows:
CHFLAG MOVE.B MBSR,-(A7);Check the MBB bit of the MBSR
BTST.B #5, (A7)+
BNE.S CHFLAG;If it is set, wait until it is clear
TXSTART MOVE.BMBCR,-(A7);Set transmit mode
BSET.B #4,(A7)
MOVE.B (A7)+, MBCR
MOVE.B MBCR, -(A7);Set master mode
BSET.B #5, (A7);Generate START condition
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
..
.