16F DS18S20 Example

There are 3
digital temperature sensors, so be sure to get the correct one. There is the
1820, 18S20 and
18B20. The 18S20 is the successor to the 1820, and does not
drift over time. The
18B20 does offer faster conversions, but for me, the 18S20
more more readily available, and suits my applications fine. Be aware that the
18B20 does require different methods of temperature conversion then that
described in this manual for the 18S20.
The basic
wiring diagram

Note the PIC's
power supply/oscillator are not shown
Click here
to watch this circuit in action
Be sure to attach the
pull-up
resistor to the databus, this is a requirement of the Dallas 1-Wire
system. This code communicates with the 18S20 with its 64 bit address, you can
attach multiple units to the databus, but be sure that you know the 64 bit
address so that the correct device is addressed. To ascertain what the device's
64 bit address is, see the
1 Wire introduction section.
Be aware that the 64 bit address information is the inner 8 pieces of
information in the serial commands, the first and last piece's of information
are commands for the device addressed.
Device 16F877
XTAL 4
Declare LCD_TYPE ALPHA ' Type of LCD Used is Alpha
Declare LCD_DTPIN PORTB.4 ' The control bits B0,B1,B2,B3
Declare LCD_RSPIN PORTB.2 ' RS pin on B4
Declare LCD_ENPIN PORTB.3 ' E pin on B5
Declare LCD_LINES 2 ' Amount of LCD lines
Declare LCD_INTERFACE 4 ' Interface method is 4 bit
PORTB_PULLUPS = true
ALL_DIGITAL = TRUE
SYMBOL DQ = Portb.0 'Place the DS1820 on bit 1 of PORTA
Dim Sign as Byte
Dim Cnt as Byte
Dim Count_Per_Deg as Byte
Dim Temp as Word
Dim Temp_Dec as Byte
Delayms 150 ' Let LCD warm up
Cls
' Send a command that creates the degree
Print $FE,$40,$06,$09,$09,$06,$00,$00,$00,$00
' symbol on the LCD, to print it, simply address
' the character as 0, eg,
' Print At 1, 1, Value, 0, "Degrees"
Again:
'Send calculate temperature command
OWRITE DQ, 1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$44]
' FF I1 I2 I3 I4 I5 I6 CRC
' FF - Family, Ix - Address bytes,
' CRC - checksum
REPEAT
DELAYMS 25 ' Wait until conversion is complete
OREAD DQ,4,[Cnt] ' Keep reading low pulses until
UNTIL Cnt <> 0 ' the DS1820 is finished
'Send read scratchpad command
OWRITE DQ,1,[$55,$10,$31,$C5,$C8,$00,$00,$00,$F4,$BE]
' FF I1 I2 I3 I4 I5 I6 CRC
' FF - Family, Ix - Address bytes,
' CRC - checksum checksum
OREAD DQ,2,[Temp.LOWBYTE,Temp.HIGHBYTE,Cnt,Cnt,Cnt,Cnt,Cnt,Count_Per_Deg]
' Each Cnt is 1/16 of a degree, therefore 100/16 = 6.25, and that's our decimal value
Temp_Dec = (6.25 * Cnt)
' Bits 8-15 are 1 for a negative temperature and 0 for a positive
If Temp.8=1 Then
' If negative then drop the first bit, and invert the value
Temp=(Temp.LowByte ^ $FF) >> 1
' If Cnt = 0 then increment Temp, as the 1820 does not do this
If Cnt = 0 then Temp = Temp + 1
' Change the sign to a negative
Sign = "-"
Else
' If positive then drop the first byte
Temp=(Temp >> 1)
' And change the sign value to positive
Sign = "+"
' Invert the decimal value
Temp_Dec = 100 - Temp_Dec
Endif
' Display the data on the LCD
Print At 1,1, Sign, Dec Temp, ".", Dec DIG Temp_Dec, 1, 0,"C "
Goto Again
* The green line should be moved to after "Temp_Dec = (6.25 *
Cnt)" If simulated in ISIS, for some reason the ISIS model inverts the decimal
value differently compared to a real-life component.

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |