SK equ p1.0 ;serial clock
DI equ p1.1 ;data input
DO equ p1.2 ;data output
CS equ p1.3 ;chip select
EEADR data 50h ;EEADR in 50h
EEBYT data 60h ;EEBYT in 60h
DSEG AT 20H
ORG 20H ; stack start
stack: DS 20H ; stack length
CSEG
ORG 0000H ; start program in 0000H
jmp start ; jump to start routine
USING 0 ; using register bank 0
start:
clr CS ; clear CS
clr SK ; clear serial clock
setb DI ; set data input
setb DO ; set data output
mov EEADR, #55h ; move value 55h into EEADR
mov EEBYT,EEADR ;move EEADR to EEBYT
mov p3,EEBYT ;move EEBYT to port 3
call ewen ; call enable write routine
call eral ; call erase all routine
call EWDS ; call disable writing routine
mov EEADR, #7fh ; address for byte
mov EEBYT, #45h ; byte value
call write ; call write routine
;routine to enable write operations
EWEN:
setb CS ; set chip select
mov dptr, #(10011b SHL 7) ;shift left
mov b, #12 ; move 12 into b
call send ; call send routine
clr CS ; clear chip select
ret ; return
;routine to erase device
eral:
setb CS ; set chip select
mov dptr, #(10010b SHL 7) ; shift left
mov b, #12 ; move 12 into b
call send ; call send routine
clr CS ; clear chip select
ret ; return
;routine to write a byte to EEPROM
write:
setb CS ; set chip select
mov dpl, #101b ; move 5 to dpl
mov b, #3 ; move 3 to b
call send ; call send routine
mov dpl, EEADR ; move EEADR to dpl
mov b, #9 ; number of bits
call send ; call send routine
mov dpl, EEBYT ; move EEBYT into dpl
mov b, #8 ; move 8 into b
call send ; call send routine
clr CS ; clear chip select
ret ; return
; routine to send data
send:
push b ; push b onto stack
mov a, b ; bit count into a
clr c ; clear bit c
subb a, #8 ; subtract 8 from a
jc less1 ; jump if less than 8
jz equal ; jump if equals eight
mov b, a ; move a into b
clr c ; clear bit
subb a, #8 ; subtract 8 from a
jc less2 ; jump if less than eight
jnz great ; jump if greater than eight
mov a, dph ; move dph into a
jmp call ; jump to call
less2:
push b ; push b onto stack
mov a, dph ; move dph into a
shift:
rr a ; rotate a to right
djnz b, shift ; jump if b not zero to shift
pop b ; pop b from stack
call:
call send2 ; call send2 routine
mov b, #8 ; move 8 into b
equal:
mov a, dpl ; move dpl into a
jmp call2 ; jump to call2
less1:
push b ; push b onto stack
mov a, dpl ; move dpl into a
shift2:
rr a ; rotate a to right
djnz b, shift2 ; jump if b not zero to shift2
pop b ; pop b from stack
call2:
call send2 ; call send2 routine
great:
pop b ;pop b from stack
ret ;return
send2:
out:
clr SK ; clear serial clock
rlc a ; move bit
mov DI, c ; move c into data input
nop ; delay min 400 nS
setb SK ; set serial clock
djnz b, out ; next bit / delay min one uS
clr SK ; clear clock
ret ;return
ewds:
setb CS ; CS equals 1
mov dptr, #(10000b SHL 7)
mov b, #12 ;move 12 into b
call send ;call send routine
clr CS ; clear chip select
ret ;return