Hardware Reference
In-Depth Information
while(!(IBSR&IBIF));
IBSR 5 IBIF;
/* clear the IBIF flag */
if (IBSR & RXAK)
/* error if EEPROM does not respond */
return 2 1;
IBCR & 5 , MSSL;
/* generate a stop condition */
return 0;
/* normal write code */
}
Example 11.17
Write a function that performs a pagewrite operation. The control byte, the starting address
of the destination, and the pointer to the data in RAM to be written are passed to this function.
Solution: The following function writes a block of up to 16 bytes to the EEPROM. The block
of data to be output is pointed to by X. The EEPROM ID and starting address to be output are
passed in accumulators A and B, respectively. The number of bytes to be output is passed in Y.
The error code 21 will be returned if the EEPROM does not respond to its ID.
EEpageWrite jsr
sendSlaveID
; generate start condition and send out slave ID
brclr
IBSR,RXAK,pwriteok1
; does the EEPROM acknowledge?
ldab
#$FF
; return error code 2 1
rts
pwriteok1
stab
IBDR
; send out the starting address to be written
brclr
IBSR,IBIF,*
; wait until the byte is shifted out
movb
#IBIF,IBSR
; clear the IBIF flag
brclr
IBSR,RXAK,w_loop
; does the EEPROM acknowledge?
ldab
#$FF
; return error code 2 1
rts
w_loop
cpy
#0
beq
done_EEwrite
; byte count is 0, done
movb
1,x 1 ,IBDR
; send out 1 byte
brclr
IBSR,IBIF,*
; wait until the byte is shifted out
movb
#IBIF,IBSR
; clear the IBIF flag
brclr
IBSR,RXAK,okNxt
; receive ACK?
ldab
#$FF
rts
okNxt
dey
; decrement byte count
bra
w_loop
done_EEwrite
bclr
IBCR,MSSL
; generate a stop condition
ldab
#0
; return error code 0
rts
The C function that performs the pagewrite operation is as follows:
char EEpageWrite(char ID, char addr, char ByteCnt, char *ptr)
{
SendSlaveID(ID);
/* send out EEPROM ID */
if (IBSR & RXAK)
return 2 1;
/* return 2 1 if EEPROM did not respond */
 
Search WWH ::




Custom Search