
call cdcreset
call cdcint
call cap

cap:
setb i2cbusy
mov i2cadd,#90h
mov cdcadd,#01h          ;first address read
mov i2cdp,#40h
mov i2ctask,#83h         ;8= read 3= three bytes
mov a,i2ctask
anl a,#0fh
mov i2cdatacnt,a				
mov ssdat,#00h
orl sscon,#20h
jb i2cbusy,$
ret

;------
;AD reset

jb i2cbusy,$
setb i2cbusy
mov i2cadd,#90h
mov cdcadd,#0bfh
mov i2ctask,#01h
mov ssdat,#00h
orl sscon,#20h
ret

;------
;AD initialization

jb i2cbusy,$
setb i2cbusy
mov 40h,#81h
mov i2cadd,#90h
mov cdcadd,#07h
mov i2cdp,#40h
mov i2ctask,#00h
mov ssdat,#00h
orl sscon,#20h
call delay50

jb i2cbusy,$ 
setb i2cbusy
mov 40h,#01h
mov i2cadd,#90h
mov cdcadd,#0ah
mov i2cdp,#40h
mov i2ctask,#00h
mov ssdat,#00h
orl sscon,#20h
jb i2cbusy,$ 
call delay50
ret


;-----
;i2c interrupt

i2c:
push acc
push psw
push 00h
push 06h
push 07h


mov r7,sscs

cjne r7,#00,end00
clr i2cbusy
jmp i2cexit
end00:

cjne r7,#08,end08
anl sscon,#~20h
mov ssdat,i2cadd
orl sscon,#04h

jmp i2cexit

end08:
cjne r7,#10h,end10

anl sscon,#~20h
inc i2cadd
mov ssdat,i2cadd
orl sscon,#04h
jmp i2cexit

end10:
cjne r7,#18h,end18


mov ssdat,cdcadd
orl sscon,#04h
orl i2ctask,#20h
jmp i2cexit

end18:
cjne r7,#28h,end28

mov a,i2ctask
jnb acc.7,writedata
orl sscon,#20h
jmp i2cexit
writedata:
mov a,i2ctask
jb acc.0,sendstop
jb acc.4,sendstop
mov ssdat,40h

orl sscon,#04h
orl i2ctask,#10h
jmp i2cexit
sendstop:
orl sscon,#10h 				;envia STOP
orl i2ctask,#10h
clr i2cbusy
jmp i2cexit

end28:
cjne r7,#40h,end40
mov a,#'A'
call dispchar

djnz i2cdatacnt,j8
anl sscon,#0c3h
jmp i2cexit
j8:
orl sscon,#04h
jmp i2cexit

end40:
cjne r7,#50h,end50

mov r0,i2cdp
mov a,ssdat
mov @r0,a
call dispascii
mov a,#'a'
call dispchar
inc i2cdp
djnz i2cdatacnt,j7
anl sscon,#0c3h
jmp i2cexit

j7:
orl sscon,#04h
jmp i2cexit


end50:
cjne r7,#58h,end58


mov r0,i2cdp
mov a,ssdat
mov @r0,a
call dispascii
mov a,#'n'
call dispchar
inc i2cdp
orl sscon,#10h
clr i2cbusy
mov a,#'P'
call dispchar


end58:
i2cexit:
anl sscon,#~08h
pop 07h
pop 06h
pop 00h
pop psw
pop acc
reti
