; 24c02的简单操作
;www.808cn.com
scl_2402 bit p3.5
sda_2402 bit p3.7
sp_begain equ 60h
org 0
mov sp,#sp_begain
acall delay
acall delay
acall delay
mov 30h,#0
mov 31h,#1
mov 32h,#2
mov 33h,#3
mov 34h,#4
mov 35h,#5
mov 36h,#6
mov 37h,#7
write: clr a ;address
mov r0,#30h ;point
mov r1,#8 ;sum
acall wrs_2402
cjne r1,#0ffh,next0
sjmp err_2402
next0: acall delay
read: clr a ;address
mov r0,#38h ;point
mov r1,#8 ;sum
acall rds_2402
cjne r1,#0ffh,next1
sjmp err_2402
next1: acall delay
write0: mov a,#8 ;address
mov r0,#38h ;point
mov r1,#8 ;sum
acall wrs_2402
cjne r1,#0ffh,next2
sjmp err_2402
next2: acall delay
sjmp $
err_2402:sjmp $
delay: mov r6,#20 ;24M--5ms----24c02的写入时间间隔
delay_0:djnz r7,$
djnz r6,delay_0
ret
;---------------------------------------------
wI2c equ 0a0h ;器件地址及写操作
rI2c equ 0a1h ;器件地址及读操作
sta_2402: ;启始
setb sda_2402
setb scl_2402
sjmp $+2 ;24M至少间隔周期
sjmp $+2
clr sda_2402
sjmp $+2
sjmp $+2
clr scl_2402
sjmp $+2
sjmp $+2
ret
stp_2402: ;结束
clr sda_2402
setb scl_2402
sjmp $+2
sjmp $+2
setb sda_2402
sjmp $+2
sjmp $+2
ret
wr_2402: ;写单个字节
mov b,#8
wr_2402_0: rlc a
jc wr_2402_1
clr sda_2402
sjmp wr_2402_2
wr_2402_1: setb sda_2402
wr_2402_2: setb scl_2402
sjmp $+2
sjmp $+2
clr scl_2402
sjmp $+2
sjmp $+2
djnz b,wr_2402_0
nmack_2402: setb sda_2402
nmack_2402_0:setb scl_2402
sjmp $+2
sjmp $+2
mov c,sda_2402
clr scl_2402
sjmp $+2
sjmp $+2
ret
mack_2402: clr sda_2402
sjmp nmack_2402_0
rd_2402: ;读单个字节
mov b,#8
setb sda_2402
rd_2402_0: setb scl_2402
sjmp $+2
sjmp $+2
mov c,sda_2402
rlc a
clr scl_2402
sjmp $+2
sjmp $+2
djnz b,rd_2402_0
ret
;---------------------------------------
;24c02的页写为8个字节。
;wrs_2402 写操作(;a--2402指针,r0--变量基址,r1--变量个数少于等于8)
;rds_2402 读操作(;a--2402指针,r0--变量基址,r1--变量个数少于等于8)
;使用a,b,psw,r0,r1 五个变量
wa_2402: push aCC
acall sta_2402
mov a,#wI2c
acall wr_2402
pop acc
jc err_2402
acall wr_2402
ret
err_2402: mov r1,#0ffh
dec sp
dec sp
ret ;2402硬件错误返回
wrs_2402: ;写多字节
acall wa_2402
wrs_2402_0:mov a,@r0
acall wr_2402
inc r0
djnz 01h,wrs_2402_0
acall stp_2402
ret
rds_2402: ;读多字节
acall wa_2402
acall sta_2402
mov a,#rI2c
acall wr_2402
rds_2402_0:acall rd_2402
mov @r0,a
djnz 01h,rds_2402_1
acall nmack_2402
acall stp_2402
ret
rds_2402_1:acall mack_2402
inc r0
sjmp rds_2402_0