-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18b20.lua
59 lines (55 loc) · 1.65 KB
/
18b20.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- 18b20 Example
-- local owtimer = tmr.create()
pin = 21
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
until (addr ~= nil) or (count > 3)
if addr == nil then
print("No more addresses.")
else
print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
print("Device is a DS18S20 family device.")
-- repeat
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
-- tmr.delay(1000000)
--owtimer:register(10, tmr.ALARM_SINGLE, function (t) -- print("expired") end)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
print("P="..present)
data = nil
data = string.char(ow.read(pin))
for increment = 1, 8 do
data = data .. string.char(ow.read(pin))
end
print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
print("CRC="..crc)
if crc == data:byte(9) then
tbits = (data:byte(1) + data:byte(2) * 256) * 625
ow_temp1 = tbits / 10000
if ow_temp1 > 200 then ow_temp1 = ow_temp1 - 4095
end
--t2 = t % 10000
--print("Temperature="..t1.."."..t2.." Centigrade")
print("Temperature = " ..ow_temp1 .. " Celsius")
end
-- until false
-- end)
-- owtimer:start()
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end