EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Need Help to Write and Read 24C04 in Assembly Language

Need Help to Write and Read 24C04 in Assembly Language

|

Microcontroller › 8051 › Need Help to Write and Read 24C04 in Assembly Language

  • This topic has 0 replies, 1 voice, and was last updated 13 years, 6 months ago by surendra kumar sahoo.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • October 31, 2011 at 8:39 am #1367
    surendra kumar sahoo
    Participant

    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.7

    WTCMD EQU 10100000B
    RDCMD EQU 10100001B

    DISPLAY EQU P2

    ;************************************************* ***
    DSEG
    ORG 30H

    ;************************************************* ****
    CSEG

    ORG 00H
    JMP START

    ORG 23H
    JMP SERIAL

    ORG 30H

    START:
    MOV TMOD,#20H
    MOV TH1,#0FDH ; 9600 BAUD
    MOV TL1,#0FDH
    MOV SCON,#50H
    MOV IE,#90H
    SETB TR1

    MOV R0,#30H ; CHAR POINTER OF RAM
    MOV R1,#30H ; DISPLAY CHAR POINTER
    MOV R3,#0 ; CHAR COUNTER
    MOV P2,#00H

    MAIN:
    MOV SP,#80H

    MOV R1,#30H ; GET DATA IN BYTES FROM RAM
    MOV R4,#0A0H ; DATA ADDRESS OF EEPROM
    MOV R6,#30H
    MOV R7,#8 ; NUMBER OF BYTES

    WR_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,#30H

    R_LOOP:
    CALL READ_EEPROM
    CALL ACK

    MOV 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_OUT

    MOV 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
    RETI

    SAVE:
    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 OUT

    B_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 = 0

    OUTS_LOOP:
    RLC A ;SHIFT BIT
    JNC BITLS
    SETB SDA ;DATA = 1
    JMP OUTS_LOOP1 ;CONTINUE

    BITLS:
    CLR SDA ;DATA = 0

    OUTS_LOOP1:
    SETB SCL ;CLOCK HI
    NOP ;NOTE 1
    NOP
    NOP

    CLR SCL ;CLOCK LOW
    DJNZ R2,OUTS_LOOP ;DECREMENT COUNTER
    SETB SDA ;TURN PIN INTO INPUT
    NOP

    SETB SCL ;CLOCK ACK
    NOP
    NOP
    NOP

    CLR 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 COUNT

    OUT_LOOP:
    RLC A ;SHIFT BIT
    JNC BITL
    SETB SDA ;DATA = 1
    JMP OUT_LOOP1 ;CONTINUE

    BITL:
    CLR SDA ;DATA = 0

    OUT_LOOP1:
    SETB SCL ;CLOCK HI
    NOP
    NOP
    NOP

    CLR SCL ;CLOCK LOW
    DJNZ R2,OUT_LOOP ;DECREMENT COUNTER
    SETB SDA ;TURN PIN INTO INPUT
    NOP

    SETB SCL ;CLOCK ACK
    NOP
    NOP
    NOP

    CLR SCL
    RET
    STOP:
    CLR SDA ;STOP CONDITION SET DATA LOW
    NOP
    NOP
    NOP

    SETB SCL ;SET CLOCK HI
    NOP
    NOP
    NOP

    SETB 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
    NOP

    SETB 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 = 0

    ACK_LOOP:
    RLC A ;SHIFT BIT
    JNC ACK_LS
    SETB SDA ;DATA = 1
    JMP ACK_LOOP1 ;CONTINUE

    ACK_LS:
    CLR SDA ;DATA = 0

    ACK_LOOP1:
    SETB SCL ;CLOCK HI
    NOP ;NOTE 1
    NOP
    NOP

    CLR SCL ;CLOCK LOW
    DJNZ R2,ACK_LOOP ;DECREMENT COUNTER
    SETB SDA ;TURN PIN INTO INPUT
    NOP ;NOTE 1

    SETB SCL ;CLOCK ACK
    NOP ;NOTE 1
    NOP
    NOP

    JNB SDA,EXIT ;EXIT IF ACK (WRITE DONE)
    JMP ACK ;START OVER

    EXIT:
    CLR SCL ;CLOCK LOW
    CLR SDA ;DATA LOW
    NOP ;NOTE 1
    NOP
    NOP

    SETB 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,D1

    RET

    END

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Chinese Tarrifs – 104%!?! May 21, 2025
  • An Update On Tarrifs May 21, 2025
  • Tariff Updates from JLCPCB as of Today May 21, 2025
  • Solar lighting motion detector May 21, 2025
  • Telegram Based Alarm - Sensor cable protection May 20, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise