I started coding a one wire communication interface for communicating with the DS18B20 temperature sensor directly instead of reading the one wire bus bus. I managed to directly read the temperature using the below tutorial:
Method dTemperature (sSensorID as string) as double
dim ftemp as new FolderItem
Dim t As TextInputStream
dim sHulp as string
try
ftemp = new FolderItem("/sys/bus/w1/devices/" + sSensorID + "/w1_slave", FolderItem.PathTypeAbsolute)
t = TextInputStream.Open(ftemp)
sHulp = t.ReadLine
if sHulp.Right(3)= "YES" then
'read 2nd line
sHulp = t.ReadLine
return sHulp.Mid(sHulp.InStrB("t=")+2).Val/1000
else
return 99999
end if
End Method
Now i try to directly communicate with the sensor using GPIO. I followed the following tuturial but with no luck so far (and I don’t have a oscilloscope). Maybe some people have some ideas what should be changed in the code. The code consists of a class called Onewire. The class has the following methods so far:
- Initiate
- Readbit
- Readdata
- WriteByte
- WriteOne
- WriteZero
Initiate - return boolean:
//Steps to reset one wire bus
//* Pull bus low
//* hold condition for 480us
//* release bus
//* wait for 60us
//* read bus
//* if bus low then device present set / return var accordingly
//* wait for balance period (480-60)
Dim value, iBit as Integer
'start
' minimal 480 Microseconds low
GPIO.PinMode(pin, GPIO.OUTPUT)
GPIO.DigitalWrite(pin, GPIO.LOW)
GPIO.DelayMicroseconds(480)
'go high and wait for 60 Microseconds
GPIO.DigitalWrite(pin, GPIO.HIGH)
GPIO.DelayMicroseconds(60)
GPIO.PinMode(pin, GPIO.INPUT)
value = GPIO.DigitalRead( pin )
GPIO.DelayMicroseconds(480)
if value = 0 then
Return True
else
Return false
end if
Readbit - return integer
//*Steps for master to issue a read request to slave device on bus aka milk slave device
//* pull bus low
//* hold for 5us
//* release bus
//* wait for 45us for recovery (46us+5us=61us)
Dim value, iBit as Integer
iBit =0
GPIO.PinMode(pin, GPIO.OUTPUT)
GPIO.DigitalWrite(pin, GPIO.LOW)
GPIO.DelayMicroseconds(5)
GPIO.PinMode(pin, GPIO.INPUT)
GPIO.DelayMicroseconds(15)
value = GPIO.DigitalRead( pin )
if value = 1 then
iBit = 1
end if
GPIO.DelayMicroseconds(46)
return iBit
Readdata (bits as integer) - returns string
dim sHulp as string
dim r as Integer
sHulp = ""
for r = 1 to bits
if Readbit = 1 then
sHulp = sHulp +"1"
else
sHulp = sHulp + "0"
end if
next r
return shulp
WriteOne
//*Steps for master to transmit logical one to slave device on bus
//* pull bus low
//* hold for 5us
//* release bus
//* wait for 1us for recovery
GPIO.DigitalWrite(pin, GPIO.LOW)
GPIO.DelayMicroseconds(5)
GPIO.DigitalWrite(pin, GPIO.HIGH)
GPIO.DelayMicroseconds(1)
WriteZero
//Steps for master to transmit logical zero to slave device on bus
//*pull bus low
//* hold for 60us
//* release bus
//* wait for 1us for recovery
GPIO.DigitalWrite(pin, GPIO.LOW)
GPIO.DelayMicroseconds(60)
GPIO.DigitalWrite(pin, GPIO.HIGH)
GPIO.DelayMicroseconds(1)
WriteByte (Byte as integer, Power as Boolean)
dim sHulp as string
dim r, ilen as Integer
sHulp = Bin(Byte)
GPIO.PinMode(pin, GPIO.OUTPUT)
ilen = sHulp.Len
for r = 1 to ilen
if sHulp.Mid(ilen-r+1,1) = "1" then
WriteZero
else
WriteOne
end if
next r
for r = 1 to 8-ilen
WriteZero
next r
if Power then
//GPIO.PinMode(pin, GPIO.OUTPUT)
GPIO.DigitalWrite(pin, GPIO.HIGH)
GPIO.Delay(10)
end if
To read the temperature you need the following code:
dim temp as new Onewire
//#define DS1820_SKIP_ROM 0xCC
//#define DS1820_READ_SCRATCHPAD 0xBE
//#define DS1820_CONVERT_T 0x44
temp.pin = 4
if temp.Initiate = true then
sHulp = "connected "
temp.WriteByte(&h0CC,false)
temp.WriteByte(&h044,true)
GPIO.Delay(755)
''if temp.Initiate = true then
''temp.WriteByte(&h0CC,false)
temp.WriteByte(&h0BE,false)
sHulp = sHulp + temp.Readdata(16)
''end if
else
sHulp = "error "
end if
TextArea1.Text = sHulp
In the tutorial is also mentioned that “Since earlier I mentioned about that releasing bus is not as same as pushing the bus high”. But how can you release then using GPIO?
The link to the full code I have here.