#define OwPort PORTA
#define OwBit 4
#define OwData OwPort,OwBit
OwInit bcf OwData ;ensure zero for output
bsf STATUS,RP0 ;bank1
bsf OwData ;set to input
bcf STATUS,RP0 ;back to bank 0
return
OwReset bsf STATUS,RP0 ;bank 1
bcf OwData ;pull line low
movlw .750/4 ;delay 750uS
call Wait8W
bsf OwData ;release line
movlw .68/4 ;delay 68uS
call Wait8W
bcf STATUS,RP0 ;bank 0
bcf b_OwPresent
btfss OwData ;pulled low?
bsf b_OwPresent ;yes so chip present
movlw .480/4 ;delay 480uS
;fall through to wait routine
Wait8W
; will delay W*8 cycles including the call/return
addlw 0xff; add -1 to W 1
btfsc STATUS,Z; =zero? 1/2
goto DoneWait; yes, better get out of here 2
nop; delay a bit 1
goto $+1; delay a bit more 2
goto Wait8W; go around again 2
DoneWait return ; 2
OwWriteByte bsf STATUS,RP0 ;bank 1
movwf OwByte ;store byte to write
movlw .8 ;send 8 bits
movwf OwCount
OwWriteLoop btfss OwByte,0 ;is bit 0
call OwSend0 ;yes so send zero
btfsc OwByte,0 ;is bit 1
call OwSend1 ;yes, so send 1
rrf OwByte,f ;move next bit to bit 0
decfsz OwCount,F ;sent all bits
goto OwWriteLoop ;no, so loop
bcf STATUS,RP0 ;bank 0
return
OwReadByte bsf STATUS,RP0 ;bank 1
movlw .8 ;get 8 bits
movwf OwCount
OwReadByteLoop call OwReadBit ;returns bit in carry
rrf OwByte,F ;move carry into byte
decfsz OwCount,F ;finished?
goto OwReadByteLoop ;no, so carry on
movfw OwByte ;return byte in W
bcf STATUS,RP0 ;bank 0
return
OwBusy bsf STATUS,RP0 ;bank 1
call OwReadBit ;get bit
btfss STATUS,C ;is it a zero
goto OwBusy ;yes, so wait
bcf STATUS,RP0 ;bank 0
return
OwSendCarry btfss STATUS,C ;is carry clear
goto OwSend0 ;yes, so goto send zero
OwSend1 bcf OwData ;pull line low
movlw 4/4
call Wait8W ;wait 4uS
bsf OwData ;release for a 1 bit
movlw .60/4 ;wait 60uS
goto Wait8W ;exit via wait sub in bank 1
OwSend0 bcf OwData ;pull line low
movlw .60/4 ;wait 60uS
call Wait8W
bsf OwData ;release
movlw 4/4
goto Wait8W ;exit via wait sub in bank 1
OwReadBit bcf OwData ;pull line low
movlw .4/4 ;wait 4uS
call Wait8W
bsf OwData ;release line
movlw .12/4 ;Wait 12uS
call Wait8W
movlw 1<<OwBit ;get the bit mask
bcf STATUS,RP0 ;bank 0
andwf OwPort,W ;test the bit
bsf STATUS,RP0 ;bank 1
movwf OwTemp ;save W
movlw .60/4 ;Wait 60uS
call Wait8W
movfw OwTemp ;retrieve W
addlw .255 ;carry will equal the received bit
return