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.

please help me .....ds1820 subroutines

Status
Not open for further replies.

rohitlucky

New Member
sir,
i made a project of digital thermostat by using pic16f628 and ds1820,and it worked. now i want to use it with ds18b20,ds18b20 is 12 bit resulation and i want to convert it to 9 bit resulation.please help me...........................



rohit
 

Attachments

  • subroutins.asm
    3.8 KB · Views: 136
If I'm not mistaken, the DS1820 and DS18S20 9-bit format looks something like this where the 'S' bits are 'sign' bits, the 'T' bits are the integer temperature, and the 'F' bit is the fractional 1/2 degree bit;

Code:
SSSSSSSS TTTTTTTF
The DS18B20 12-bit data looks like this;

Code:
SSSSSTTT TTTTFFFF
And so you could use something like this to convert the 12-bit formatted temperature data to the 9-bit format.
Code:
        rlf     tempHi,W        ; preserve sign bit in Carry
        rrf     tempHi,F        ; SSSSSSTT -> T 
        rrf     tempLo,F        ; TTTTTFFF
        rlf     tempHi,W        ; preserve sign bit in Carry
        rrf     tempHi,F        ; SSSSSSST -> T
        rrf     tempLo,F        ; TTTTTTFF
        rlf     tempHi,W        ; preserve sign bit in Carry
        rrf     tempHi,F        ; SSSSSSSS -> T
        rrf     tempLo,F        ; TTTTTTTF
If you want to support both the 9-bit and 12-bit formats then you would have to issue a "read rom" comand and then test the family ID byte (0x10 or 0x28) to determine when to do that conversion.

Regards, Mike
 
thanks mike,are u able to convert my subroutins to ds18b20 i have done this but dosnt work.



;**********************************************************************
; Read Data From DS1820 *
;**********************************************************************
RD_Temp call DS_Rx ; Check DS1820 status
addlw 0x01 ; 255=ready, 0= ready
btfss STATUS,Z ; Zero flag set = ready
return ; W register is not zero = not ready
Get_Temp call DS_Reset ; Reset chip first
btfss STATUS,Z ; Zero flag set = OK
goto Error_ ; If not response, exit
movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command

movlw 0x4e ; scratch pad write command
call DS_Tx ; Send command

movlw 0x00 ; tl byte
call DS_Tx ; Send command

movlw 0x00 ; th byte
call DS_Tx ; Send command

movlw b'00011111' ; resulation configuration byte
call DS_Tx ; Send command
call DS_Reset ; Restart


movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command
movlw 0xbe ; Read scratch pad command
call DS_Tx ; Send command
call DS_Rx ; Read 8-bit data
movwf DS_DAT ; Save data to register
call DS_Rx ; Sign (FF=-VE, 00=+VE)
movwf DS_SIGN ; Save 9th bit to register
call DS_Reset ; Restart
movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command
movlw 0x44 ; Start convert command
call DS_Tx ; Send command

movf DS_SIGN,w ; Check dign
btfsc STATUS,Z ; Check dign = 00 ?
goto Pass ; If yes, Temp as positive then return
addlw 0x01 ;
btfss STATUS,Z ; Check sign = FF ?
goto Error_ ; If not, end with error
Pass retlw 0x00 ; If Yes, (Temp as negstive then return
Error_ retlw 0x01

;**********************************************************************
; MACRO For DS1820 *
;**********************************************************************
DQLOW macro
bcf DQ ; DQ bit ready
bsf STATUS,RP0
bcf DQ ; Set DQ to output
bcf STATUS,RP0
endm

DQHIZ macro
bsf STATUS,RP0
bsf DQ ; Set DQ to input
bcf STATUS,RP0
endm

PAUSE macro DELAY ; Generate delay time
movlw DELAY
movwf DUMMY0
call DelayAL
endm

;**********************************************************************
; DS1820 SubRoutine *
;**********************************************************************
DelayAL nop ; Use for pausing macro
nop
decfsz DUMMY0,f
goto DelayAL
return

;**********************************************************************
; Reset DS1820 *
;**********************************************************************
DS_Reset DQLOW
PAUSE 0x77 ; 600 microsecond delay
DQHIZ
PAUSE 0x0c ; Wait 67 microsecond for response bit
nop
nop
movf PORTB,w
andlw 0x02 ; Use RA0 only
movwf DUMMY1
PAUSE 0x3b ; 300 microsecond delay
movf DUMMY1,w ; Response in W register
return

;**********************************************************************
; Send Data To DS1820 (8 Bit) *
;**********************************************************************
DS_Tx movwf DUMMY2 ; Transmission data
movlw 0x08 ; Prepare 8-bit counter for sending data
movwf DUMMY1 ; Define loop counter
Tx_Loop DQLOW ; Macro of DQ pin to low, This is start bit
PAUSE 0x01 ; 10 microsecond delay
rrf DUMMY2,f ; Rotate data to Carry flag
btfsc STATUS,C ; Test Carry flag
bsf DQ ; If Carry flag = "1" , set DQ to high
PAUSE 0x0d ; 70 microsecond delay
DQHIZ
nop
decfsz DUMMY1,f ; 8 times ?
goto Tx_Loop ; No, send again
return

;**********************************************************************
; Recieve Data From DS1820 (8 Bit) *
;**********************************************************************
DS_Rx movlw 0x08 ; Recieve 8-bit data
movwf DUMMY1
Rx_Loop DQLOW ; Macro of DQ pin to low, this is start bit
PAUSE 0x01 ; 10 microsecond delay
DQHIZ ; Back to high for receiving
nop
nop
movf PORTB,w ; Read data
andlw 0x02 ; Get data bit 0 only
addlw 0xff ; Move data bit 0 to Carry flag with addition method
rrf DUMMY2,f ; Move data bit 0 to DUMMY bit 7
PAUSE 0x0b ; 60 microsecond delay
decfsz DUMMY1,f ; Loop 8 times
goto Rx_Loop ; Read again
movf DUMMY2,w ; Save data to W register
return
 
I think I may have misunderstood what you are trying to do. Are you trying to command the DS18B20 to output the temperature data in 9-bit format?
 
yes mike ,i want to use ds18b20 instead of ds1820,for this i have to configure ds18b20 to read 9 bit configuration. the above blue section is only for that.actualy i dont want to change my whole programe to read 12bit data .i only want to configure ds18b20 to 9 bit configuration
 
Byte 4 of the DS18B20 is the configuration byte. You could set it to 0b00011111 to enable 9 bit mode, see figure 8 and the following table on https://www.electro-tech-online.com/custompdfs/2010/05/DS18B20.pdf

Page 12 explains how you write to the "scratchpad", you need to write 3 bytes in total and the last one of these is the configuration.

Read that datasheet and then come back with any questions of specific things you don't understand.
 
i understand all thease thing but i am not able to implement it.i dont know where to insert write scratchpad command in my code.


;************************************************* *********************
; Read Data From DS1820 *
;************************************************* *********************
RD_Temp call DS_Rx ; Check DS1820 status
addlw 0x01 ; 255=ready, 0= ready
btfss STATUS,Z ; Zero flag set = ready
return ; W register is not zero = not ready
Get_Temp call DS_Reset ; Reset chip first
btfss STATUS,Z ; Zero flag set = OK
goto Error_ ; If not response, exit
movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command

movlw 0x4e ; scratch pad write command
call DS_Tx ; Send command

movlw 0x00 ; tl byte
call DS_Tx ; Send command

movlw 0x00 ; th byte
call DS_Tx ; Send command

movlw b'00011111' ; to make it 9 bit
call DS_Tx ; Send command
call DS_Reset ; Restart

movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command
movlw 0xbe ; Read scratch pad command
call DS_Tx ; Send command
call DS_Rx ; Read 8-bit data
movwf DS_DAT ; Save data to register
call DS_Rx ; Sign (FF=-VE, 00=+VE)
movwf DS_SIGN ; Save 9th bit to register
call DS_Reset ; Restart
movlw 0xcc ; Skip ROM command
call DS_Tx ; Send command
movlw 0x44 ; Start convert command
call DS_Tx ; Send command

movf DS_SIGN,w ; Check dign
btfsc STATUS,Z ; Check dign = 00 ?
goto Pass ; If yes, Temp as positive then return
addlw 0x01 ;
btfss STATUS,Z ; Check sign = FF ?
goto Error_ ; If not, end with error
Pass retlw 0x00 ; If Yes, (Temp as negstive then return
Error_ retlw 0x01

;************************************************* *********************
; MACRO For DS1820 *
;************************************************* *********************
DQLOW macro
bcf DQ ; DQ bit ready
bsf STATUS,RP0
bcf DQ ; Set DQ to output
bcf STATUS,RP0
endm

DQHIZ macro
bsf STATUS,RP0
bsf DQ ; Set DQ to input
bcf STATUS,RP0
endm

PAUSE macro DELAY ; Generate delay time
movlw DELAY
movwf DUMMY0
call DelayAL
endm

;************************************************* *********************
; DS1820 SubRoutine *
;************************************************* *********************
DelayAL nop ; Use for pausing macro
nop
decfsz DUMMY0,f
goto DelayAL
return

;************************************************* *********************
; Reset DS1820 *
;************************************************* *********************
DS_Reset DQLOW
PAUSE 0x77 ; 600 microsecond delay
DQHIZ
PAUSE 0x0c ; Wait 67 microsecond for response bit
nop
nop
movf PORTB,w
andlw 0x02 ; Use RA0 only
movwf DUMMY1
PAUSE 0x3b ; 300 microsecond delay
movf DUMMY1,w ; Response in W register
return

;************************************************* *********************
; Send Data To DS1820 (8 Bit) *
;************************************************* *********************
DS_Tx movwf DUMMY2 ; Transmission data
movlw 0x08 ; Prepare 8-bit counter for sending data
movwf DUMMY1 ; Define loop counter
Tx_Loop DQLOW ; Macro of DQ pin to low, This is start bit
PAUSE 0x01 ; 10 microsecond delay
rrf DUMMY2,f ; Rotate data to Carry flag
btfsc STATUS,C ; Test Carry flag
bsf DQ ; If Carry flag = "1" , set DQ to high
PAUSE 0x0d ; 70 microsecond delay
DQHIZ
nop
decfsz DUMMY1,f ; 8 times ?
goto Tx_Loop ; No, send again
return

;************************************************* *********************
; Recieve Data From DS1820 (8 Bit) *
;************************************************* *********************
DS_Rx movlw 0x08 ; Recieve 8-bit data
movwf DUMMY1
Rx_Loop DQLOW ; Macro of DQ pin to low, this is start bit
PAUSE 0x01 ; 10 microsecond delay
DQHIZ ; Back to high for receiving
nop
nop
movf PORTB,w ; Read data
andlw 0x02 ; Get data bit 0 only
addlw 0xff ; Move data bit 0 to Carry flag with addition method
rrf DUMMY2,f ; Move data bit 0 to DUMMY bit 7
PAUSE 0x0b ; 60 microsecond delay
decfsz DUMMY1,f ; Loop 8 times
goto Rx_Loop ; Read again
movf DUMMY2,w ; Save data to W register
return
 
Add a new routine that you call before doing anything else.
Code:
InitDS		call	DS_Reset	; Reset chip first
		movlw	0xcc		; Skip ROM command
		call	DS_Tx		; Send command
		movlw	0x4e		; scratch pad write command
		call	DS_Tx		; Send command
		movlw	0x00		; tl byte
		call	DS_Tx		; Send command
		movlw	0x00		; th byte
		call	DS_Tx		; Send command
		movlw	b'00011111'	; to make it 9 bit 
		call	DS_Tx		; Send command
		return

Edit, to post formatted code put
Code:
 before it and
after it.

Mike.
 
Last edited:
Which bit don't you understand? Do you know what code to use? Do you know when to send the commands? Or do you want us to do everything for you?

Please use
Code:
 tags when you post code.  And don't just keep posting the same thing over and over.
 
done this but it is still work for ds1820 not for ds18b20.when i connect ds18b20 it show 16'c but actual temp is 32'c
 
sir
thanks for your wonderful solution.now i am able to use both ds18s20 and ds18b20 by changing few lines in my program.
 
Status
Not open for further replies.

Latest threads

Back
Top