hi all, i'm stuck with this eeprom for last one week. Initially i could write and read from EEPROM (AT24C16), but any one in an execution, that is if i write a byte then my next function of reading a byte wont work and vice versa. And the page read/write was also not working. So i started to sniff around my 'stop' protocol for I2C, but to no avail. I continued troubleshooting and now i ended up with NOTHING. I've lost even my previous output. No read and no write. Im using AT89C2051 micro-controller. Code is provided below. Any help will be greatly appreciated.
; program for AT89C2051
; layout details
; write protect pin (eeprom) is connected to gnd
; Xtal used is 20Mhz
; R4 memmory address1
; R5 eeprom data
; status led connected to p1.3 is sinked for switching ON
; SDA and SCL pins are provided with pull ups - 4.7k
led EQU P1.3 ; Led for the status indication
sda EQU P3.7 ;
scl EQU P1.2 ;
;===============================================
ORG 0000h
sjmp start
;===============================================
ORG 0030h ; Initialization
start:
mov sp, #30h ; moving the stack pointer
setb led ; put the LED off
;======================================================== Main Program
mov r4,#00h ;mem location
mov r5,#14h ;data
lcall write_data
mov r4,#01h ;mem location
mov r5,#15h ;data
lcall write_data
clr led
sjmp $
;==========================================================
;=========================================================
; Sub routines for I2C communication (EEPROM)
;=========================================================
write_data:
acall eeprom_start
mov a,#0a0h
acall send_data
mov a,r4 ;location address
acall send_data
mov a,r5 ;data to be send
lcall send_data
acall eeprom_stop
ret
;=========================================================
read_data:
acall eeprom_start
mov a,#0a0h
acall send_data
mov a,r4 ;location address
acall send_data
acall eeprom_start
mov a,#0a1h
acall send_data
acall get_data
acall eeprom_stop
ret
;=========================================================
eeprom_start:
setb scl
nop
setb sda
nop
nop
clr sda
nop
clr scl
nop
nop
nop
ret
;=========================================================
eeprom_stop: setb sda
nop
nop
setb scl
nop
nop
clr scl
nop
nop
nop
ret
;=========================================================
send_data: mov r7,#00h
senda: rlc a
mov sda,c
acall clock
inc r7
cjne r7,#08,senda
setb sda
nop
setb scl
jb sda, $
clr scl
nop
nop
nop
; acall eeprom_delay
ret
;=========================================================
get_data:
mov r7,#00h
setb sda
get: setb scl
nop
nop
mov c,sda
rlc a
inc r7
clr scl
nop
nop
nop
cjne r7,#08,get
mov r1,a
ret
;=========================================================
clock : setb scl
nop
nop
clr scl
nop
nop
nop
ret
;=========================================================
eeprom_delay : mov r7,#16 ;delay of 4.9 msec
eepromd: mov r2,#0ffh
djnz r2,$
djnz r7,eepromd
ret
;=====================================================
/*
ack:
setb sda
nop
setb scl
nop
nop
clr sda
nop
nop
clr scl
nop
nop
nop
ret
;=====================================================
no_ack:
clr sda
setb scl
nop
nop
setb sda
clr scl
nop
nop
nop
ret */
;=====================================================
end;
; layout details
; write protect pin (eeprom) is connected to gnd
; Xtal used is 20Mhz
; R4 memmory address1
; R5 eeprom data
; status led connected to p1.3 is sinked for switching ON
; SDA and SCL pins are provided with pull ups - 4.7k
led EQU P1.3 ; Led for the status indication
sda EQU P3.7 ;
scl EQU P1.2 ;
;===============================================
ORG 0000h
sjmp start
;===============================================
ORG 0030h ; Initialization
start:
mov sp, #30h ; moving the stack pointer
setb led ; put the LED off
;======================================================== Main Program
mov r4,#00h ;mem location
mov r5,#14h ;data
lcall write_data
mov r4,#01h ;mem location
mov r5,#15h ;data
lcall write_data
clr led
sjmp $
;==========================================================
;=========================================================
; Sub routines for I2C communication (EEPROM)
;=========================================================
write_data:
acall eeprom_start
mov a,#0a0h
acall send_data
mov a,r4 ;location address
acall send_data
mov a,r5 ;data to be send
lcall send_data
acall eeprom_stop
ret
;=========================================================
read_data:
acall eeprom_start
mov a,#0a0h
acall send_data
mov a,r4 ;location address
acall send_data
acall eeprom_start
mov a,#0a1h
acall send_data
acall get_data
acall eeprom_stop
ret
;=========================================================
eeprom_start:
setb scl
nop
setb sda
nop
nop
clr sda
nop
clr scl
nop
nop
nop
ret
;=========================================================
eeprom_stop: setb sda
nop
nop
setb scl
nop
nop
clr scl
nop
nop
nop
ret
;=========================================================
send_data: mov r7,#00h
senda: rlc a
mov sda,c
acall clock
inc r7
cjne r7,#08,senda
setb sda
nop
setb scl
jb sda, $
clr scl
nop
nop
nop
; acall eeprom_delay
ret
;=========================================================
get_data:
mov r7,#00h
setb sda
get: setb scl
nop
nop
mov c,sda
rlc a
inc r7
clr scl
nop
nop
nop
cjne r7,#08,get
mov r1,a
ret
;=========================================================
clock : setb scl
nop
nop
clr scl
nop
nop
nop
ret
;=========================================================
eeprom_delay : mov r7,#16 ;delay of 4.9 msec
eepromd: mov r2,#0ffh
djnz r2,$
djnz r7,eepromd
ret
;=====================================================
/*
ack:
setb sda
nop
setb scl
nop
nop
clr sda
nop
nop
clr scl
nop
nop
nop
ret
;=====================================================
no_ack:
clr sda
setb scl
nop
nop
setb sda
clr scl
nop
nop
nop
ret */
;=====================================================
end;