I2C Programming Examples

the interrupt routine. I2SR[ICF] is cleared either by reading from I2DR in receive mode or by writing to I2DR in transmit mode.

Software can service the I2C I/O in the main program by monitoring IIF if the interrupt function is disabled. Polling should monitor IIF rather than ICF because that operation is different when arbitration is lost.

When an interrupt occurs at the end of the address cycle, the master is always in transmit mode; that is, the address is sent. If master receive mode is required I2CR[MTX] should be toggled.

During slave-mode address cycles (I2SR[IAAS] = 1), I2SR[SRW] is read to determine the direction of the next transfer. MTX is programmed accordingly. For slave-mode data cycles (IAAS = 0), SRW is invalid. MTX should be read to determine the current transfer direction.

The following is an example of a software response by a master transmitter in the interrupt routine (see Figure 24-10).

I2SR

LEA.L I2SR,-(A7)

;Load effective address

 

BCLR.B #1,(A7)+

;Clear

the

IIF flag

 

 

MOVE.B I2CR,-(A7)

;Push the address on

stack,

 

BTST.B #5,(A7)+

;check

the

MSTA flag

 

 

BEQ.S SLAVE

;Branch if

slave mode

 

 

MOVE.B I2CR,-(A7)

;Push the address on

stack

 

BTST.B #4,(A7)+

;check

the

mode flag

 

 

BEQ.S RECEIVE

;Branch if

in receive mode

 

MOVE.B I2SR,-(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)+, I2DR

;Transmit next byte of data

24.6.4

Generation of STOP

 

 

 

 

A data transfer ends when the master signals a STOP, which can occur after all data is sent, as in the following example.

MASTX

MOVE.B I2SR, -(A7)

;If no ACK, branch to end

 

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)+,I2DR

 

 

MOVE.B TXCNT,D0

;Decrease the TXCNT

 

SUBQ.L #1,D0

 

 

MOVE.B D0,TXCNT

 

 

BRA.S EMASTX;Exit

 

END

LEA.L I2CR,-(A7)

;Generate a STOP condition

 

BCLR.B #5,(A7)+

 

 

EMASTX RTE

;Return from interrupt

24-12

MCF5282 User’s Manual

MOTOROLA

Page 522
Image 522
Motorola MCF5282, MCF5281 user manual Generation of Stop, I2SR