Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Dallas 1-Wire Issue (DS18B20)

Status
Not open for further replies.
Can someone see what I'm doing wrong here.
It is in relation to checking if the sensor is busy doing a temp conversion...
Resolution is 12 bit so it should take 750mS
How do I properly check the sensors busy status?

I structured it like this so I can put this in the main loop and execute other tasks while the conversion is taking place. Didn't want to use interrupt for this. Open to suggestions on a better way to do this also....
Code:
MAIN_LOOP  

GET_TEMP
  ; Check FLAG, 4 to determin if temp sensors finished Temperature Conversion and ready for next conversion
        BTFSC   FLAGS, 4 
        GOTO    CONVERSION_NOT_READY
  ; Broadcast temperature conversion command to 1-Wire bus
        CALL    OW_RESET                ; Reset
        MOVLW   H'CC'                   ; Skiprom (send to all devices)
        CALL    OW_WRITE_BYTE           ; 
        MOVLW   H'44'                   ; Convert T
        CALL    OW_WRITE_BYTE           ; 
       
        BSF     FLAGS, 4                ; Set to indicate temp sensors busy
CONVERSION_NOT_READY


       

TEMPERATURE_CONVERSION_DONE?
        CALL    OW_READ_BIT             ; Initiate Read Time Slots
        BTFSS   STATUS, C               ; DS18B20 transmits a 0 while the temperature conversion is in progress and a 1 when the conversion is done
        GOTO    CONVERSION_NOT_DONE     ; Transmitting 0 therefore NOT done
       
        CALL    OW_RESET                ; Reset
        MOVLW   H'CC'                   ; Skiprom
        CALL    OW_WRITE_BYTE           ; 
        MOVLW   H'BE'                   ; Read Scratchpad
        CALL    OW_WRITE_BYTE           ; 
       
        CALL    OW_READ_BYTE            ; Move Scrachpad Byte 0 to W
        MOVWF   TEMP_C_L                ; Store in Celsius TEMPLO GPR
        CALL    OW_READ_BYTE            ; Move Scrachpad Byte 1 to W
        MOVWF   TEMP_C_H                ; Store in Celsius TEMPHI GPR
  ; Clear flag to indicate temp sensors ready for next conversion
        BCF     FLAGS, 4                ; Temp sensors ready for another Convert T Command
CONVERSION_NOT_DONE

  ; ... rest of main loop here...

The 1-Wire Subroutines. (Which im confident are fine because they work if I change the main code.
Code:
  ; Dallas 'One Wire' Communication Routines. 
  ; DISABLE INTERRUPTS DURING TIME CRITICAL COMMUNICATION
OW_RESET  ; This routine Resets devices on the 1-Wire bus
        CALL    DQ_LL                   ; Force the DQ Line to Logic Low
        MOVLW   (D'500'-5)/5            ; Reset pulse must be held a minimum of 480uS. 
        CALL    DELAY                   ; For delays from 10uS to 1285uS. Example, MOVLW D'10' for 10uS, MOVLW D'500' for 500uS. (D'n'-5)/5; n must be divisable by 5
        CALL    DQ_HIZ                  ; Release DQ Line
        MOVLW   (D'70'-5)/5             ; Wait for recovery
        CALL    DELAY                   ; 
        BTFSC   PORTB, 0                ; Test for 'Presence Pulse'
        GOTO    OW_RESET                ; If not present, Reset
        MOVLW   (D'430'-5)/5            ; Must wait a minmum of 480uS from when DQ line is released before moving on
        CALL    DELAY                   ; 
        RETURN                          ;
       
OW_READ_BYTE  ; This routine reads a byte of data from the DS18B20
        MOVLW   H'08'                   ; Amount of bits to shift out
        MOVWF   COUNT                   ; Store in COUNT GPR
        CALL    OW_READ_BIT             ; Read bit
        RRF     SHIFT, F                ; Rotate bit out of carry into SHIFT GPR
        DECFSZ  COUNT, F                ; Decrement COUNT
        GOTO    $-D'3'                  ; If not zero, read next bit
        MOVF    SHIFT, W                ; If zero, move SHIFT GPR to W
        RETURN                          ;
       
OW_READ_BIT  ; This routine reads one bit of data from the DS18B20
        BCF     INTCON, GIE             ; Disable Global Interrupts
        CALL    DQ_LL                   ; Force the DQ Line to Logic Low
        CALL    DQ_HIZ                  ; Release DQ Line
        BSF     STATUS, C               ; Preset Carry Flag
        BTFSS   PORTB, 0                ; Test DQ Line
        BCF     STATUS, C               ; If zero, Clear Carry Flag
        BSF     INTCON, GIE             ; Enable Global Interrupts
        MOVLW   (D'60'-5)/5             ; Wait for Time Slot to end
        CALL    DELAY                   ; 
        RETURN                          ;
       
OW_WRITE_BYTE  ; This routine writes a byte of data to the DS18B20
        MOVWF   SHIFT                   ; Move data to shift into DS18B20 to SHIFT GPR
        MOVLW   D'08'                   ; Amount of bits to shift in
        MOVWF   COUNT                   ; Store in COUNT GPR
        RRF     SHIFT, F                ; Rotate valid data into Carry Flag
        CALL    OW_WRITE_BIT            ; Write bit
        DECFSZ  COUNT, F                ; Decrement COUNT
        GOTO    $-D'3'                  ; If not zero, read next bit
        RETURN                          ; If zero, RETURN
       
OW_WRITE_BIT  ; This routine writes one bit of data to the DS18B20
        BCF     INTCON, GIE             ; Disable Global Interrupts
        CALL    DQ_LL                   ; Force the DQ Line to Logic Low
        BTFSS   STATUS, C               ; Test Carry Flag
        GOTO    $+D'2'                  ; If zero, leave DQ Line Logic Low
        CALL    DQ_HIZ                  ; If not zero, release DQ Line to Logic High
        MOVLW   (D'60'-5)/5             ; Hold Logic Low, write 0
        CALL    DELAY                   ; 
        CALL    DQ_HIZ                  ; If not zero, release DQ Line to Logic High, write 1
        BSF     INTCON, GIE             ; Enable Global Interrupts
        RETURN                          ; 
       
DQ_HIZ  ; This routine forces the DQ Line to an Input / High Impedance state
        BSF     STATUS, RP0             ; Bank 1
        BSF     TRISB, 0                ; Make Pin 3 an input, Pullup resistor forces line to logic 1, unless DS18B20 pulls it low
        BCF     STATUS, RP0             ; Bank 0
        RETURN
       
DQ_LL  ; This routine forces the DQ Line to Logic Low
        BCF     PORTB, 0                ; Clear output latch
        BSF     STATUS, RP0             ; Bank 1
        BCF     TRISB, 0                ; Make Pin 3 an output
        BCF     STATUS, RP0             ; Bank 0
        RETURN
       
DELAY  ; This routine can provide delays from 10uS to 1285uS depending on the number moved to the Working register prior to calling the delay
        MOVWF   DELAYGPR1               ; Move integer to GPR
        NOP                             ; No Operation
        NOP                             ; No Operation
        DECFSZ  DELAYGPR1, F            ; Decrement GPR and place back in itself
        GOTO    $-D'3'                  ; Not finished, GOTO here - 3 instructions
        RETURN                          ; Finished, Return
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top