This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C troubleshooting

hi i ve written the follwing program for interfacing 24c02 eeprom with 8051 using i2c protocol it works in proteous simulation but doesnot work in real time please help me to solve the program, thanks in advance
------------------------------------------------ lcd_output equ P1 rs equ P3.5 r_w equ P3.6 e equ P3.7 temp equ 0feh
;***************************************
;ports used for i2c communication
;*************************************** sda equ P3.3 scl equ P3.4
;****************************************
;***************************************

mov r5,#08h acall startc

mov a,#10100000b ; write command acall send

mov a,#00h ;memory address acall send

mov a,#00h ;value acall send

mov a,#01h ;value acall send

mov a,#02h ;value acall send mov a,#03h ;value acall send mov a,#04h ;value acall send mov a,#05h ;value acall send mov a,#06h ;value acall send mov a,#07h ;value acall send acall stop

acall delay1 acall startc

mov a,#10100000b ; write command acall send ;device address

mov a,#00h ;memory address acall send

acall startc

mov a,#10100001b acall send

l3: acall recv

mov r0,#temp orl a,#30h mov @r0,a

acall display_setup acall display

djnz r5,l3

here: sjmp here
;---command--------------------------------------------------------------------------
command: mov lcd_output,a ;copy reg a to port 1 clr rs ;rs=0 for command clr r_w ;r/w=0 for write setb e ;e=1 for high pulse acall delay1;give lcd some time clr e ;e=0 for h-to-l pulse ret
;---data_display----------------------------------------------------------------------------
data_display: mov lcd_output,a ;copy reg a to port 1 setb rs ;rs=1 for data clr r_w ;r/w=0 for write setb e ;e=1 for high pulse acall delay1 ;give lcd some time clr e ;e=0 for h-to-l pulse ret
;---display_setup-----------------------------------------------------------------------
display_setup: mov a,#38h ;init. lcd 2 lines ,5x7 matrix lcall command ;issue command mov a,#0ch ;lcd on, cursor off lcall command ;issue command mov a,#01h ;clear lcd command lcall command ;issue command ret
;---address----------------------------------------------------------
display: mov r0,#temp mov a,@r0

lcall data_display ret
;---delay_for_busy_flag-------------------------------------------
delay1: mov r7,#0ffh
delay_3: mov r6,#0ffh
delay_4: djnz r6,delay_4 djnz r7,delay_3 ret

;****************************************
;start condition for i2c communication
;****************************************
startc: setb scl setb sda nop nop clr sda nop nop clr scl setb sda ret

;*****************************************
;stop condition for i2c bus
;***************************************** stop: clr scl clr sda setb scl nop nop setb sda nop nop

ret

;*****************************************
;sending data to slave on i2c bus
;*****************************************
send: mov r7,#08h
back: clr scl rlc a mov sda,c setb scl nop nop nop djnz r7,back clr scl nop
nop
setb sda
;l1: jb sda, l1 acall ack ret

;*****************************************
;ack and nak for i2c bus
;*****************************************
ack: clr sda setb scl clr scl setb sda ret

nak: setb sda setb scl clr scl setb scl ret

;*****************************************
;receiving data from slave on i2c bus
;*****************************************
recv: mov r7,#08h
back2: clr scl setb scl mov c,sda rlc a djnz r7,back2 clr scl nop nop setb sda acall ack
;l4: jb sda, l4 ret delay: mov r7,#4h
here1: djnz r7,here1 ret end

  • hi i ve written the follwing program for interfacing 24c02 eeprom with 8051 using i2c protocol it works in proteous simulation but doesnot work in real time please help me to solve the program, thanks in advance
    ------------------------------------------------
                            lcd_output equ P1
                            rs equ P3.5
                            r_w equ P3.6
                            e equ P3.7
                            temp equ 0feh
    ;***************************************
    ;ports used for i2c communication
    ;***************************************
                            sda equ P3.3
                            scl equ P3.4
    ;****************************************
    ;***************************************
    
    
                            mov r5,#08h
                            acall startc
    
                            mov a,#10100000b        ; write command
                            acall send
    
                            mov a,#00h      ;memory address
                            acall send
    
                            mov a,#00h      ;value
                            acall send
    
                            mov a,#01h      ;value
                            acall send
    
                            mov a,#02h      ;value
                            acall send
                            mov a,#03h      ;value
                            acall send
                            mov a,#04h      ;value
                            acall send
                            mov a,#05h      ;value
                            acall send
                            mov a,#06h      ;value
                            acall send
                            mov a,#07h      ;value
                            acall send
                            acall stop
    
                            acall delay1
                            acall startc
    
                            mov a,#10100000b        ; write command
                            acall send      ;device address
    
                            mov a,#00h      ;memory address
                            acall send
    
                            acall startc
    
                            mov a,#10100001b
                            acall send
    
    
    l3:                     acall recv
    
    
    
                            mov r0,#temp
                            orl a,#30h
                            mov @r0,a
    
                            acall display_setup
                            acall display
    
                            djnz r5,l3
    
    here:           sjmp here
    ;---command--------------------------------------------------------------------------
    command:
                            mov lcd_output,a ;copy reg a to port 1
                            clr rs ;rs=0 for command
                            clr r_w ;r/w=0 for write
                            setb e ;e=1 for high pulse
                            acall delay1;give lcd some time
                            clr e ;e=0 for h-to-l pulse
                            ret
    ;---data_display----------------------------------------------------------------------------
    data_display:
                            mov lcd_output,a ;copy reg a to port 1
                            setb rs ;rs=1 for data
                            clr r_w ;r/w=0 for write
                            setb e ;e=1 for high pulse
                            acall delay1 ;give lcd some time
                            clr e ;e=0 for h-to-l pulse
                            ret
    ;---display_setup-----------------------------------------------------------------------
    display_setup:
                            mov a,#38h                                                               ;init. lcd 2 lines ,5x7 matrix
                            lcall command                                                    ;issue command
                            mov a,#0ch                                                               ;lcd on, cursor off
                            lcall command                                                    ;issue command
                            mov a,#01h                                                               ;clear lcd command
                            lcall command                                                                                   ;issue command
                            ret
    ;---address----------------------------------------------------------
    display:
                            mov r0,#temp
                            mov a,@r0
    
                            lcall data_display
                            ret
    ;---delay_for_busy_flag-------------------------------------------
    delay1:          mov r7,#0ffh
    delay_3:         mov r6,#0ffh
    delay_4:         djnz r6,delay_4
                            djnz r7,delay_3
                            ret
    
    
    
    
    ;****************************************
    ;start condition for i2c communication
    ;****************************************
    startc:         setb scl
                            setb sda
                            nop
                            nop
                            clr sda
                            nop
                            nop
                            clr scl
                            setb sda
                            ret
    
    ;*****************************************
    ;stop condition for i2c bus
    ;*****************************************
                            stop:
                            clr scl
                            clr sda
                            setb scl
                            nop
                            nop
                            setb sda
                            nop
                            nop
    
                            ret
    
    ;*****************************************
    ;sending data to slave on i2c bus
    ;*****************************************
    send:           mov r7,#08h
    back:           clr scl
                            rlc a
                            mov sda,c
                            setb scl
                            nop
                            nop
                            nop
                            djnz r7,back
                            clr scl
                            nop
    nop
    setb sda
    ;l1:                    jb sda, l1
                            acall ack
                            ret
    
    ;*****************************************
    ;ack and nak for i2c bus
    ;*****************************************
    ack:
                            clr sda
                            setb scl
                            clr scl
                            setb sda
                            ret
    
    nak:
                            setb sda
                            setb scl
                            clr scl
                            setb scl
                            ret
    
    ;*****************************************
    ;receiving data from slave on i2c bus
    ;*****************************************
    recv:   mov r7,#08h
    back2:  clr scl
                    setb scl
                    mov c,sda
                    rlc a
                    djnz r7,back2
                    clr scl
                    nop
                    nop
                    setb sda
                    acall ack
    ;l4:            jb sda, l4
                    ret
                    delay:
                    mov r7,#4h
    here1:  djnz r7,here1
                    ret
                    end
    

  • it works in proteous simulation but doesnot work in real time
    you do have pullup resistors on the signals, I hope

  • By "real time", I presume you mean "in the real hardware"?

    If it works in simulation, but not real hardware, that should give you some clues as to what to look for - think about what kind(s) of thing(s) could happen in real hardware that wouldn't affect a simulation?

    Remember: the most important tool for debugging is the one between your ears...

    What checks have you done on your hardware to verify that it's correct?

  • hi, sorry for the belated reply, actually now the above state program works well in real time(in hardware) but in simulation(proteus) same digit is repeated twice,but I2C debugger says correct instruction...what could be the reason for repeating the same digit twice like 11335577