LIST       p=16F628A
    __CONFIG	b'11111100011000'
   
CMCON	    equ		1Fh
PCL         equ         02h
TRISA	    equ         85h
TRISB	    equ         86h
PORTA	    equ         05h
PORTB	    equ         06h
STATUS      equ         03h
Reg_1       equ         20h
Reg_2       equ         21h
Reg_3       equ         22h
One_Wire_Byte equ       23h
Counter     equ         24h
DQ          equ         1
PORTDQ      equ         PORTA
TRISDQ      equ         TRISA
TL          equ         25h
TH          equ         26h
Temp	    equ         27h
            org 0
	    movlw	.7
	    movwf	CMCON
            bsf         STATUS,5
            bcf         STATUS,6
            movlw	.27
	    movwf	TRISA
            clrf        TRISB
            bcf         STATUS,5
begin       call        One_Wire_Init
            movlw       H'CC'
            call        One_Wire_Write_Byte
            movlw       H'44'
            call        One_Wire_Write_Byte
            call        delay_800ms
            call        One_Wire_Init
            movlw       H'CC'
            call        One_Wire_Write_Byte
            movlw       H'BE'
            call        One_Wire_Write_Byte
            call        One_Wire_Read_Byte
            movwf       TL
            call        One_Wire_Read_Byte
            movwf       TH
            call        One_Wire_Convert_Temp
	    MOVF	Temp,0
	    MOVWF	PORTB
	    BTFSC       Temp,7 ;proveryaem znak minus
            goto        VKL
	    movlw	.5
	    bcf         STATUS,0
            SUBWF       Temp,0 ;vichitanie is Temp-4=?
	    BTFSS       STATUS,0 ;proveriaem zaim edinic
            goto        VKL ;bil zaim to vkl
            goto        VIKL ;bil zaim to vkl
	    call	delay_800ms
            goto        begin

One_Wire_Init
            bsf         STATUS,5
            bcf         TRISDQ,DQ; TRISA,0
            bcf         STATUS,5
            bcf         PORTDQ,DQ; PORTA,0
            call        delay_500us
            bsf         STATUS,5
            bsf         TRISDQ,DQ; TRISA,1
            bcf         STATUS,5
            call        delay_500us
            return
VIKL
            bcf         PORTA,2
            call        delay_800ms
            goto	begin
VKL
            bsf         PORTA,2 ;vkl
            call        delay_800ms
            goto	begin
One_Wire_Write_Byte
            movwf       One_Wire_Byte
            movlw       .8
            movwf       Counter
One_Wire_Write_Bit
	    bcf         PORTDQ,DQ; PORTA,0
            bsf         STATUS,5
	    bcf         TRISDQ,DQ; TRISA,0
            bcf         STATUS,5
	    nop
            nop
            rrf         One_Wire_Byte
            bsf         STATUS,5
	    btfsc       STATUS,0
            bsf         TRISDQ,DQ
            bcf         STATUS,5
	    call        delay_60us
            bsf         STATUS,5
	    bsf         TRISDQ,DQ; TRISA,0
            bcf         STATUS,5
	    decfsz      Counter,1
            goto        One_Wire_Write_Bit
            return

One_Wire_Read_Byte
            movlw       .8
            movwf       Counter
One_Wire_Read_Bit
	    bcf         PORTDQ,DQ; PORTA,0
            bsf         STATUS,5
	    bcf         TRISDQ,DQ; TRISA,0
            bcf         STATUS,5
	    nop
	    nop
	    nop
            nop
            nop
            nop
            bsf         STATUS,5
	    bsf         TRISDQ,DQ; TRISA,1
            bcf         STATUS,5
	    nop
	    nop
            nop
            nop
            bcf         STATUS,0
	    btfsc       PORTDQ,DQ
            bsf         STATUS,0
	    rrf         One_Wire_Byte
            call        delay_60us
            decfsz      Counter,1
            goto        One_Wire_Read_Bit
            movf        One_Wire_Byte,0
            return
One_Wire_Convert_Temp
	    movlw       b'11110000'
            andwf       TL,0;xxxxxxxx;w=xxxx0000
            movwf       Temp;xxxx0000
            movlw       b'00001111'
            andwf       TH,0;yyyyyyyy;w=0000yyyy
            iorwf       Temp,1;xxxxyyyy
            swapf       Temp,1;yyyyxxxx
            return
delay_60us  movlw       .19
            movwf       Reg_1
            decfsz      Reg_1,F
            goto        $-1
            nop
            nop
            return
delay_500us
            movlw       .166
            movwf       Reg_1
            decfsz      Reg_1,F
            goto        $-1
            nop
            return
delay_800ms movlw       .241
            movwf       Reg_1
            movlw       .15
            movwf       Reg_2
            movlw       .5
            movwf       Reg_3
            decfsz      Reg_1,F
            goto        $-1
            decfsz      Reg_2,F
            goto        $-3
            decfsz      Reg_3,F
            goto        $-5
            return
            end