
&MAX7301INPUT:
&Max7301OUTPUT:

	PUSH 	IE			;Save current interrupt state.
        CLR	EA
	MOV	A,max7301output|regname
	
	JB	acc.7,Readdata  ; D7 :If HIGH its a READ if LOW its a write.

 	CALL 	writedata        
        SETB	7301_cs

	POP	IE		;Don't forget interrupt on stack
	RET
	REGEND

READDATA:
	CALL 	writedata		;Write out address of register to be read, with D15 set, write 8 other bits of crap.
	SETB	7301_CS	;Then writeout dummys and read BACK from Dout
        ;Get data from chip, do 8 dummy reads and clock out the state of Dout to a register.
	CLR	7301_CS

	MOV	r0,#8
	CLR	A
	CLR 	7301_Dat		;Effectively write NOP TO selected register while still being READ
	CLR	C
loop2:	SETB	7301_Sclk
	CLR 	7301_Sclk	
	DJNZ	r0,loop2 ;This is the dummy write of first 8 bits
	
	MOV	r0,#8                   
loop3:	SETB	7301_Sclk
	MOV	C,7301_Dout
	
        CLR	7301_Sclk	
	RLC	A
	DJNZ	r0,loop3
        MOV	r0,max7301Input|regdatainput ; Get address of    register to return value of
	MOV	@r0,A           ;Result in Acc., send to indirect  
	SETB	7301_cs
	POP 	IE		;Don't forget that interrupt
	RET
	REGEND

;write out data
writedata:
	CLR 	7301_sclk
        CLR 	7301_CS
        CALL    clockdata ;Acc contains address of internal                    register to set
        MOV	A,max7301output|regdataoutput  ; Now acc contains data to set.
        CALL 	clockdata
        SETB	7301_CS
        RET

clockdata:
	MOV 	r0,#8
loop1:  RLC 	A
	MOV 	7301_dat,C
        SETB	7301_sclk
        clr     7301_sclk
        DJNZ 	r0,loop1
        RET  

