I have studied (but not fully understood) the 18S20 data sheet and concluded that all that needs to happen is...
1: Initiate the device (start the conversion)
2: Wait until the conversion is complete (pause for at least 750ms)
3: Read what is in the register.
I have it running okay. The LCD displays the raw temp i.e. the register value in decimal as well as hex and whether bit 0 is set or not.
It seems that the value in the 18S20 register is actually 0.5 degrees centigrade units
so I figured out why not just divide it by 2 to get the temperature.
Code:
@ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT,LVP_OFF,WDT_OFF,MCLR_OFF,PROTECT_OFF
@ DEVICE pic16F628a, CPD_OFF
trisa =%00100000
trisb =%00000000
cmcon =%00000111 'Comparators Off
Define LCD_BITS 4
Define LCD_DREG PORTB
Define LCD_DBIT 0
Define LCD_RWREG PORTA
Define LCD_RWBIT 4
Define LCD_RSREG PORTA
Define LCD_RSBIT 3
Define LCD_EREG PORTA
Define LCD_EBIT 2
Define LCD_COMMANDUS 5000
Define LCD_DATAUS 100
Define LCD_INITMS 2
pause 100
DQ VAR PORTB.5 ' One-wire data pin
temperature VAR WORD ' Temperature storage
decimal var byte
loop:
OWOut DQ, 1, [$CC, $44] ' Start temperature conversion
pause 750 'Needed to complete conversion
OWOut DQ, 1, [$CC, $BE] ' Read the temperature
OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
'just check if bit0 is set
decimal = temperature mod 2
if decimal = 1 then 'if set, it must be .5
decimal =5
else
decimal = 0
endif
' raw value is the register value
lcdout $FE,1, "Raw ", dec(temperature)," ", hex(temperature), " ", dec(decimal)
LCDOut $FE, $C0, "Temp ", dec(temperature /2), ".", dec(decimal), " C"
goto loop
My LCD displays
Raw 44 2C 0
Temp 22.0 C
When I touch the 18S20 for five seconds I get
Raw 55 37 5
Temp 27.5 C
I am not sure how accurate it is because I do not have a reference temperature.
I tried putting the 18S20 under my tongue but I got a shock.
It seems to work okay but I am not convinced it is the correct way to go about it.
Am I on the right track or just plain lucky that it works?
Wilson
Bookmarks