Microcontroller › 8051 › Need Help to Write and Read 24C04 in Assembly Language
- This topic has 0 replies, 1 voice, and was last updated 12 years, 10 months ago by surendra kumar sahoo.
-
AuthorPosts
-
October 31, 2011 at 8:39 am #1367surendra kumar sahooParticipant
Dear Sirs.
Please help me by guiding me to correct the Code i have written.
I am using Hyper Terminal for inputting 8 Bytes of data to 8051,
which has to be written in to EEPROM 24C04 at ADDR 0A0H
From RAM location 30H to 37H.
After writing cycle is completed, I want to read from EEPROM
and again save in RAM starting location from 30H. These data
has to be displayed at PORT2.When simulating in Proteus I am not getting any Display at Port 2
The DATA from Hyper Terminal is showing in RAM locations
as well as in EEPROM but the EEPROM DATA are replaced
by 00 after a while.
I think i am not able to stop the write cycle. Please Help.Thanks
S.K.SAHOO
I am posting the code:
CODE:
;************************************************* ***
; Write and Read EEPROM from addr 0A0H
; and Show the values at P2
; NAME: WRITE_READ_EEPROM.ASM
;************************************************* ***SCL EQU P3.6
SDA EQU P3.7WTCMD EQU 10100000B
RDCMD EQU 10100001BDISPLAY EQU P2
;************************************************* ***
DSEG
ORG 30H;************************************************* ****
CSEGORG 00H
JMP STARTORG 23H
JMP SERIALORG 30H
START:
MOV TMOD,#20H
MOV TH1,#0FDH ; 9600 BAUD
MOV TL1,#0FDH
MOV SCON,#50H
MOV IE,#90H
SETB TR1MOV R0,#30H ; CHAR POINTER OF RAM
MOV R1,#30H ; DISPLAY CHAR POINTER
MOV R3,#0 ; CHAR COUNTER
MOV P2,#00HMAIN:
MOV SP,#80HMOV R1,#30H ; GET DATA IN BYTES FROM RAM
MOV R4,#0A0H ; DATA ADDRESS OF EEPROM
MOV R6,#30H
MOV R7,#8 ; NUMBER OF BYTESWR_LOOP:
CALL WRITE_EEPROM
CALL ACK
DJNZ R7,WR_LOOP
CALL STOP;************************************************* ***********************************
; WRITE LOOP ENDS AND READ LOOP STARTS FROM HERE
;************************************************* ***********************************MOV R7,#8 ; NUMBER OF BYTES
MOV R1,#30HR_LOOP:
CALL READ_EEPROM
CALL ACKMOV R7,#8 ; NUMBER OF BYTES
MOV R1,#30H;************************************************* ***********************************
; READ LOOP ENDS HERE
;************************************************* ***********************************DISP_OUT:
MOV A,@R1
MOV P2,A
CALL DELAY
INC R1
DJNZ R7,DISP_OUTMOV R7,#8 ; NUMBER OF BYTES
MOV R1,#30H
SJMP DISP_OUT; ************************************************** **************************
; SERIAL ISR STARTS FROM HERE
;************************************************* ****************************SERIAL:
JNB RI,$
MOV A,SBUF
CJNE A,#0DH,SAVE
CLR RI
RETISAVE:
MOV @R0,A
INC R0
CLR RI
JMP SERIAL;************************************************* ****************************
; WRITE DATA IN EEPROM ADDR 20H
;************************************************* ****************************WRITE_EEPROM:
MOV A,#WTCMD
CALL OUTS
MOV A,R4 ; R4 HOLDS THE ADDRESS IN EEPROM (0a0H)
CALL OUTB_W_LOOP:
MOV A,@R1 ;; GET DATA FROM RAM
CALL OUT
INC R1
DJNZ R7,B_W_LOOP
CALL STOP
RET;************************************************* **********************
; THIS SUB-ROUTINE SENDS OUT CONTENTS OF THE ACCUMULATOR
; to the EEPROM and includes START condition.
;************************************************* **********************OUTS:
MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNT
SETB SDA ;INSURE DATA IS HI
SETB SCL ;INSURE CLOCK IS HI
NOP ;NOTE 1
NOP
NOP
CLR SDA ;START CONDITION — DATA = 0
NOP
NOP
NOP
CLR SCL ;CLOCK = 0OUTS_LOOP:
RLC A ;SHIFT BIT
JNC BITLS
SETB SDA ;DATA = 1
JMP OUTS_LOOP1 ;CONTINUEBITLS:
CLR SDA ;DATA = 0OUTS_LOOP1:
SETB SCL ;CLOCK HI
NOP ;NOTE 1
NOP
NOPCLR SCL ;CLOCK LOW
DJNZ R2,OUTS_LOOP ;DECREMENT COUNTER
SETB SDA ;TURN PIN INTO INPUT
NOPSETB SCL ;CLOCK ACK
NOP
NOP
NOPCLR SCL
RET;************************************************* *********************
; THIS SUB-ROUTINE SENDS OUT CONTENTS OF ACCUMLATOR TO EEPROM
; without sending a START condition.
;************************************************* *********************OUT:
MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNTOUT_LOOP:
RLC A ;SHIFT BIT
JNC BITL
SETB SDA ;DATA = 1
JMP OUT_LOOP1 ;CONTINUEBITL:
CLR SDA ;DATA = 0OUT_LOOP1:
SETB SCL ;CLOCK HI
NOP
NOP
NOPCLR SCL ;CLOCK LOW
DJNZ R2,OUT_LOOP ;DECREMENT COUNTER
SETB SDA ;TURN PIN INTO INPUT
NOPSETB SCL ;CLOCK ACK
NOP
NOP
NOPCLR SCL
RET
STOP:
CLR SDA ;STOP CONDITION SET DATA LOW
NOP
NOP
NOPSETB SCL ;SET CLOCK HI
NOP
NOP
NOPSETB SDA ;SET DATA HIGH
RET;************************************************* ****************************
; READ DATA FROM EEPROM
;************************************************* ****************************
READ_EEPROM:
MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
CALL OUTS ;SEND IT
MOV A,R4 ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
MOV A,#RDCMD ;LOAD READ COMMAND
CALL OUTS ;SEND IT
READ_BACK:
CALL IN ;READ DATA
MOV @R1,a ;STORE DATA
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,RDB_LOOP ;DECREMENT LOOP COUNTER
CALL STOP ;IF DONE, ISSUE STOP CONDITION
RET ;DONE, EXIT ROUTINE
RDB_LOOP:
CLR SDA ;NOT DONE, ISSUE ACK
SETB SCL
NOP
NOP
NOP
NOP
NOP
CLR SCL
JMP READ_BACK ;CONTINUE WITH READS;************************************************* *********************
; THIS ROUTINE READS IN A BYTE FROM THE EEPROM
; and stores it in the accumulator
;************************************************* *********************IN:
MOV R2,#8 ;LOOP COUNT
SETB SDA ;SET DATA BIT HIGH FOR INPUT
IN_LOOP:
CLR SCL ;CLOCK LOW
NOP ;NOTE 1
NOP
NOP
NOPSETB SCL ;CLOCK HIGH
CLR C ;CLEAR CARRY
JNB SDA,IN_LOOP1 ;JUMP IF DATA = 0
CPL C ;SET CARRY IF DATA = 1
IN_LOOP1:
RLC A ;ROTATE DATA INTO ACCUMULATOR
DJNZ R2,IN_LOOP ;DECREMENT COUNTER
CLR SCL ;CLOCK LOW
RET;************************************************* ********************
; This routine test for WRITE DONE condition
; by testing for an ACK.
; This routine can be run as soon as a STOP condition
; has been generated after the last data byte has been sent
; to the EEPROM. The routine loops until an ACK is received from
; the EEPROM. No ACK will be received until the EEPROM is done with
; the write operation.
;************************************************* ********************
ACK:
MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNT
CLR SDA ;START CONDITION — DATA = 0
NOP ;NOTE 1
NOP
NOP
CLR SCL ;CLOCK = 0ACK_LOOP:
RLC A ;SHIFT BIT
JNC ACK_LS
SETB SDA ;DATA = 1
JMP ACK_LOOP1 ;CONTINUEACK_LS:
CLR SDA ;DATA = 0ACK_LOOP1:
SETB SCL ;CLOCK HI
NOP ;NOTE 1
NOP
NOPCLR SCL ;CLOCK LOW
DJNZ R2,ACK_LOOP ;DECREMENT COUNTER
SETB SDA ;TURN PIN INTO INPUT
NOP ;NOTE 1SETB SCL ;CLOCK ACK
NOP ;NOTE 1
NOP
NOPJNB SDA,EXIT ;EXIT IF ACK (WRITE DONE)
JMP ACK ;START OVEREXIT:
CLR SCL ;CLOCK LOW
CLR SDA ;DATA LOW
NOP ;NOTE 1
NOP
NOPSETB SCL ;CLOCK HIGH
NOP
NOP
SETB SDA ;STOP CONDITION
RET;~~~~~~~DELAY SUB-ROUTINE~~~~~~~~~~~~~~
DELAY:
MOV 50H,#25
D1:
MOV 51H,#50
D2:
MOV 52H,#200
DJNZ 52H,$
DJNZ 51H,D2
DJNZ 50H,D1RET
END
-
AuthorPosts
- You must be logged in to reply to this topic.