GET_TEMP
CALL DS18B20_RESET ; Issue device reset command
MOVLW H'CC' ; ROM Command - Skip ROM
CALL DS18B20_WRITE_BYTE ; Write to DS18B20
MOVLW H'44' ; Function Command - Convert T
CALL DS18B20_WRITE_BYTE ; Write to DS18B20
CALL DS18B20_READ_BIT ; Master can issue read time slots after the Convert T command and the DS18B20 will respond by transmitting a 0 while the temperature conversion is in progress and a 1 when the conversion is done
BTFSS STATUS, C ; Is Carry flag Set, to indicate Temperature Conversion finished?
GOTO $-2 ; NO, GOTO previous instruction and continue testing
CALL DS18B20_RESET ; Issue device reset command
MOVLW H'CC' ; ROM Command - Skip ROM
CALL DS18B20_WRITE_BYTE ; Write to DS18B20
MOVLW H'BE' ; Function Command - Read Scratchpad
CALL DS18B20_WRITE_BYTE ; Write to DS18B20
CALL DS18B20_READ_BYTE ; Read from DS18B20
MOVWF SCRATCHPAD_BYTE0 ; Move read byte to corresponding GPR
CALL DS18B20_READ_BYTE ; Read from DS18B20
MOVWF SCRATCHPAD_BYTE1 ; Move read byte to corresponding GPR
CALL REARRANGE_RESULT ; This sub routine is working.... tested with dummy figures, therefore problem lies with communication with the sensor
GOTO GET_TEMP
DS18B20_RESET ; RESET
BSF STATUS, RP0 ; }
BCF TRISA, 4 ; } DQ LOW
BCF STATUS, RP0 ; }
MOVLW D'123' ; Move 123 to W register
CALL DELAY ; CALL Delay: 500 cycle delay including CALL/GOTO & RETURN. Send Reset Pulse
MOVLW D'123' ; Move Decimal 123 to W register
GOTO DELAY ; GOTO Delay: 500 cycle delay including CALL/GOTO & RETURN. Wait MIN 480uS for DS18B20 & return with DQ line High Z
DS18B20_WRITE_BYTE ; WRITE BYTE
MOVWF SHIFT_IN_OUT ; Store Byte to shift into DS18B20
MOVLW D'08' ; Move Decimal 8 to W
MOVWF COUNT ; Store in COUNT GPR. This is how many bit will be read
RRF SHIFT_IN_OUT, F ; Rotate Right File Register SHIFT_IN_OUT to move bit out of SHIFT_IN_OUT and into Carry
CALL DS18B20_WRITE_BIT ; Send bit
DECFSZ COUNT, F ; Have all 8 bits been written?
GOTO $-3 ; NO, GOTO here - 3 instructions to write next bit
RETURN ;
DS18B20_WRITE_BIT ; WRITE BIT
BCF INTCON,GIE ; Disable Interrupts
BSF STATUS, RP0 ; }
BCF TRISA, 4 ; } DQ LOW
BCF STATUS, RP0 ; }
BTFSS STATUS, C ; Test Carry bit of STATUS register to determine which Logic Level to send
GOTO $+4 ; Carry = 0, Skip next 3 instructions, maintain Low Logic Level for MNI 60uS
BSF STATUS, RP0 ; }
BSF TRISA, 4 ; } DQ HIGH Z (Input, Pullup resistor forces line to Logic 1, unless DS18B20 pulls it low)
BCF STATUS, RP0 ; }
MOVLW D'16' ; Move 16 to W register
GOTO DELAY ; GOTO Delay: 72 cycle delay including CALL/GOTO & RETURN
DS18B20_READ_BYTE ; READ BYTE
MOVLW D'08' ; Move Decimal 8 to W
MOVWF COUNT ; Store in COUNT GPR. This is how many bit will be read
CALL DS18B20_READ_BIT ; CALL DS18B20_READ_BIT and return with read bit in Carry bit of STATUS register
RRF SHIFT_IN_OUT, F ; Rotate Right File Register SHIFT_IN_OUT to move bit out of Carry and into SHIFT_IN_OUT GPR
DECFSZ COUNT, F ; Have all 8 bits been read?
GOTO $-3 ; NO, GOTO here - 3 instructions to read next bit
RETURN ;
DS18B20_READ_BIT ; READ BIT
BCF INTCON,GIE ; Disable Interrupts
BSF STATUS, RP0 ; }
BCF TRISA, 4 ; } DQ LOW
BCF STATUS, RP0 ; }
BSF STATUS, RP0 ; }
BSF TRISA, 4 ; } DQ HIGH Z (Input, Pullup resistor forces line to Logic 1, unless DS18B20 pulls it low)
BCF STATUS, RP0 ; }
BCF STATUS, C ; Clear Carry bit of STATUS register
BTFSC PORTA, 4 ; Output data from the DS18B20 is valid for 15µs after the falling edge that initiated the read time slot
BSF STATUS, C ; DQ line Logic 1, therefore set Carry bit of STATUS register
BSF INTCON,GIE ; DQ line Logic 0, leave Carry bit of STATUS register clear and Enable Interrupts
MOVLW D'16' ; Move 16 to W register
GOTO DELAY ; GOTO Delay: 72 cycle delay including CALL/GOTO & RETURN
DELAY ; This routine is used for delays from 12 cycles to 1028 cycles (Move D'1' for 12 cycle delay or a D'0' for 1028 cycle delay)
MOVWF DELAYGPR1 ; Formula for this delay shown below (includes 'MOVLW number' and 'CALL/GOTO DELAY' instructions)
DECFSZ DELAYGPR1, W ; Cycles = 8 + (Number x 4) OR Number = (C - 8) / 4
GOTO $-2 ; Example. If a delay of at least 480uS is required (4MHz OSC), Number = (480 - 8) / 4 = 118. Therefore MOVLW D'118' before CALLing the DELAY
BSF STATUS, RP0 ; Bank 1
BSF TRISA, 4 ; Make Pin 3 an Input, Pullup resistor forces line to logic 1, unless DS18B20 pulls it low
BCF STATUS, RP0 ; Bank 0
BSF INTCON, GIE ; Enable Interrupts
RETURN