;   -  .
;
LIST P=16F84a
;    
#include "P16f84a.INC"
 __CONFIG    03FF5H
option_reg         equ           01h
tmr0               equ           01h
pcl                equ           02h
status             equ           03h
porta              equ           05h
portb              equ           06h
trisa              equ           05h
trisb              equ           06h
intcon             equ           0bh
eedata             equ           08h
eeadr              equ           09h
eecon1             equ           08h
eecon2             equ           09h
;
time               equ           0dh
key_temp           equ           0fh
temp_w             equ           10h
temp_stat          equ           11h
flags              equ           12h
ns_temp            equ           14h
ind1               equ           1dh
erom               equ           1eh
temp1              equ           1fh
ind2               equ           20h
k_t2               equ           21h
out1               equ           22h
out2               equ           23h
out3               equ           24h
k_t1               equ           25h
I_BYTE             equ           26h
O_BYTE             equ           27h
TEMP_READL         equ           28h
TEMP_READH         equ           29h
TEMP               equ           2ah
_N                 equ           2bh
LOOP1              equ           2ch
LOOP2              equ           2dh
key_f              equ           2eh
;
;
                   org           2100h
                   de            60h
;
                   org           00h
                   goto          start
                   org           04h
                   bcf           intcon,7
                   movwf         temp_w
                   movf          status,0
                   movwf         temp_stat
                   bcf           status,5
                   movlw          70h
                   movwf         tmr0
                   clrf          portb
                   bsf           portb,7
                   decfsz        time,1
                   goto          int_m1
                   movlw         0d8h
                   movwf         time
                   btfsc         flags,0
                   goto          int_m2
                   bsf           flags,0
                   bcf           flags,1
                   goto          int_m1
int_m2             bcf           flags,0
                   bsf           flags,1
int_m1             btfss         flags,6
                   goto          i_ind
int_exit           bcf           intcon,2
                   movf          temp_stat,0
                   movwf         status
                   movf          temp_w,0
                   bsf           intcon,7
                   retfie
i_ind              movf          time,0
                   andlw         b'00000011'
                   addwf         pcl,1
                   goto          ind_m1
                   goto          ind_m2
                   goto          ind_m3
                   goto          int_exit
ind_m3             movf          out3,1
                   btfsc         status,2
                   goto          i_m3_exit
                   movf          out3,0
                   andlw         b'00001111'
                   movwf         portb
                   bsf           portb,7
                   bsf           portb,6
                   goto          int_exit
i_m3_exit          clrf          portb
                   goto          int_exit
ind_m2             movf          out2,0
                   andlw         b'00001111'
                   movwf         portb
                   bsf           portb,5
                   bcf           portb,7
                   goto          int_exit
ind_m1             movf          out1,0
                   andlw         b'00001111'
                   movwf         portb
                   bsf           portb,4
                   bsf           portb,7
                   goto          int_exit
_16                movf          TEMP_READL,0
                   andlw         b'00001111'
                   addwf         pcl,1
                   retlw         00h
                   retlw         01h
                   retlw         02h
                   retlw         02h
                   retlw         03h
                   retlw         03h
                   retlw         04h
                   retlw         05h
                   retlw         05h
                   retlw         06h
                   retlw         06h
                   retlw         07h
                   retlw         08h
                   retlw         08h
                   retlw         09h
                   retlw         09h
_10                movf          ns_temp,0
                   andlw         b'00001111'
                   addwf         pcl,1
                   retlw         00h
                   retlw         01h
                   retlw         03h
                   retlw         04h
                   retlw         06h
                   retlw         07h
                   retlw         09h
                   retlw         0bh
                   retlw         0ch
                   retlw         0fh
                   retlw         0fh
                   retlw         0fh
                   retlw         0fh
                   retlw         0fh
                   retlw         0fh
                   retlw         0fh
;
start              clrf          intcon
                   clrwdt
                   bsf           status,5
                   movlw         b'11010011'
                   movwf         option_reg
                   movlw         b'11111101'
                   movwf         trisa
                   movlw         b'00000000'
                   movwf         trisb
                   bcf           status,5
                   bcf           porta,1
                   clrf          flags
		   clrf 	 TEMP_READL
		   clrf 	 TEMP_READH
                   clrf          ind1
                   clrf          ind2
                   clrf          out1
                   clrf          out2
                   clrf          out3
                   clrf          portb
                   clrf          key_temp
                   clrf          key_f
                   bsf           portb,7
                   movlw         00h  
                   movwf         eeadr
                   bsf           status,5
                   bsf           eecon1,0
                   bcf           status,5
                   movf          eedata,0
                   andlw         b'01111111'
                   movwf         erom
                   movwf         ns_erom
                   btfsc         porta,2
                   goto          y0
                   bsf           key_f,2
y0                 movlw         b'10100000'
                   movwf         intcon
		   call	         START_DS
y8                 clrwdt
                   btfsc         flags,0
                   goto          y1
                   btfss         flags,2
                   goto          y2
                   bcf           flags,2
                   clrwdt
                   call          READ_T
                   clrwdt
                   call          START_DS
                   clrwdt
                   btfsc         TEMP_READH,7
                   goto          y3
                   movlw         02h
                   subwf         TEMP_READH,0
                   btfsc         status,2
                   goto          y4
                   btfss         status,0
                   goto          y3
y7                 bcf           porta,1
                   bcf           flags,7
                   goto          y5
y3                 bsf           porta,1
                   bsf           flags,7
                   goto          y5
y4                 btfsc         TEMP_READL,7
                   goto          y7
                   movf          TEMP_READL,0
                   subwf         erom,0
                   btfsc         status,2
                   goto          y7
                   btfsc         flags,7
                   goto          y9
                   btfss         status,0
                   goto          y7
                   sublw         03h
                   btfss         status,0
                   goto          y3
                   goto          y7
y9                 btfss         status,0
                   goto          y7
                   goto          y3
y1                 bsf           flags,2
                   clrwdt
                   goto          y2
y5                 btfsc         flags,4
                   goto          y8 
                   call          _16
                   movwf         out1
                   movf          TEMP_READL,0
                   andlw         b'11110000'
                   movwf         temp1
                   movf          TEMP_READH,0
                   andlw         b'00001111'
                   iorwf         temp1,1
                   swapf         temp1,1
                   call          b_d
                   movf          ind1,0
                   movwf         out3
                   movf          ind2,0
                   movwf         out2
                   goto          y8              
y2                 clrwdt
                   bcf           flags,4
                   btfsc         flags,3
                   goto          ns
                   btfss         porta,3
                   goto          k_sb
                   btfss         porta,4
                   goto          k_sb
                   btfsc         key_f,2
                   goto          sb9                 
                   btfss         porta,2
                   goto          k_sb3
k_x                bcf           flags,4
                   movlw         02h
                   movwf         key_temp
                   goto          y8
sb9                btfsc         porta,2
                   bcf           key_f,2
                   goto          y8
k_sb3              btfsc         porta,2
                   goto          k_x
                   call          k_p20
                   btfsc         porta,2
                   goto          k_x
                   bsf           flags,4
                   movf          erom,0
                   movwf         TEMP_READL
                   call          _16
                   movwf         out1
                   movf          erom,0
                   andlw         b'11110000'
                   movwf         temp1
                   movlw         02h
                   iorwf         temp1,1
                   swapf         temp1,1
                   call          b_d
                   clrwdt
                   movf          ind1,0
                   movwf         out3
                   movf          ind2,0
                   movwf         out2
                   goto          y8
k_p20              clrwdt
                   movlw         50h
                   movwf         k_t1
k_p202             movlw         0ffh
                   movwf         k_t2
k_p201             decfsz        k_t2,1
                   goto          k_p201
                   clrwdt
                   decfsz        k_t1,1
                   goto          k_p202
                   clrwdt
                   return
ns_8               bcf           flags,6
yy1                bsf           flags,5
                   goto          y8
;
k_sb               btfsc         key_f,3
                   goto          k_sb_1
                   bsf           key_f,3
                   movlw         02h
                   movwf         key_temp
                   goto          y8
k_sb_1             btfsc         flags,0
                   goto          yy1
                   btfss         flags,5
                   goto          y8
                   bcf           flags,5
                   btfss         porta,3
                   goto          k_sb_2
                   btfss         porta,4
                   goto          k_sb_2
                   bcf           key_f,3
                   goto          y8
k_sb_2             decfsz        key_temp,1
                   goto          y8
                   bsf           flags,3
                   goto          y8
ns                 clrwdt  
                   bsf           flags,4
                   btfss         key_f,3
                   goto          ns_1
                   movlw         0ffh
                   movwf         key_temp
                   btfss         porta,3
                   goto          ns_10
                   btfss         porta,4
                   goto          ns_10
                   call          k_p20
                   btfss         porta,3
                   goto          ns_10
                   btfss         porta,4
                   goto          ns_10
                   movlw         0ah
                   movwf         key_temp
                   bcf           key_f,3
                   goto          y8
ns_1               clrwdt
                   bsf           flags,4
                   btfss         porta,4
                   goto          ns_b
                   bcf           key_f,0
                   btfss         porta,3
                   goto          ns_h
                   bcf           key_f,1
                   btfss         porta,2
                   goto          ns_ok
ns_10              clrwdt 
                   btfsc         flags,1
                   goto          ns_8
                   btfss         flags,5
                   goto          y8
                   bcf           flags,5
                   call          ind
                   bsf           flags,6
                   bsf           key_f,5
                   decfsz        key_temp,1
                   goto          y8
                   goto          start
ind                bsf           flags,4
                   clrwdt
                   movf          ns_erom,0
                   movwf         TEMP_READL
                   call          _16
                   movwf         out1
                   movf          ns_erom,0
                   andlw         b'11110000'
                   movwf         temp1
                   movlw         02h
                   iorwf         temp1,1
                   swapf         temp1,1
                   call          b_d
                   movf          ind1,0
                   movwf         out3
                   movf          ind2,0
                   movwf         out2
                   bcf           flags,6
                   return
ns_b               call          k_p20
                   btfsc         porta,4
                   goto          y8
                   call          ind
                   movlw         0ah
                   movwf         key_temp
                   bcf           flags,6
ns_b_3             btfsc         key_f,0
                   goto          ns_b_7
                   bsf           key_f,0
                   movf          ns_erom,0
                   movwf         TEMP_READL
                   call          _16
                   movwf         ns_temp
                   incf          ns_temp,1
                   movlw         0ah
                   subwf         ns_temp,0
                   btfsc         status,2
                   goto          ns_b_4
                   call          _10
                   movwf         ns_temp
                   movlw         b'00001111'
                   andwf         ns_temp,1
                   movlw         b'11110000'
                   andwf         ns_erom,1
                   movf          ns_temp,0
                   iorwf         ns_erom,1
                   goto          ns_b_7
ns_b_4             movlw         0fh
                   clrwdt
                   addwf         ns_erom,1
                   movlw         b'11110000'
                   andwf         ns_erom,1
                   btfss         ns_erom,7
                   goto          ns_b_7
                   movlw         b'01111111'
                   movwf         ns_erom
ns_b_7             call          ind
                   clrwdt
                   bcf           flags,5
                   bcf           flags,1
                   goto          y8
ns_h               call          k_p20
                   btfsc         porta,3
                   goto          y8
                   movlw         0ah
                   movwf         key_temp
                   bcf           flags,6
                   bcf           flags,5
                   bcf           flags,1
                   btfsc         key_f,1
                   goto          ns_b_7
                   bsf           key_f,1
                   movf          ns_erom,0
                   movwf         TEMP_READL
                   call          _16
                   movwf         ns_temp
                   decf          ns_temp,1
                   movlw         0ffh
                   subwf         ns_temp,0
                   btfsc         status,2
                   goto          ns_h1
                   call          _10
                   clrwdt
                   movwf         ns_temp
                   movlw         b'00001111'
                   andwf         ns_temp,1
                   movlw         b'11110000'
                   andwf         ns_erom,1
                   movf          ns_temp,0
                   iorwf         ns_erom,1
                   goto          ns_b_7
ns_h1              movlw         0fh
                   subwf         ns_erom,1
                   movlw         b'00001111'
                   clrwdt
                   iorwf         ns_erom,1
                   btfss         ns_erom,7
                   goto          ns_b_7
                   clrf          ns_erom
                   goto          ns_b_7
ns_ok              call          k_p20
                   clrwdt
                   btfsc         porta,2
                   goto          y8
                   bcf           intcon,7
                   movlw         00h
                   movwf         eeadr
                   movf          ns_erom,0
                   movwf         eedata
                   bsf           status,5
                   bsf           eecon1,2
                   movlw         055h
                   movwf         eecon2
                   movlw         0AAh
                   movwf         eecon2
                   bsf           eecon1,1
                   bcf           eecon1,4
                   bcf           status,5
                   call          k_p20
                   goto          start
; -----------      -----------

b_d  	           clrf 	 ind1
                   clrwdt
	           movf 	 temp1,0
	           movwf	 ind2
b_2	           movlw	 D'10'
	           subwf	 ind2,0
	           btfss	 status,0
	           goto	         b_1
	           movwf	 ind2
	           incf	         ind1,1
	           goto	         b_2
b_1	           movlw	 d'10'
	           subwf	 ind1,0
	           btfss         status,0
	           goto  	 b_3
	           movwf	 ind1
b_3	           return
;
;
;       DS1820
READ_T		CALL	INIT
                   clrwdt
		MOVLW	0xCC						;Skip ROM 
		MOVWF	O_BYTE
		CALL	OUT_BYTE
		MOVLW	0xBE						;Read Scratchpad 
		MOVWF	O_BYTE
		CALL	OUT_BYTE
		; 
		CALL 	IN_BYTE
		MOVWF	TEMP_READL					;0
		CALL 	IN_BYTE
		MOVWF	TEMP_READH					;1
                   clrwdt
		CALL 	IN_BYTE						;2
                   clrwdt
		CALL 	IN_BYTE						;3
		CALL 	IN_BYTE		;4
                   clrwdt
		CALL 	IN_BYTE						;5
                   clrwdt
		CALL 	IN_BYTE						;6
                   clrwdt
		CALL 	IN_BYTE						;7
                   clrwdt
		CALL 	IN_BYTE						;8
		;     
		RETLW	0xFF
;
;         DS1820
START_DS
		CALL	INIT
                   clrwdt
		MOVLW	0xCC						;Skip ROM 
		MOVWF	O_BYTE
		CALL	OUT_BYTE
                   clrwdt
		MOVLW	0x44						;Convert T
		MOVWF	O_BYTE
		CALL	OUT_BYTE
                   clrwdt
		RETLW	0xFF
		
; The following are common 1-Wire routines used in all applications
INIT:           CALL	PIN_HI
                CALL	PIN_LO
		MOVLW	.50              	; 500 us delay
		CALL 	DELAY_10USEC
		CALL	PIN_HI		  
                MOVLW 	.50					; 500 usec delay
                CALL 	DELAY_10USEC
                RETURN
;
IN_BYTE:              				; returns byte in W
		MOVLW	.8
		MOVWF	 _N
      	        CLRF	I_BYTE
IN_BYTE_1:
		CALL PIN_LO					; momentary low on DATA_PIN
                NOP
		CALL PIN_HI
		NOP
		NOP
		NOP
		NOP
		NOP
                NOP
                MOVF	PORTA, W			; 7 usecs later, fetch from DATA_PIN
		MOVWF	TEMP                                                        
		BTFSS	TEMP, 0
		BCF     STATUS, C				; its a zero
		BTFSC	TEMP, 0
		BSF     STATUS, C				; its a one
                RRF	I_BYTE, F
		MOVLW 	.6					; now delay 60 usecs
                CALL	DELAY_10USEC
                DECFSZ	_N, F
                GOTO	IN_BYTE_1
                MOVF	I_BYTE, 0			; return the result in W
                RETURN

OUT_BYTE:
		MOVLW	.8
                MOVWF	_N
OUT_BYTE_1:
		RRF	O_BYTE, F
                BTFSS	STATUS, C
		GOTO	OUT_0
		GOTO	OUT_1	
OUT_BYTE_2:
		DECFSZ	_N, F
                GOTO	OUT_BYTE_1
                RETURN
OUT_0:		CALL	PIN_LO				; bring DATA_PIN low
		MOVLW	.6					; for 60 usecs
		CALL 	DELAY_10USEC
                CALL	PIN_HI
                GOTO	OUT_BYTE_2
OUT_1:   	CALL	PIN_LO				; momentary low
                CALL	PIN_HI
                MOVLW 	.6
		CALL 	DELAY_10USEC
                GOTO	OUT_BYTE_2
;
PIN_HI:		BCF		INTCON,	GIE
		BSF		STATUS, RP0
       	 	BSF		TRISA, 0	; high impedance
       		BCF		STATUS, RP0
        	BSF		INTCON,	GIE
        	RETURN
PIN_LO:		BCF		INTCON,	GIE
		BCF		PORTA, 0
		BSF		STATUS, RP0
        	BCF		TRISA, 0	; low impedance zero
        	BCF		STATUS, RP0
        	BSF		INTCON,	GIE
        	RETURN
;
DELAY_LONG
		MOVLW 	.250				; 250 msec delay
		MOVWF 	LOOP1
DELAY_N_MS:
OUTTER		MOVLW	.110		; close to 1.0 msec delay when set to .110
		MOVWF 	LOOP2
INNER		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		DECFSZ	LOOP2, F			; decrement and leave result in LOOP2 
		GOTO 	INNER				; skip next statement if zero
		DECFSZ 	LOOP1, F
		GOTO 	OUTTER
		RETURN
;
DELAY_10USEC:						; provides a delay equal to W * 10 usecs
		MOVWF 	LOOP1
DELAY_100USEC_1:
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		DECFSZ 	LOOP1, F
		GOTO 	DELAY_100USEC_1
		RETURN
                END