choi_new
Newbie level 6
Dear Friend,
Sorry, is me again.
I have writen a 8051 code to access AT24C512 data.
BTW, I found my program only can read FFH from the EPROM when I using EMU board, but I already burned a bmp file into the EEPROM.
Hence, I think my program have bug.
The below is my code.
can someone help me to find out the program??
Thanks a million!!!!
Peter
;;==== code
SDA BIT P2.2
SCL BIT P2.3
;SDA BIT P2.3
;SCL BIT P2.2
ADD0 BIT P2.0
ADD1 BIT P2.1
FADDR EQU 0A0H
ADDR_HI1 EQU 30H
ADDR_LO1 EQU 31H
ADDR_HI2 EQU 32H
ADDR_LO2 EQU 33H
RAM_DATA EQU 34H
ORG 00H
MOV ADDR_HI1, #00H
MOV ADDR_LO1, #00H
MOV ADDR_HI2, #00H
MOV ADDR_LO2, #00H
MOV RAM_DATA, #00H
I2C:
INC DPTR
MOV ADDR_HI2, DPH
MOV ADDR_LO2, DPL
LCALL READ_RANDOM
NOP
MOV A, RAM_DATA
MOV A, RAM_DATA
MOV A, RAM_DATA
MOV A, RAM_DATA
SJMP I2C
READ_CURRENT:
; AT24Cxx Current Address Read function.
; Called with programmable address in A. Returns data in A.
; Returns CY set to indicate that the bus is not available
; or that the addressed device failed to acknowledge.
LCALL START
JC X_45 ; abort if bus not available
; RL A ; programmable address to bits 3:1
; ORL A, #FADDR ; add fixed address
; SETB ACC.0 ; specify read operation
MOV A, #0A0H
LCALL SHOUT ; send device address
JC X_44 ; abort if no acknowledge
LCALL SHIN ; receive data byte
MOV RAM_DATA, A
LCALL NAK ; do not acknowledge byte
CLR C ; clear error flag
X_44:
LCALL STOP
RET
X_45:
RET
READ_RANDOM:
; AT24Cxx Random Read function.
; Called with programmable address in A, byte address in
; register pair ADDR_HI:ADDR_LO. Returns data in A.
; Returns CY set to indicate that the bus is not available
; or that the addressed device failed to acknowledge.
PUSH B
MOV B, A ; save copy of programmable address
; Send dummy write command to set internal address.
LCALL START
JC X_47 ; abort if bus not available
; RL A ; programmable address to bits 3:1
; ORL A, #FADDR ; add fixed address
; CLR ACC.0 ; specify write operation
MOV A, #0A0H
LCALL SHOUT ; send device address
JC X_46 ; abort if no acknowledge
MOV A, ADDR_HI1 ;; send high byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_LO1 ; send low byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_HI2 ;; send high byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_LO2 ; send low byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
; Call Current Address Read function.
MOV A, B ; get programmable address
LCALL READ_CURRENT
LJMP X_47 ; exit
X_46:
LCALL STOP
X_47: POP B
RET
START:
; Send START, defined as high-to-low SDA with SCL high.
; Return with SCL, SDA low.
; Returns CY set if bus is not available.
SETB SDA
SETB SCL
; Verify bus available.
JNB SDA, X_40 ; jump if not high
JNB SCL, X_40 ; jump if not high
NOP ; enforce setup delay and cycle delay
CLR SDA
nop ; enforce hold delay
nop ;
nop ;
nop ;
nop ;
CLR SCL
CLR C ; clear error flag
SJMP X_41
X_40:
SETB C ; set error flag
X_41:
RET
STOP:
; Send STOP, defined as low-to-high SDA with SCL high.
; SCL expected low on entry. Return with SCL, SDA high.
CLR SDA
nop ; enforce SCL low and data setup
nop
SETB SCL
nop ; enforce setup delay
nop ;
nop ;
nop ;
nop ;
SETB SDA
RET
SHOUT:
; Shift out a byte to the AT24Cxx, most significant bit first.
; SCL, SDA expected low on entry. Return with SCL low.
; Called with data to send in A.
; Returns CY set to indicate failure by slave to acknowledge.
; Destroys A.
PUSH B
MOV B, #8 ; bit counter
X_42:
RLC A ; move bit into CY
MOV SDA, C ; output bit
nop ; enforce SCL low and data setup
SETB SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
CLR SCL ; drop clock
DJNZ B, X_42 ; next bit
SETB SDA ; release SDA for ACK
nop ; enforce SCL low and tAA
nop ;
SETB SCL ; raise ACK clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
MOV C, SDA ; get ACK bit
CLR SCL ; drop ACK clock
;LCALL ACK
POP B
RET
SHIN:
; Shift in a byte from the AT24Cxx, most significant bit first.
; SCL expected low on entry. Return with SCL low.
; Returns received data byte in A.
; SETB SDA ; make SDA an input
; PUSH B
; MOV B, #8 ; bit count
;X_43:
; nop ; enforce SCL low and data setup
; nop ;
; nop ;
; SETB SCL ; raise clock
; nop ; enforce SCL high
; nop ;
; MOV C, SDA ; input bit
; RLC A ; move bit into byte
; CLR SCL ; drop clock
; DJNZ B, X_43 ; next bit
; POP B
; RET
SETB SDA ;; make SDA an input,使SDA为高,准备读
PUSH B ;
MOV B, #08D ;;;;; bit count,设置位计数器
L2: NOP ;; enforce SCL low and data setup,保持SCL为低且使数据稳定
NOP
SETB SCL ;; raise clock,升高时钟
NOP ;; enforce SCL high,保持SCL为高
NOP
MOV C, SDA ;; input bit,输入位
RLC A ;; move bit into byte,将位移入A
CLR SCL ;; drop clock,降低时钟
DJNZ B, L2 ;; next bit,传送下一位
POP B
RET
NAK:
; Clock out a negative acknowledge bit (high).
; SCL expected low on entry. Return with SCL low, SDA high.
SETB SDA ; NAK bit
nop ; enforce SCL low and data setup
nop ;
SETB SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
CLR SCL ; drop clock
RET
ACK:
; Clock out an acknowledge bit (low).
; SCL expected low on entry. Return with SCL, SDA low.
clr SDA ; ACK bit
nop ; enforce SCL low and data setup
nop ;
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
Added after 1 minutes:
just a remind, I am not the aurthor of this code.
I just change the code for my application after I read it.
So, I am not copy right holder
Sorry, is me again.
I have writen a 8051 code to access AT24C512 data.
BTW, I found my program only can read FFH from the EPROM when I using EMU board, but I already burned a bmp file into the EEPROM.
Hence, I think my program have bug.
The below is my code.
can someone help me to find out the program??
Thanks a million!!!!
Peter
;;==== code
SDA BIT P2.2
SCL BIT P2.3
;SDA BIT P2.3
;SCL BIT P2.2
ADD0 BIT P2.0
ADD1 BIT P2.1
FADDR EQU 0A0H
ADDR_HI1 EQU 30H
ADDR_LO1 EQU 31H
ADDR_HI2 EQU 32H
ADDR_LO2 EQU 33H
RAM_DATA EQU 34H
ORG 00H
MOV ADDR_HI1, #00H
MOV ADDR_LO1, #00H
MOV ADDR_HI2, #00H
MOV ADDR_LO2, #00H
MOV RAM_DATA, #00H
I2C:
INC DPTR
MOV ADDR_HI2, DPH
MOV ADDR_LO2, DPL
LCALL READ_RANDOM
NOP
MOV A, RAM_DATA
MOV A, RAM_DATA
MOV A, RAM_DATA
MOV A, RAM_DATA
SJMP I2C
READ_CURRENT:
; AT24Cxx Current Address Read function.
; Called with programmable address in A. Returns data in A.
; Returns CY set to indicate that the bus is not available
; or that the addressed device failed to acknowledge.
LCALL START
JC X_45 ; abort if bus not available
; RL A ; programmable address to bits 3:1
; ORL A, #FADDR ; add fixed address
; SETB ACC.0 ; specify read operation
MOV A, #0A0H
LCALL SHOUT ; send device address
JC X_44 ; abort if no acknowledge
LCALL SHIN ; receive data byte
MOV RAM_DATA, A
LCALL NAK ; do not acknowledge byte
CLR C ; clear error flag
X_44:
LCALL STOP
RET
X_45:
RET
READ_RANDOM:
; AT24Cxx Random Read function.
; Called with programmable address in A, byte address in
; register pair ADDR_HI:ADDR_LO. Returns data in A.
; Returns CY set to indicate that the bus is not available
; or that the addressed device failed to acknowledge.
PUSH B
MOV B, A ; save copy of programmable address
; Send dummy write command to set internal address.
LCALL START
JC X_47 ; abort if bus not available
; RL A ; programmable address to bits 3:1
; ORL A, #FADDR ; add fixed address
; CLR ACC.0 ; specify write operation
MOV A, #0A0H
LCALL SHOUT ; send device address
JC X_46 ; abort if no acknowledge
MOV A, ADDR_HI1 ;; send high byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_LO1 ; send low byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_HI2 ;; send high byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
MOV A, ADDR_LO2 ; send low byte of address
LCALL SHOUT ;
JC X_46 ; abort if no acknowledge
; Call Current Address Read function.
MOV A, B ; get programmable address
LCALL READ_CURRENT
LJMP X_47 ; exit
X_46:
LCALL STOP
X_47: POP B
RET
START:
; Send START, defined as high-to-low SDA with SCL high.
; Return with SCL, SDA low.
; Returns CY set if bus is not available.
SETB SDA
SETB SCL
; Verify bus available.
JNB SDA, X_40 ; jump if not high
JNB SCL, X_40 ; jump if not high
NOP ; enforce setup delay and cycle delay
CLR SDA
nop ; enforce hold delay
nop ;
nop ;
nop ;
nop ;
CLR SCL
CLR C ; clear error flag
SJMP X_41
X_40:
SETB C ; set error flag
X_41:
RET
STOP:
; Send STOP, defined as low-to-high SDA with SCL high.
; SCL expected low on entry. Return with SCL, SDA high.
CLR SDA
nop ; enforce SCL low and data setup
nop
SETB SCL
nop ; enforce setup delay
nop ;
nop ;
nop ;
nop ;
SETB SDA
RET
SHOUT:
; Shift out a byte to the AT24Cxx, most significant bit first.
; SCL, SDA expected low on entry. Return with SCL low.
; Called with data to send in A.
; Returns CY set to indicate failure by slave to acknowledge.
; Destroys A.
PUSH B
MOV B, #8 ; bit counter
X_42:
RLC A ; move bit into CY
MOV SDA, C ; output bit
nop ; enforce SCL low and data setup
SETB SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
CLR SCL ; drop clock
DJNZ B, X_42 ; next bit
SETB SDA ; release SDA for ACK
nop ; enforce SCL low and tAA
nop ;
SETB SCL ; raise ACK clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
MOV C, SDA ; get ACK bit
CLR SCL ; drop ACK clock
;LCALL ACK
POP B
RET
SHIN:
; Shift in a byte from the AT24Cxx, most significant bit first.
; SCL expected low on entry. Return with SCL low.
; Returns received data byte in A.
; SETB SDA ; make SDA an input
; PUSH B
; MOV B, #8 ; bit count
;X_43:
; nop ; enforce SCL low and data setup
; nop ;
; nop ;
; SETB SCL ; raise clock
; nop ; enforce SCL high
; nop ;
; MOV C, SDA ; input bit
; RLC A ; move bit into byte
; CLR SCL ; drop clock
; DJNZ B, X_43 ; next bit
; POP B
; RET
SETB SDA ;; make SDA an input,使SDA为高,准备读
PUSH B ;
MOV B, #08D ;;;;; bit count,设置位计数器
L2: NOP ;; enforce SCL low and data setup,保持SCL为低且使数据稳定
NOP
SETB SCL ;; raise clock,升高时钟
NOP ;; enforce SCL high,保持SCL为高
NOP
MOV C, SDA ;; input bit,输入位
RLC A ;; move bit into byte,将位移入A
CLR SCL ;; drop clock,降低时钟
DJNZ B, L2 ;; next bit,传送下一位
POP B
RET
NAK:
; Clock out a negative acknowledge bit (high).
; SCL expected low on entry. Return with SCL low, SDA high.
SETB SDA ; NAK bit
nop ; enforce SCL low and data setup
nop ;
SETB SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
nop ;
CLR SCL ; drop clock
RET
ACK:
; Clock out an acknowledge bit (low).
; SCL expected low on entry. Return with SCL, SDA low.
clr SDA ; ACK bit
nop ; enforce SCL low and data setup
nop ;
setb SCL ; raise clock
nop ; enforce SCL high
nop ;
nop ;
Added after 1 minutes:
just a remind, I am not the aurthor of this code.
I just change the code for my application after I read it.
So, I am not copy right holder