Hardware Reference
In-Depth Information
Example 11.16
Write a subroutine that writes a byte into the 24LC08B. The device ID, memory address,
and data to be written are passed to this routine in the stack.
Solution: The assembly subroutine that writes a byte into the 24LC08B to the specified address
is as follows:
EE_ID
equ
5
; stack offset for EE_ID
EE_addr
equ
4
; stack offset for EE_addr
EE_dat
equ
3
; stack offset for EE_data
EEbyteWrite
psha
ldaa
EE_ID,sp
; get the EEPROM ID from stack
jsr
sendSlaveID
; generate start condition, send EEPROM ID
brclr
IBSR,RXAK,bywriteok1
; does EEPROM acknowledge?
ldab
#$FF
; return 2 1 as the error code
pula
rts
bywriteok1
ldaa
EE_addr,sp
; get the address to be accessed from the stack
staa
IBDR
; send address to I2C bus
brclr
IBSR,IBIF,*
; wait until address is shifted out
movb
#IBIF,IBSR
; clear the IBIF flag
brclr
IBSR,RXAK,bywriteok2
; does EEPROM acknowledge?
ldab
#$FF
; return 2 1 as the error code
pula
rts
bywriteok2
ldaa
EE_dat,sp
; get the data byte to be written from stack
staa
IBDR
; send out the data byte
brclr
IBSR,IBIF,*
; wait until data byte is shifted out
movb
#IBIF,IBSR
; clear the IBIF flag
brclr
IBSR,RXAK,bywriteok3
; does EEPROM acknowledge?
ldab
#$FF
; return 2 1 as the error code
pula
rts
bywriteok3
bclr
IBCR,MSSL
; generate stop condition
ldab
#0
; return error code 0
pula
rts
The C language version of the function is as follows:
char EEbyteWrite(char ID, char addr, char data)
{
SendSlaveID(ID);
if (IBSR & RXAK)
/* error if EEPROM does not respond */
return 2 1;
IBDR 5 addr;
/* send out address of the location to be written */
while(!(IBSR & IBIF));
IBSR 5 IBIF;
/* clear the IBIF flag */
if (IBSR & RXAK)
/* error if EEPROM does not respond */
return 2 1;
IBDR 5 data;
/* send out the data byte */
 
Search WWH ::




Custom Search