最近在弄51读写24c系列,按照
数据手册写出了
程序,但是奇怪的是可以读出数据却没法写入数据
还请大家赐教
硬件环境是at89s52,at24c08a,12mhz晶振
------------------------
;定义区
EEPROM_WP BIT P2.6
EEPROM_SCL BIT P2.5
EEPROM_SDA BIT P2.4
;代码区
org 0000h
jmp main
org 0080h
;r/w 24c08
main:mov sp,#60h
call init_eeprom
call delay_10ms
jmp test_eeprom
test_eeprom: clr p0.1
mov 0x49,#30h
mov 0x4a,#31h
mov r2,#1
mov 0x30,#81h
mov 0x31,#20h
call write_eeprom
mov 0x49,#31h
mov 0x4a,#32h
mov r2,#1
call read_eeprom
mov p1,0x32
jmp test_eeprom
init_eeprom:
mov 0x47,#0a0h
mov 0x48,#0a1h
;clr p2.0
;clr p2.1
;clr p2.2
;clr p2.3
setb p2.7
;clr EEPROM_WP
ret
read_eeprom:
call start_eeprom
mov a,0x47
call send_eeprom_byte
jc read_eeprom
mov r0,0x49
mov a,@r0
call send_eeprom_byte
jc read_eeprom
call start_eeprom
mov a,0x48
call send_eeprom_byte
jc read_eeprom
mov r0,0x4a
read_eeprom_loop:call read_eeprom_byte
mov @r0,a
inc r0
djnz r2,read_eeprom_loop
call stop_eeprom
jmp delay_10ms
write_eeprom: clr EEPROM_WP
call start_eeprom
mov a,0x47
call send_eeprom_byte
jc write_eeprom
mov r0,0x4a
mov a,@r0
call send_eeprom_byte
jc write_eeprom
mov r0,0x49
write_eeprom_loop:
mov a,@r0
call send_eeprom_byte
jc write_eeprom
inc r0
djnz r2,write_eeprom_loop
call stop_eeprom
setb EEPROM_WP
jmp delay_10ms
read_eeprom_byte: mov r4,#8
clr EEPROM_SCL
setb EEPROM_SDA
read_eeprom_byte_loop:setb EEPROM_SCL
mov c,EEPROM_SDA
rlc a
clr EEPROM_SCL
djnz r4,read_eeprom_byte_loop
clr EEPROM_SDA
ret
send_eeprom_byte:
mov r4,#8
send_eeprom_byte_loop:rlc a
clr EEPROM_SCL
mov EEPROM_SDA,c
setb EEPROM_SCL
djnz r4,send_eeprom_byte_loop
jmp waitAck
waitAck:
clr EEPROM_SCL
nop
setb EEPROM_SDA
setb EEPROM_SCL
mov c,EEPROM_SDA
jc waitAck
clr EEPROM_SDA
clr EEPROM_SCL
ret
start_eeprom:
setb EEPROM_SDA
setb EEPROM_SCL
clr EEPROM_SDA
clr EEPROM_SCL
ret
stop_eeprom:
clr EEPROM_SDA
setb EEPROM_SCL
setb EEPROM_SDA
ret
delay:
nop
nop
nop
nop
nop
ret
delay_10ms:
mov r4,#50
delay_10ms_loop: mov r5,#200
djnz r5,$
djnz r4,delay_10ms_loop
ret
此帖出自
小平头技术问答
一周热门 更多>