;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DELAYNS:
;
MOV DR7,#00H
LOOPD:
INC DR7
MOV A,DR7
CJNE A,#0FFH,LOOPD
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DELAYRS:
MOV DR6,#046H
MOV DR5,#001H
LOOPC:
JB START_STOP_INT, RET_DELAY
JB MENU_INT, RET_DELAY
INC DR6
ACALL DELAYNS
MOV A,DR6
JNZ LOOPC
DEC DR5
MOV A,DR5
JNZ LOOPC
RET_DELAY:
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
READ_EEPROM:
ACALL DELAYRS
MOV R0, #42H ;start writing from 42h location of 89c51 RAM
MOV EE_ADDR, #00H ;start writing to 00h location of EEPROM
MOV R3, #02H ;to read 2-bytes, i.e from 42h to 43h,
BACK_READ:
ACALL EE_READ
MOV @R0, EE_DATA
INC R0
INC EE_ADDR
DJNZ R3, BACK_READ
MOV R0, #42H
MOV EE_ADDR, #00H
MOV R3, #02H
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
WRITE_EEPROM:
MOV R0, #42H
MOV EE_ADDR, #00H
MOV R3, #02H
BACK_WRITE:
MOV EE_DATA, @R0
ACALL EE_WRITE
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
ACALL DELAYNS
INC R0
INC EE_ADDR
DJNZ R3, BACK_WRITE
MOV R0, #42H
MOV EE_ADDR, #00H
MOV R3, #02H
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EE_WRITE: ACALL EE_START ; SEND A START FLAG TO THE EEPROM
MOV A,#0A0H ; SPECIFY A WRITE EEPROM @ ADDRESS 0H
ACALL SHOUT ; SHIFT OUT THE DEVICE ADDRESS
JC WR_ABORT ; ABORT IF NO ACK FROM EEPROM
MOV A,EE_ADDR ; GET EEPROM MEMORY ADDRESS
ACALL SHOUT ; SHIFT OUT THE MEMORY ADDRESS
JC WR_ABORT ; ABORT IF NO ACK FROM EEPROM
MOV A, EE_DATA ; GET THE DATA TO BE WRITTEN
ACALL SHOUT ; SHIFT OUT THE DATA
JC WR_ABORT ;
CLR C ;
WR_ABORT:
ACALL EE_STOP ; SEND STOP CONDITION TO EEPROM
; WAIT FOR WRITE TIME OF THE 24C02 {10ms}
; THE EEPROM TAKES 10ms TO INTERNALLY STORE THE DATA. YOU CAN EITHER
; PUT THE MICROCONTROLLER IN A WAIT STATE, OR CONTINUE WITH EXECUTION,
; KEEPING IN MIND THAT THE EEPROM DATA IS NOT STORED FOR 10ms!
RET ; GO BACK TO MAIN PROGRAM
; READ THE EEPROM DATA - FIRST PERFORM 'DUMMY WRITE'
EE_READ:
MOV EE_DATA,#00H ; CLEAR OLD DATA
ACALL EE_START ; SEND A START FLAG TO EEPROM
MOV A,#0A0H ; SPECIFY A WRITE TO EEPROM @ ADDRESS 0H
ACALL SHOUT ; PERFORM 'DUMMY WRITE'
JC RD_ABORT ; ABORT IF NO ACK
MOV A,EE_ADDR ; LOAD EEPROM MEMORY LOCATION
; FROM WHICH TO READ
ACALL SHOUT ; WRITE EEPROM MEMORY LOCATION
JC RD_ABORT ; ABORT IF NO ACK
; NOW READ THE DATA!
ACALL EE_START ; SEND A START FLAG
MOV A,#0A1H ; SPECIFY A READ FROM EEPROM
ACALL SHOUT ; SHIFT OUT EEPROM ADDRESS
JC RD_ABORT ; ABORT IF NO ACK
ACALL SHIN ; SHIFT IN THE DATA FROM EEPROM
MOV EE_DATA,A ; STORE THE DATA
ACALL NAK ; SEND A NAK (NO ACKNOWLEDGE) TO THE
; EEPROM
CLR C ; CLEAR ERROR FLAG
RD_ABORT:
ACALL EE_STOP ; ALL DONE
RET ;
; EE_START BIT-BANGS A START SEQUENCE TO EEPROM (HI-TO-LOW SDA TRANSITION
; WITH SCL HIGH).
EE_START:
SETB SDA
SETB SCL ; SET BOTH BITS
NOP ; DELAY
CLR SDA ; START CONDITION; SDA HI TO LOW TRANSITION
NOP
NOP ; EEPROM ACCESS TIME DELAY
CLR SCL
CLR C ; CLEAR ERROR FLAG
RET ; ALL DONE
; EE_STOP SENDS A STOP SEQUENCE TO THE EEPROM (LOW-TO-HIGH SDA TRANSITION
; WITH SCL HIGH).
EE_STOP:
CLR SDA
NOP
NOP
SETB SCL
NOP
NOP ; SETUP TIME DELAY
SETB SDA ; SEND A STOP CONDITION
RET
; SHOUT SHIFTS DATA OUT TO THE EEPROM
SHOUT:
PUSH B
MOV B,#8 ; SAVE B AND LOAD BIT COUNT
EEOUT:
RLC A ; SHIFT BIT LEFT (RLC=ROTATE LEFT THROUGH
; CARRY)
MOV SDA,C ; GET DATA BIT FROM CARRY
NOP
SETB SCL ; CLOCK IN 1-BIT
NOP ; CLOCK HIGH TIME
CLR SCL ; CLOCK IS NOW LOW
DJNZ B,EEOUT ; DO IT 8 TIMES
SETB SDA ; RELEASE SDA FOR ACK
NOP
NOP
SETB SCL ; ACK CLOCK
NOP
MOV C,SDA ; GET THE ACK
CLR SCL ; CLEAR THE CLOCK BIT
POP B ; RESTORE WHATEVER B WAS
RET
; SHIN SHIFT DATA IN FROM THE EEPROM
SHIN:
SETB SDA ; MAKE SDA AN INPUT
PUSH B
MOV B,#8 ; SAVE B AND SET BIT COUNTER
EEIN:
NOP
SETB SCL ; SET CLOCK
NOP
NOP ; EEPROM ACCESS TIME
SETB SDA ; SET = 1 SO USED AS INPUT
MOV C,SDA ; READ 1-BIT
RLC A ; SHIFT BIT LEFT
CLR SCL ; CLEAR CLOCK BIT
DJNZ B,EEIN ; GET NEXT BIT IF LESS THAN 8 BITS READ
POP B
RET
; ACK SENDS AN EEPROM ACKNOWLDEGE
ACK:
CLR SDA
NOP
NOP
SETB SCL ; CLOCK THE ACK
NOP
CLR SCL ; BRING CLOCK LOW
RET
NAK: ; NAK SENDS A NO ACKNOWLEDGE
SETB SDA
NOP
NOP
SETB SCL ; CLOCK THE NAK
NOP
CLR SCL ; BRING CLOCK LOW
RET
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx