;ds_th_20
#define DQ porta,0 ; a,0 pin 
;**********************************************************************
;    	Read Data From DS1820		                              *
;**********************************************************************
RD_Temp	call	DS_Rx		; Check DS1820 status
	addlw	0x01		; 255=ready, 0= ready 
	btfss	STATUS,Z	; Zero flag set = ready
	return			; W register is not zero = not ready
Get_Temp   	call	DS_Reset	; Reset chip first
	btfss	STATUS,Z	; Zero flag set = OK
	goto	Error_		; If not response, exit
	movlw	0xcc		; Skip ROM command
	call	DS_Tx		; Send command
	movlw	0xbe		; Read scratch pad command
	call	DS_Tx		; Send command
	call	DS_Rx		; Read 8-bit data
	movwf	DS_DAT		; Save data to register
	call	DS_Rx		; Sign (FF=-VE, 00=+VE)
	movwf	DS_SIGN		; Save 9th bit to register 
	call	DS_Reset	; Restart
	movlw	0xcc		; Skip ROM command
	call	DS_Tx		; Send command
	movlw	0x44		; Start convert command
	call	DS_Tx		; Send command

	movf	DS_SIGN,w	; Check dign 
	btfsc	STATUS,Z	; Check dign = 00 ?
	goto	Pass		; If yes, Temp as positive then return
	addlw	0x01		; 
	btfss	STATUS,Z	; Check sign = FF ?
	goto	Error_		; If not, end with error
Pass	retlw	0x00		; If Yes, (Temp as negstive then return	
Error_	retlw	0x01
	
;**********************************************************************
;    	MACRO For DS1820		                              *
;**********************************************************************
DQLOW	macro
	bcf	DQ		; DQ bit ready
	bsf	STATUS,RP0
	bcf	DQ		; Set DQ to output
	bcf	STATUS,RP0
	endm

DQHIZ	macro
	bsf	STATUS,RP0
	bsf	DQ; Set DQ to input
	bcf	STATUS,RP0
	endm

PAUSE	macro	DELAY		; Generate delay time
	movlw	DELAY
	movwf	DUMMY0
	call	DelayAL
	endm

;**********************************************************************
;    	DS1820 SubRoutine				   *
;**********************************************************************
DelayAL	nop			; Use for pausing macro
	nop
	decfsz	DUMMY0,f
	goto	DelayAL
	return

;**********************************************************************
;    	Reset DS1820						      *
;**********************************************************************
DS_Reset 	DQLOW
	PAUSE	0x50		; 600 microsecond delay (jav:0x77)
	DQHIZ
	PAUSE	0x0c		; Wait 67 microsecond for response bit (jav:0x0c)
	nop
	nop
	movf	PORTA,w
	andlw	0x01		; Use RA0 only
	movwf	DUMMY1
	PAUSE	0x3b		; 300 microsecond delay
	movf	DUMMY1,w	; Response in W register
	return


;**********************************************************************
;    	Send Data To DS1820 (8 Bit)                                   *
;**********************************************************************
DS_Tx	movwf	DUMMY2		; Transmission data 
	movlw	0x08		; Prepare 8-bit counter for sending data
	movwf	DUMMY1		; Define loop counter
Tx_Loop	DQLOW			; Macro of DQ pin to low, This is start bit
	PAUSE	0x01		; 10 microsecond delay
	rrf	DUMMY2,f	; Rotate data to Carry flag
	btfsc	STATUS,C	; Test Carry flag
	bsf	DQ		; If Carry flag = "1" , set DQ to high
	PAUSE	0x0d		; 70 microsecond delay
	DQHIZ
	nop
	decfsz	DUMMY1,f	; 8 times ?
	goto	Tx_Loop		; No, send again
	return	

;**********************************************************************
;    	Recieve Data From DS1820 (8 Bit)		              *
;**********************************************************************
DS_Rx	movlw	0x08		; Recieve 8-bit data
	movwf	DUMMY1
Rx_Loop	DQLOW			; Macro of DQ pin to low, this is start bit
	PAUSE	0x01		; 10 microsecond delay
	DQHIZ			; Back to high for receiving
	movf	PORTA,w		; Read data
	andlw	0x01		; Get data bit 0 only
	addlw	0xff		; Move data bit 0 to Carry flag with addition method
	rrf	DUMMY2,f	; Move data bit 0 to DUMMY bit 7
	PAUSE	0x0b		; 60 microsecond delay
	decfsz	DUMMY1,f	; Loop 8 times
	goto	Rx_Loop		; Read again
	movf	DUMMY2,w	; Save data to W register
	return