Hardware Reference
In-Depth Information
Example 11.2
Write a subroutine that generates a start condition and also sends out the address of the
slave with which the MCU intends to exchange data. The address and the R/W bit value is
passed in accumulator A.
Solution: The subroutine is as follows:
sendSlaveID
brset
IBSR,IBB,*
; wait until I 2 C bus is free
bset
IBCR,TXRX 1 MSSL
; generate a start condition
staa
IBDR
; send out the slave address
brclr
IBSR,IBIF,*
; wait for address transmission to complete
movb
#IBIF,IBSR
; clear the IBIF flag
rts
The C language version of the function is as follows:
void sendSlaveID (char cx)
{
while (IBSR&IBB);
/* wait until I 2 C bus is idle */
IBCR | 5 TXRX 1 MSSL;
/* generate a start condition */
IBDR 5 cx;
/* send out the slave address with R/W bit */
while(!(IBSR & IBIF));
/* wait for address transmission to complete */
IBSR 5 IBIF;
/* clear IBIF flag */
}
11.5.2 I 2 C Data Transfer in Master Mode
When the HCS12 I 2 C module is configured as a master, it is responsible for initiating the
data transmission and reception. By writing a byte into the IBDR register, nine clock pulses are
generated to shift out 8 bits of data and shift in the acknowledgement bit. By reading from the
IBDR register, nine clock pulses are triggered to shift in 8 bits of data and shift out the acknowl-
edgement (positive or negative) bit.
After sending the slave ID and receiving the acknowledgement from the slave, the I 2 C mod-
ule can send out a byte by performing the following three steps:
Step 1
Store the data byte in the IBDR register. This step will trigger nine clock pulses on the
SCL pin to shift out 8 data bits and shift in the acknowledgement bit from the slave.
Step 2
Wait until the IBIF flag is set to 1, which indicates the byte has been shifted out.
Step 3
Clear the IBIF flag by writing a 1 to it.
If the data byte is stored in accumulator A, then the following instruction sequence can send a
byte to a slave via the I 2 C bus:
staa
IBDR
brclr
IBSR,IBIF,*
; wait until IBIF flag is set to 1
movb
#IBIF,IBSR
; clear the IBIF flag
 
Search WWH ::




Custom Search