  
at reset, start up with :

setb	MOSI			;
setb	MISO			;
clr	SCK			;
clr	RST			;	


;*************************************************
; Prepare the target AT89S52 for programming.
;enter here with the rst low, sck low, MOSI & MISO high.
En_pg_Mode:
	setb RST 	; force target into reset 		
	call delay1ms 	; wait 1 ms

			; Enable writes to code memory.
	mov a, #0ACh	; send first byte of enable code
	call shout 	;
	mov a, #053h	; send second byte
	call shout 	;
	mov a, #055h	; send dummy byte
	call shout 	;

        mov a, #069h	; send fourth byte
	call shout 	;according to the datasheet
	ret             ;the last byte = output 69h

;when returning back to main,from each routine,
;RST is taken low again.
;*************************************************

Read_Sig:
	mov	dptr,#Sigbits_str	;
	lcall	OutStr		        ;

	mov	r1,#000h	;
	mov	r7,#003h		;

Read_Sig1:
	mov	a,#028h		;command byte
	lcall	shout		;
	
	mov	a,r1		;address byte

;???not sure on the next instruction
;the datasheet states that the address of the 3 signature bits
;=(000H,100H, & 200H)
; i do not understand the above values can't be 8 bit? 

	anl	a,#01Fh		;isolate 5 bits
	lcall	shout		;
	
	mov	a,#055h		;dummy byte
	lcall	shout		;	

	lcall	shin		;read sig byte into acc
	lcall	PutByte		;print it
	inc	r1		;next byte
	djnz	r7,Read_Sig1	;
	ret
; *******************************************************************
Read_Lock:
	
	mov	a,#024h		;command byte
	lcall	shout		;

	mov	a,#055h		;dummy byte
	lcall	shout		;

	mov	a,#055h		;dummy byte
	lcall	shout		;

	lcall	shin		;read lock byte into acc
	lcall	putbyte
        ret
;*******************************************************************
;write all lock bits up to mode 4
Lock:
	mov	r1,#0E0h	;
	mov	r7,#003h	;

Lock1:
	mov	a,#0ACh		;
	lcall	shout		;

	mov	a,r1		;lock bit in R1
	lcall	shout		;

	mov	a,#055h		;dummy byte
	lcall	shout		;

	mov	a,#055h		;dummy byte
	lcall	shout		;

	inc	r1
	djnz	r7,Lock1

	ret

;***************************************************************
shout:
; Shift out a byte, most significant bit first.
; SCK expected low on entry. Return with SCK low.
; Called with data to send in A.
	mov r7, #8 	; bit counter
shout1:
	rlc a 		; move bit into CY
	mov MOSI, c 	; output bit
	nop 		; enforce data setup
	nop 		;
	setb SCK 	; raise clock
	nop 		; enforce SCK high
	nop 		;
	nop 		;
	nop 		;
	clr SCK 	; drop clock
	djnz r7, shout1 ; next bit
	ret
;***************************************************************
; Shift in a byte, most significant bit first.
; SCK expected low on entry. Return with SCK low.
; Returns received data byte in A.
shin:
	mov r7, #8 		; bit counter
shin1:
	setb SCK 		; raise clock
	mov c, MISO 	        ; input bit
	rlc a 		        ; move bit into byte
	nop 			; enforce SCK high
	nop 			;
	clr SCK 		; drop clock
	nop 			; enforce SCK low
	nop 			;
	djnz r7, shin1 	        ; next bit
	ret

Erase:
	acall	En_Pg_Mode	;
	mov a, #0ACh 		; send first byte of erase 
	call shout 		;
	mov a, #080h		; send second byte
	call shout 		;
	mov a, #055h		; send third byte (dummy byte)
	call shout 		;	
	call delay10ms 		; wait 10 milliseconds
        ret




