# DS1820 slave, 16F877 master, picbasic pro, any info?

Status
Not open for further replies.

#### jessey

##### New Member
Hello everyone, I have recently purchased a LAB-X1 (formerly called the PIC-X1) experimenters board. I have one DS1820 Digital Thermometer chip up and running on the LABX1 to read the DS1820 1-wire temperature sensor and display the temperature on the LCD. It works fine but now I would like to add some code to have it function as a thermostat to control a relay. I have read the data sheet for the DS1820 and can not make any sense of it at all. I'm a hobbyist and learning from example seems to be the best way for me. Is there any sample code posted on the net anywhere that shows how to do that? I'm using the picbasic pro compiler with an epic programmer and I'm using the 16F877 micro. Can anyone help me out here or point me in the right direction? Thanks...................jessey

#### Surge

##### New Member
Find rentron.com there I saw some codes regarding one wire temp sensor some times ago.
hope this helps

#### motion

##### New Member
Here is a code lisiting of a macro which I insert if I want to use a DS1821 thermostat. I had last used it on a PIC16F877 and it seems to work quite well. The variables TEMP_HIGH and TEMP_LOW are the high and low settings of the thermostat in degrees C. MODESEL is a multi-pupose file register I use to store flag bits, one of which is the PR_DET (presence detect flag).

The code initializes the DS1821 by lowering the VDD pin and apllying 16 pulses to the DQ pin. This transforms the DS1821 to one-wire protocol mode. The thermostat values are loaded and then the DS1821 is converted back to thermostat mode.

Code:
INIT_DS1821 MACRO
LOCAL    INIT_START,INIT_END,INIT_LOOP
CALL     ONE_WIRE_ENABLE
INIT_START:
CALL     ONE_WIRE_RESET
BTFSS    MODESEL,PR_DET
GOTO     INIT_END
;
MOVLW    H'01'
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
MOVLW    TEMP_HIGH
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
;
CALL     ONE_WIRE_RESET
BTFSS    MODESEL,PR_DET
GOTO     INIT_END
;
MOVLW    H'02'
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
MOVLW    TEMP_LOW
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
;
CALL     ONE_WIRE_RESET
BTFSS    MODESEL,PR_DET
GOTO     INIT_END
;
MOVLW    H'0C'
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
MOVLW    B'00000100'
MOVWF    SHFT_BYT
CALL     ONE_WIRE_TX_BYTE
;
CLRF     TEMP1
CLRF     TEMP2
INIT_LOOP:
CLRWDT
NOOP
DECFSZ   TEMP1,F
GOTO     INIT_LOOP
DECFSZ   TEMP2,F
GOTO     INIT_LOOP
;
CALL     ONE_WIRE_ENABLE
;
GOTO     INIT_END
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ONE_WIRE_ENABLE:
BANKSEL  TRISA
BCF      DQ_TRIS         ; 1 = i/p, 0 = o/p
BCF      VDD_TRIS        ; 1 = i/p, 0 = o/p
BANKSEL  PORTA
;
NOOP
BSF      DQ_PORT
NOOP
NOOP
BCF      VDD_PORT
;
CLRF     TEMP1
ENABLE_LOOP1:
CLRWDT
DECFSZ   TEMP1,F
GOTO     ENABLE_LOOP1
;
MOVLW    D'16'
MOVWF    TEMP1
;
ENABLE_LOOP2:
NOOP
NOOP
BCF      DQ_PORT
;
CLRWDT
NOOP
NOOP
BSF      DQ_PORT
;
DECFSZ   TEMP1,F
GOTO     ENABLE_LOOP2
;
NOOP
NOOP
NOOP
NOOP
BSF      VDD_PORT
;
ENABLE_LOOP3:
CLRWDT
DECFSZ   TEMP1,F
GOTO     ENABLE_LOOP3
;
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ONE_WIRE_RESET:
BSF      VDD_PORT
;
MOVLW    3
MOVWF    TEMP2
CLRF     TEMP1
;
BANKSEL  TRISA
BCF      DQ_TRIS         ; 1 = i/p, 0 = o/p
BANKSEL  PORTA
BCF      DQ_PORT
RESET_LOOP1:
CLRWDT
DECFSZ   TEMP1,F
GOTO     RESET_LOOP1
DECFSZ   TEMP2,F
GOTO     RESET_LOOP1
;
BANKSEL  TRISA
BSF      DQ_TRIS         ; 1 = i/p, 0 = o/p
BANKSEL  PORTA
;
BCF      MODESEL,PR_DET
RESET_LOOP2:
BTFSC    PR_PORT
GOTO     CHK_PRESENCE
;
CLRWDT
DECFSZ   TEMP1,F
GOTO     RESET_LOOP2
;
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CHK_PRESENCE:
MOVLW    8
MOVWF    TEMP2
CLRF     TEMP1
RESET_LOOP3:
BTFSS    PR_PORT
BSF      MODESEL,PR_DET
;
CLRWDT
DECFSZ   TEMP1,F
GOTO     RESET_LOOP3
DECFSZ   TEMP2,F
GOTO     RESET_LOOP3
;
RETURN
;-----------------------------------------------------------
SLOT_1:
MOVLW    D'8'
MOVWF    TEMP1
MOVLW    D'90'
MOVWF    TEMP2
GOTO     SLOT_TIME
;-----------------------------------------------------------
SLOT_0:
MOVLW    D'90'
MOVWF    TEMP1
MOVLW    D'8'
MOVWF    TEMP2
GOTO     SLOT_TIME
;-----------------------------------------------------------
SLOT_TIME:
BCF      DQ_PORT
SLOT_LOW:
NOOP
DECFSZ   TEMP1,F
GOTO     SLOT_LOW
;
BSF      DQ_PORT
SLOT_HIGH:
NOOP
DECFSZ   TEMP2,F
GOTO     SLOT_HIGH
;
RETURN
;-----------------------------------------------------------
ONE_WIRE_TX_BYTE:
BANKSEL  TRISA
BCF      DQ_TRIS         ; 1 = i/p, 0 = o/p
BANKSEL  PORTA
;
MOVLW    8
MOVWF    SHFT_CNT
ONE_WIRE_TX_BYTE2:
BTFSS    SHFT_BYT,0
CALL     SLOT_0
BTFSC    SHFT_BYT,0
CALL     SLOT_1
;
RRF      SHFT_BYT,F
DECFSZ   SHFT_CNT,F
GOTO     ONE_WIRE_TX_BYTE2
;
RETURN
;
INIT_END:
BSF      DQ_PORT
BSF      VDD_PORT
;
BANKSEL  TRISA
BSF      DQ_TRIS         ; 1 = i/p, 0 = o/p
BANKSEL  PORTA
;
ENDM

#### motion

##### New Member
I found this russian website that accomplishes what you need using PicBasic Pro.

http://www.elin.ru/1-Wire/08.htm

Attached is what I had obtained from the site:

#### Attachments

• 47.3 KB Views: 575

#### jessey

##### New Member
Thanks Motion, I looked at the file you found on the Russian site and it is exactly the same program that came in the example code that was provided with the LABX 1 experimenters board's software that I purchased. This code does nothing more than to display the temperature on the LCD. What I'm looking for is an example code that would use one of the pins of the 16F877 as an output that would turn on and off a heater. Some code that will show me how to change the TH and the TL settings, if that's what sets the temperature for the heater to turn on and off at. I don't understand the data sheet and need to see working code in order to begin to understand how to write the code and to change it for what I want. I'll keep looking and appreciate your help.

Also thanks for suggesting rentron.com Surge, I checked it out and the same thing there. They only show code to display the temperature as well.

Thanks..........jessey

#### motion

##### New Member
If you are able to display the temp, I think it would be easy to compare the displayed temperature to a setpoint and set/clear an output pin on the PIC, wouldn't it?

There wouldn't be any need to to switch the DS1820 to thermostat mode. Just use the temperature, compare it to values stored in the PIC and set/clear an output pin.

Status
Not open for further replies.