Code:
INCLUDE "modedefs.bas"
DEFINE OSC 20 ' declare xtal to give exact delays
ip var Byte ' hi pulse from sensor
temp var Byte
hum var Byte
hum1 var Byte
chksm var Byte
test var Byte
i var Byte
TRISB.0 = 0 ' PORTB.0 = output, OneWire sensor
TRISB.1 = 0 ' PORTB.1 = output, serout
Pause 1000 ' give sensor time to settle
mainloop:
chksm = 0
' TRISB.0 = 0 ' portb o/p
PORTB.0 = 1 ' make high
pause 50 ' wait for a while
PORTB.0 = 0 : pause 18 ' send 18ms low
PORTB.0 = 1 : pauseus 30 ' send 30us hi
PulsIn PORTB.0, 1, ip ' wait for hi
If ip < 130 Then GoTo subloop ' if < 80us loop
ip = 0
For i = 7 To 0 Step -1 ' 8 bits (1..8)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
hum = hum | 1 << i ' SetBit hum, i
Else
hum = hum xor (1 << i) ' ClearBit hum, i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (9..16)
PulsIn PORTB.0,1, ip ' receive pulses from sensor
If ip > 70 Then
hum1 = hum1 | 1 << i ' SetBit hum1, i
Else
hum1 = hum1 XOR (1 << i) ' ClearBit hum1,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (17..24)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
temp = temp | 1 << i ' SetBit temp, i
Else
temp = temp xor (1 << i) ' ClearBit temp,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (25..32)
PulsIn PORTB.0, 1, ip ' receive pulses from sensor
If ip > 70 Then
temp1 = temp1 | 1 << i ' SetBit temp1, i
Else
temp1 = temp1 xor (1 << i) ' ClearBit temp1,i
EndIf
Next i
For i = 7 To 0 Step -1 ' 8 bits (33..40)
PulsIn PORTB.0, 1, ip
If ip > 70 Then
chksm = chksm | 1 << i ' SetBit chksm,i
Else
chksm = chksm xor (1 << i) ' ClearBit chksm,i
EndIf
Next i
SerOut PORTB.1, 4, ["Check sum = ", #chksm, 13]
serout PORTB.1, 4, ["Humidity = ", #hum1, 13]
serout PORTB.1, 4, ["Temperature = ", #temp, "C.", 13]
test = (temp1 + hum1 + hum + temp)
If chksm <> test Then serout PORTB.1, 4, ["Checksum error", 13] 'HRSOut "error",13
pause 3000 ' give sensor time to settle
GoTo mainloop
subloop:
serout PORTB.1, 4, ["Sensor not ready", 13]
GoTo mainloop
I have some doubs:
Bookmarks