A program to readout the temperature of multiple DS18XXX sensors on a Multidrop line.
The features are:
' Detects no line / shorted line at startup
' Detects at startup the normal / parasite power mode, adapts the delay
' time after a ConvertT or COPYSCRATCH command in the two power modes
' Detects at startup the presence of (all) One-Wire devices
' Supports in parasite power mode the use of a Strong Bus Pullup Port
' Reads the temperature of multiple devices (a mix of DS18B20/DS1822
' and DS18S20/DS1820)on a Multidrop line.
' Detects transmission errors by calculating CRC
' Selectable decimal places printing; 0 to 4 digits. If choosing 0 digits,
' the temperature will be raised by 1 if the fraction > .5. Changing
' the decimal places printing changes only the output, not the resolution.
' Selectable display refreshing, by the variable Interval
' Reading, calculating and printing the temperature takes less
' than a second with 14 devices connected.
' Temperature display in Celsius or Fahrenheit
' Tested with a Multidrop line of 15 mtr with normal power; 14 devices connected:
' 4 x DS18S20 and 10 x DS18B20
' Important:
' in Normal and Parasite Power Mode the pullup resistor must be 3K3 or 2K7 (3300 or 2700 ohm).
' Perform a test for the use of the Strong Bus Pullup port; not needed in my case.
' (Read DS18B20.pdf, "Powering the DS18B20" and see fig 4.)
'
' The mixture of DS18B20 and DS18S20 isn't a problem; the DS18S20 has standard a
' RESOLUTION OUTPUT of 9 bits, but by calculating the temperature with the use of additional
' registers from the Scratchpad the result is the same as with a DS18B20; 12 bits resolution!
' (See DS18S20.pdf, Operation-Measuring Temperature)
On the screenshot you see the readout of 14 DS18XXX devices, the first 4 are DS18S20 and
the rest are DS18B20, 4 are taken of the line to demo a CRC error #######.
At the end of every line the total number of CRC errors is displayed.
See the program for changing the display interval, the decimal places and Cels. or Fahr.
For your own satisfaction: it would be a nice feature to changes the just called variables
by checking some free port pins, the values (and the display) could be changed on the flight!
Some changes could be made in how the output is produced; you could rewrite the output in a
.csv file format and capturing the output into a file for later analysis.
During the testing I noticed some remarkable things; if you scan the devices too often, they
start to heatup a little; almost 1 degree celsius. So don't scan to quickly.
If you look at 14 temperatures in a row you notice that there is a greater difference in the
readout of the devices than .5 degree celsius (see Appl.Note).That means they need some kind
of callibration, but that depends on your application.
The use of Parasite Power Mode makes no sence to me; the readout is much slower, more transmission errors aso. But it's possible if you like.
Fill in your own Rom-Id's and have fun with it.
~~~~
' *** Read Temperature of DS18xxx devices******
' Detects no line / shorted line at startup
' Detects at startup the normal / parasite power mode, adapts the delay
' time after a ConvertT or COPYSCRATCH command in the two power modes
' Detects at startup the presence of (all) One-Wire devices
' Supports in parasite power mode the use of a Strong Bus Pullup Port
' Reads the temperature of multiple devices (a mix of DS18B20/DS1822
' and DS18S20/DS1820)on a Multidrop line.
' Detects transmission errors by calculating CRC
' Selectable decimal places printing; 0 to 4 digits. If choosing 0 digits,
' the temperature will be raised by 1 if the fraction > .5. Changing
' the decimal places printing changes only the output, not the resolution.
' Selectable display refreshing, by the variable Interval
' Reading, calculating and printing the temperature takes less
' than a second with 14 devices connected
' Temperature display in Celsius or Fahrenheit
' Tested with a Multidrop line of 15 mtr with normal power; 14 devices connected:
' 4 x DS18S20 and 10 x DS18B20
' Important:
' in Normal and Parasite Power Mode the pullup resistor must be 3K3 or 2K7 (3300 or 2700 ohm).
' Perform a test for the use of the Strong Bus Pullup port; not needed in my case.
' (Read DS18B20.pdf, "Powering the DS18B20" and see fig 4.)
'
' The mixture of DS18B20 and DS18S20 isn't a problem; the DS18S20 has standard a
' RESOLUTION OUTPUT of 9 bits, but by calculating the temperature with the use of additional
' registers from the Scratchpad the result is the same as with a DS18B20; 12 bits resolution!
' (See DS18S20.pdf, Operation-Measuring Temperature)
'''Choose the right connections for your Hardware UART and OW/SBP pins.
'''
'''@Theo Loermans
'''@licence GPL
'''@version 1.0
'''@date 09.04.2016
'''**************
#chip mega8, 16
; ----- Define Hardware settings
; Required to LCD to Serial redirection
#define LCD_IO 0
#define USART_BAUD_RATE 9600
Dir PORTD.1 Out
Dir PORTD.0 In
#define USART_BLOCKING
'=========ROM Commands for 1-Wire DS18XXX=========
#define SearchRom 0xF0 '(240) identifying Slave Rom codes
#define ReadRom 0x33 '(51) command for single slave
#define MatchRom 0x55 '(085) identify a specific slave code
#define SkipRom 0xCC '(204) addressing all devices simultaneously
#define AlarmSearch 0xEC '(236) identify any slave with alarm flags
'==========Function Commands for for 1-Wire DS18XXX============
#define ConvertT 0x44 '(68) Single Temp Conversion
#define WriteScratch 0x4E '(78) Write Scratchpad
#define ReadScratch 0xBE '(190) Read Scratchpad
#define CopyScratch 0x48 '(72) copying Scratchpad TH and TL to EEPROM
#define RecallE2 0xB8 '(184) recalling alarm trigger values from EEPROM
#define ReadPowerSupply 0xB4 '(180) identifying Power Supply Mode
#Define DQ PortB.0 ' input/output pin for 1-wire device(s), normal power mode with pullup resistor 3K3 or 2K7
#Define SBP PortB.1 ' Output pin for Strong Bus Pullup, used in Parasite power mode
Dim ROM_NO(9) as byte ' 9 bytes to store ROM ID + CRC, don't use ROM_NO(0)
Dim TSign as string ' Temp Sign " " or "+" or "-"
Dim DSInt as word ' Temp integer
Dim DSdec as integer ' Temp float integer
Dim DSData as integer ' Calculated Temp
Dim PowerModeFlag as Bit ' Power Mode Flag
Dim LongDelayFlag as Bit ' Delaytime in Waitloop
Dim Fraction as string * 7 ' String for fraction, lenght 7
Dim FlagCRCError as Bit ' Indicates CRC error
Dim TotalCrcError as Word ' Total number of CRC errors
TotalCrcError = 0
DecPlaces = 2 ' Number of decimal places (0-4)
Interval = 10 ' Interval(seconds) for reading Temperature, not < 2 seconds,
' otherwise the sensors are self-heating, almost 1 degree Cels!
LongDelayFlag = 0 ' 0 = 10 ms delay after ConvertT, 1 = 750 ms delay after ConverT,
FlagFahrenheit = 0 ' 0 = celsius, 1 = fahrenheit
CRLF
Hserprint "Wait..." : CRLF
MasterRST ' send reset command and check status of sensor(s)
If command = 1 then
HSerPrint "No device(s) detected!":CRLF
End ' exit script for now, future expand
End if
If command = 2 then
HSerPrint "Check line, shorted?":CRLF
End ' exit script, resolve problem
End if
' sequence for checking Power Supply Mode
OWout SkipRom ' send SkipRom command
OWout ReadPowerSup ' send read power supply mode command
OWinBit Command ' get yes/no response bit
If Command Then
HSerPrint "Normal Power Mode detected.":CRLF
PowerModeFlag = 0 ' Normal Power Mode
Else
HSerPrint "Parasite Power Mode detected.":CRLF
PowerModeFlag = 1 ' Parasitepower Mode
End If
MasterRST ' Perform this next sequence to prevent
OWout SkipRom ' an error reading(85.0 deg.) the first
OWout ConvertT ' time after Power Up for the sensors.
WaitLoop ' Don't change the order of this commands
' Start main loop
Do
crlf
ROM_NO(1)=0x10:ROM_NO(2)=0x6C:ROM_NO(3)=0x11:ROM_NO(4)=0xDB
ROM_NO(5)=0x01:ROM_NO(6)=0x08:ROM_NO(7)=0x00:ROM_NO(8)=0xBC
gosub CalcTemp
ROM_NO(1)=0x10:ROM_NO(2)=0x31:ROM_NO(3)=0x21:ROM_NO(4)=0xDB
ROM_NO(5)=0x01:ROM_NO(6)=0x08:ROM_NO(7)=0x00:ROM_NO(8)=0x5C
gosub CalcTemp
ROM_NO(1)=0x10:ROM_NO(2)=0xD9:ROM_NO(3)=0x96:ROM_NO(4)=0xAA
ROM_NO(5)=0x02:ROM_NO(6)=0x08:ROM_NO(7)=0x00:ROM_NO(8)=0x09
gosub CalcTemp
ROM_NO(1)=0x10:ROM_NO(2)=0xF7:ROM_NO(3)=0x31:ROM_NO(4)=0xAB
ROM_NO(5)=0x02:ROM_NO(6)=0x08:ROM_NO(7)=0x00:ROM_NO(8)=0x41
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x50:ROM_NO(4)=0x91
ROM_NO(5)=0xA4:ROM_NO(6)=0x15:ROM_NO(7)=0x04:ROM_NO(8)=0xA9
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x82:ROM_NO(4)=0x49
ROM_NO(5)=0xA8:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0xED
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x9A:ROM_NO(4)=0x31
ROM_NO(5)=0xA8:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0x1B
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x0E:ROM_NO(4)=0x5C
ROM_NO(5)=0xB2:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0x9E
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0xDE:ROM_NO(4)=0x44
ROM_NO(5)=0xB2:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0xE4
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x99:ROM_NO(4)=0x83
ROM_NO(5)=0xB2:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0xE3
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0xF9:ROM_NO(4)=0x0A
ROM_NO(5)=0xA8:ROM_NO(6)=0x15:ROM_NO(7)=0x03:ROM_NO(8)=0x24
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0x43:ROM_NO(4)=0x84
ROM_NO(5)=0xB2:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0x86
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0xB3:ROM_NO(4)=0x4C
ROM_NO(5)=0xA8:ROM_NO(6)=0x15:ROM_NO(7)=0x03:ROM_NO(8)=0x99
gosub CalcTemp
ROM_NO(1)=0x28:ROM_NO(2)=0xFF:ROM_NO(3)=0xB3:ROM_NO(4)=0x81
ROM_NO(5)=0xB2:ROM_NO(6)=0x15:ROM_NO(7)=0x01:ROM_NO(8)=0xA1
gosub CalcTemp
If FlagFahrenheit Then
HSerPrint " deg. Fahrenheit."
Else
HSerPrint " deg. Celsius."
End If
Hserprint " CRC error: ":Hserprint TotalCrcError ' print total CRC errors
wait Interval S ' not < 2 seconds, otherwise the sensors start to heat up, almost 1 degree Cels!
MasterRST
OWout SkipRom
OWout ConvertT
WaitLoop
Loop
'-------------------------------- SUBS -------------------------------
Sub CRLF ' Carriage return, Line feed
HSerSend 13
HSerSend 10
End sub
Sub Calctemp ' Calculate temperature
FamID = ROM_NO(1) ' store family ID for determing delay
MasterRST ' reset devices
OWout MatchRom ' select 1 device by
For index = 1 to 8 ' sending ROM-ID to sensor
OWout ROM_NO(index)
Next
OWout ReadScratch ' read selected device
For index = 1 to 9
Owin ROM_NO(index) ' read DS memory bytes and store in ROM_NO(x)
Next
CalcCRC ' calculate CRC
FlagCRCError = 0
If CRC <> ROM_NO(9) Then ' Calculated CRC identical to DS-ROM CRC?
TotalCrcError = TotalCrcError + 1
FlagCRCError = 1
End If
If not FlagCRCError Then ' calculate and print temperature
DSData = ROM_NO(1) ' create a 16 bit value
DSData_H = ROM_NO(2) ' for the temperature.
TSign = " " ' or "+" if you like
If FamID = 0x10 Then ' DS1820/DS18S20 needs special calculation.
DSData = FnLSR(DSData,1) ' calculate temperature with the
DSData = FNLSL(DSData,4) ' value of 2 additional register
DSData = DSData -4 + [word](ROM_NO(8)-ROM_NO(7))' and decrement 0.25
end if
If DSData.11 = 1 then ' Negative temperature
TSign = "-"
DSData = (DSData XOR 0xFFFF) + 1' two's complement
End If
If FlagFahrenheit then
DSData = (DSData*9)/5 + 32*16
End if
DSInt = (FnLSR(DSData,4) AND 0x7F) ' Split the calculated temperature in integer
DSdec = ((DSData AND 0x000F)*625) ' and fraction
Fraction = STR(DSdec) ' create a string with the fraction in it
If Fraction = "0" then Fraction = "0000" ' if there are too less leading zeros, create a
If Fraction = "625" then Fraction = "0625" ' string with leading zeros
If DecPlaces = 0 then ' if printing temperature with zero decimal places
If DSdec > 4999 then ' then increase temperature if fraction > 4999
DSInt = DSInt + 1
End if
Hserprint TSign : HSerPrint DSInt : HSerPrint " "' print temperature integer
Else
HSerPrint TSign : HSerPrint DSInt : HSerPrint "." ' Or "," if you like
HSerPrint left(Fraction,DecPlaces) ' print fraction, DecPlaces is number of digits
HSerprint " "
End if
Else
Fraction = Fill(4 + DecPlaces,"#" ) ' adapt lenght for a nice output
HSerprint Fraction : HSerPrint " " ' if there's a CRC Error
End If
End Sub
Sub MasterRST (optional command=0)
IntOff
Dir DQ Out
Set DQ Off
wait 480 us
Dir DQ In
wait 86 us ' 70 us default. Tested with 15mtr line: low 30, High 143; (143-30)/2=56+30=86
If DQ On then ' No sensor(s) found
command = 1
end If
wait 410 us ' 410 us default: tested 394
If DQ Off then ' line still low after end time slot: shorted?
Command = 2
end If
IntOn
end sub
Sub OWout (In Command)
IntOff
if Command = 0x44 then ' command is ConverT; use long delay time in WaitLoop
LongDelayFlag = 1 ' : hserprint "Bingo** "
End if
Repeat 8
wait 1 us ' Recovery time between write cycles
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
If Command.0 On then
Dir DQ In
End If
wait 60 us
Dir DQ In
ROTATE Command Right ' LSB first
End Repeat
IntOn
end sub
Sub OWoutBit (In Command)
IntOff
wait 1 us ' Recovery time between write cycles
Dir DQ Out
Set DQ Off
wait 6 us
If Command.0 On then
Dir DQ In
End If
wait 60 us
Dir DQ In
IntOn
end sub
Sub OWin (Command)
IntOff ' Interrupt Disabled
Repeat 8
Rotate Command Right ' LSB first
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
Dir DQ In ' Release bus for one wire Reception
wait 9 us
If DQ On Then Set Command.7 1
If DQ Off Then Set Command.7 0
wait 55 us
End Repeat
IntOn ' Interrupt Enabled
end sub
Sub OWinBit (Command)
IntOff
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
Dir DQ In ' Release bus for one wire Reception
wait 9 us
If DQ On Then Set Command = 1
If DQ Off then Set Command = 0
wait 55 us
IntOn
end Sub
Sub WaitLoop ' wait for DS after ConvertT
IntOff
If PowerModeFlag = 1 Then ' Parasite power Mode Flag
wait 3 us ' Strong Pullup active within 10 us
SET DQ On
DIR DQ Out
Set SBP On ' set Strong Bus Power on
Dir SBP Out ' SET SBP Output, activate Strong Bus Power port
if LongDelayFlag = 1 then ' adapt wait time:
wait 750 ms ' 750ms default with ConverT
else
wait 10 ms ' 10ms default with Copy Scratchpad
End if
Set SBP Off
Dir SBP In
Dir DQ In
Set DQ Off
Else ' normal power mode
Set DQ Off
Dir DQ out
wait 4 us
Dir DQ In
wait 7 us
Do
If DQ = 1 Then
wait 60 us 'minimum 60 us time slots between reads
Exit Do
End If
Loop
End If
LongDelayFlag = 0 ' set delay time back to short
IntOn
end sub
Sub CalcCRC
CRC = 0 ' Calculate CRC8: DALLAS/MAXIM, reversed Polynomial
For index = 1 to 8
Tmp1 = ROM_NO(index)
Repeat 8
Tmp2 = (Tmp1 XOR CRC) AND 1
CRC = CRC/2
Tmp1 = Tmp1/2
If Tmp2 = 1 then
CRC = CRC XOR 140 ' 0x8C
end If
end Repeat
Next
end sub
~~~~
A program to readout the temperature of multiple DS18XXX sensors on a Multidrop line.
The features are:
' Detects no line / shorted line at startup
' Detects at startup the normal / parasite power mode, adapts the delay
' time after a ConvertT or COPYSCRATCH command in the two power modes
' Detects at startup the presence of (all) One-Wire devices
' Supports in parasite power mode the use of a Strong Bus Pullup Port
' Reads the temperature of multiple devices (a mix of DS18B20/DS1822
' and DS18S20/DS1820)on a Multidrop line.
' Detects transmission errors by calculating CRC
' Selectable decimal places printing; 0 to 4 digits. If choosing 0 digits,
' the temperature will be raised by 1 if the fraction > .5. Changing
' the decimal places printing changes only the output, not the resolution.
' Selectable display refreshing, by the variable Interval
' Reading, calculating and printing the temperature takes less
' than a second with 14 devices connected.
' Temperature display in Celsius or Fahrenheit
' Tested with a Multidrop line of 15 mtr with normal power; 14 devices connected:
' 4 x DS18S20 and 10 x DS18B20
' Important:
' in Normal and Parasite Power Mode the pullup resistor must be 3K3 or 2K7 (3300 or 2700 ohm).
' Perform a test for the use of the Strong Bus Pullup port; not needed in my case.
' (Read DS18B20.pdf, "Powering the DS18B20" and see fig 4.)
'
' The mixture of DS18B20 and DS18S20 isn't a problem; the DS18S20 has standard a
' RESOLUTION OUTPUT of 9 bits, but by calculating the temperature with the use of additional
' registers from the Scratchpad the result is the same as with a DS18B20; 12 bits resolution!
' (See DS18S20.pdf, Operation-Measuring Temperature)
On the screenshot you see the readout of 14 DS18XXX devices, the first 4 are DS18S20 and
the rest are DS18B20, 4 are taken of the line to demo a CRC error #######.
At the end of every line the total number of CRC errors is displayed.
See the program for changing the display interval, the decimal places and Cels. or Fahr.
For your own satisfaction: it would be a nice feature to changes the just called variables
by checking some free port pins, the values (and the display) could be changed on the flight!
Some changes could be made in how the output is produced; you could rewrite the output in a
.csv file format and capturing the output into a file for later analysis.
During the testing I noticed some remarkable things; if you scan the devices too often, they
start to heatup a little; almost 1 degree celsius. So don't scan to quickly.
If you look at 14 temperatures in a row you notice that there is a greater difference in the
readout of the devices than .5 degree celsius (see Appl.Note).That means they need some kind
of callibration, but that depends on your application.
The use of Parasite Power Mode makes no sence to me; the readout is much slower, more transmission errors aso. But it's possible if you like.
Fill in your own Rom-Id's and have fun with it.
~~~~
' *** Read Temperature of DS18xxx devices******
' Detects no line / shorted line at startup
' Detects at startup the normal / parasite power mode, adapts the delay
' time after a ConvertT or COPYSCRATCH command in the two power modes
' Detects at startup the presence of (all) One-Wire devices
' Supports in parasite power mode the use of a Strong Bus Pullup Port
' Reads the temperature of multiple devices (a mix of DS18B20/DS1822
' and DS18S20/DS1820)on a Multidrop line.
' Detects transmission errors by calculating CRC
' Selectable decimal places printing; 0 to 4 digits. If choosing 0 digits,
' the temperature will be raised by 1 if the fraction > .5. Changing
' the decimal places printing changes only the output, not the resolution.
' Selectable display refreshing, by the variable Interval
' Reading, calculating and printing the temperature takes less
' than a second with 14 devices connected
' Temperature display in Celsius or Fahrenheit
' Tested with a Multidrop line of 15 mtr with normal power; 14 devices connected:
' 4 x DS18S20 and 10 x DS18B20
' Important:
' in Normal and Parasite Power Mode the pullup resistor must be 3K3 or 2K7 (3300 or 2700 ohm).
' Perform a test for the use of the Strong Bus Pullup port; not needed in my case.
' (Read DS18B20.pdf, "Powering the DS18B20" and see fig 4.)
'
' The mixture of DS18B20 and DS18S20 isn't a problem; the DS18S20 has standard a
' RESOLUTION OUTPUT of 9 bits, but by calculating the temperature with the use of additional
' registers from the Scratchpad the result is the same as with a DS18B20; 12 bits resolution!
' (See DS18S20.pdf, Operation-Measuring Temperature)
'''Choose the right connections for your Hardware UART and OW/SBP pins.
'''
'''@Theo Loermans
'''@licence GPL
'''@version 1.0
'''@date 09.04.2016
'''**************
#chip mega8, 16
; ----- Define Hardware settings
; Required to LCD to Serial redirection
#define LCD_IO 0
#define USART_BAUD_RATE 9600
Dir PORTD.1 Out
Dir PORTD.0 In
#define USART_BLOCKING
'=========ROM Commands for 1-Wire DS18XXX=========
#define SearchRom 0xF0 '(240) identifying Slave Rom codes
#define ReadRom 0x33 '(51) command for single slave
#define MatchRom 0x55 '(085) identify a specific slave code
#define SkipRom 0xCC '(204) addressing all devices simultaneously
#define AlarmSearch 0xEC '(236) identify any slave with alarm flags
#Define DQ PortB.0 ' input/output pin for 1-wire device(s), normal power mode with pullup resistor 3K3 or 2K7
#Define SBP PortB.1 ' Output pin for Strong Bus Pullup, used in Parasite power mode
Dim ROM_NO(9) as byte ' 9 bytes to store ROM ID + CRC, don't use ROM_NO(0)
Dim TSign as string ' Temp Sign " " or "+" or "-"
Dim DSInt as word ' Temp integer
Dim DSdec as integer ' Temp float integer
Dim DSData as integer ' Calculated Temp
Dim PowerModeFlag as Bit ' Power Mode Flag
Dim LongDelayFlag as Bit ' Delaytime in Waitloop
Dim Fraction as string * 7 ' String for fraction, lenght 7
Dim FlagCRCError as Bit ' Indicates CRC error
Dim TotalCrcError as Word ' Total number of CRC errors
TotalCrcError = 0
DecPlaces = 2 ' Number of decimal places (0-4)
Interval = 10 ' Interval(seconds) for reading Temperature, not < 2 seconds,
' otherwise the sensors are self-heating, almost 1 degree Cels!
LongDelayFlag = 0 ' 0 = 10 ms delay after ConvertT, 1 = 750 ms delay after ConverT,
FlagFahrenheit = 0 ' 0 = celsius, 1 = fahrenheit
Hserprint "Wait..." : CRLF
MasterRST ' send reset command and check status of sensor(s)
If command = 1 then
HSerPrint "No device(s) detected!":CRLF
End ' exit script for now, future expand
End if
If command = 2 then
HSerPrint "Check line, shorted?":CRLF
End ' exit script, resolve problem
End if
' sequence for checking Power Supply Mode
OWout SkipRom ' send SkipRom command
OWout ReadPowerSup ' send read power supply mode command
OWinBit Command ' get yes/no response bit
If Command Then
HSerPrint "Normal Power Mode detected.":CRLF
PowerModeFlag = 0 ' Normal Power Mode
Else
HSerPrint "Parasite Power Mode detected.":CRLF
PowerModeFlag = 1 ' Parasitepower Mode
End If
MasterRST ' Perform this next sequence to prevent
OWout SkipRom ' an error reading(85.0 deg.) the first
OWout ConvertT ' time after Power Up for the sensors.
WaitLoop ' Don't change the order of this commands
' Start main loop
Do
crlf
ROM_NO(1)=0x10:ROM_NO(2)=0x6C:ROM_NO(3)=0x11:ROM_NO(4)=0xDB
ROM_NO(5)=0x01:ROM_NO(6)=0x08:ROM_NO(7)=0x00:ROM_NO(8)=0xBC
gosub CalcTemp
Sub MasterRST (optional command=0)
IntOff
Dir DQ Out
Set DQ Off
wait 480 us
Dir DQ In
wait 86 us ' 70 us default. Tested with 15mtr line: low 30, High 143; (143-30)/2=56+30=86
If DQ On then ' No sensor(s) found
command = 1
end If
wait 410 us ' 410 us default: tested 394
If DQ Off then ' line still low after end time slot: shorted?
Command = 2
end If
IntOn
end sub
Sub OWout (In Command)
IntOff
if Command = 0x44 then ' command is ConverT; use long delay time in WaitLoop
LongDelayFlag = 1 ' : hserprint "Bingo** "
End if
Repeat 8
wait 1 us ' Recovery time between write cycles
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
If Command.0 On then
Dir DQ In
End If
wait 60 us
Dir DQ In
ROTATE Command Right ' LSB first
End Repeat
IntOn
end sub
Sub OWoutBit (In Command)
IntOff
wait 1 us ' Recovery time between write cycles
Dir DQ Out
Set DQ Off
wait 6 us
If Command.0 On then
Dir DQ In
End If
wait 60 us
Dir DQ In
IntOn
end sub
Sub OWin (Command)
IntOff ' Interrupt Disabled
Repeat 8
Rotate Command Right ' LSB first
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
Dir DQ In ' Release bus for one wire Reception
wait 9 us
If DQ On Then Set Command.7 1
If DQ Off Then Set Command.7 0
wait 55 us
End Repeat
IntOn ' Interrupt Enabled
end sub
Sub OWinBit (Command)
IntOff
Dir DQ Out
Set DQ Off ' Start time slot
wait 6 us
Dir DQ In ' Release bus for one wire Reception
wait 9 us
If DQ On Then Set Command = 1
If DQ Off then Set Command = 0
wait 55 us
IntOn
end Sub
Sub WaitLoop ' wait for DS after ConvertT
IntOff
If PowerModeFlag = 1 Then ' Parasite power Mode Flag
wait 3 us ' Strong Pullup active within 10 us
SET DQ On
DIR DQ Out
Set SBP On ' set Strong Bus Power on
Dir SBP Out ' SET SBP Output, activate Strong Bus Power port
if LongDelayFlag = 1 then ' adapt wait time:
wait 750 ms ' 750ms default with ConverT
else
wait 10 ms ' 10ms default with Copy Scratchpad
End if
Set SBP Off
Dir SBP In
Dir DQ In
Set DQ Off
Else ' normal power mode
Set DQ Off
Dir DQ out
wait 4 us
Dir DQ In
wait 7 us
Do
If DQ = 1 Then
wait 60 us 'minimum 60 us time slots between reads
Exit Do
End If
Loop
End If
LongDelayFlag = 0 ' set delay time back to short
IntOn
end sub
Sub CalcCRC
CRC = 0 ' Calculate CRC8: DALLAS/MAXIM, reversed Polynomial
For index = 1 to 8
Tmp1 = ROM_NO(index)
Repeat 8
Tmp2 = (Tmp1 XOR CRC) AND 1
CRC = CRC/2
Tmp1 = Tmp1/2
If Tmp2 = 1 then
CRC = CRC XOR 140 ' 0x8C
end If
end Repeat
Next
end sub
~~~~
Last edit: Theo 2016-04-09