Formatted ds18b20.lua.
This commit is contained in:
parent
c9bce72cec
commit
d4dcf55764
|
@ -35,27 +35,27 @@ setfenv(1,M)
|
||||||
C = 0
|
C = 0
|
||||||
F = 1
|
F = 1
|
||||||
K = 2
|
K = 2
|
||||||
function setup(dq)
|
function setup(dq)
|
||||||
pin = dq
|
pin = dq
|
||||||
if(pin == nil) then
|
if(pin == nil) then
|
||||||
pin = defaultPin
|
pin = defaultPin
|
||||||
end
|
end
|
||||||
ow.setup(pin)
|
ow.setup(pin)
|
||||||
end
|
end
|
||||||
|
|
||||||
function addrs()
|
function addrs()
|
||||||
setup(pin)
|
setup(pin)
|
||||||
tbl = {}
|
tbl = {}
|
||||||
ow.reset_search(pin)
|
ow.reset_search(pin)
|
||||||
repeat
|
repeat
|
||||||
addr = ow.search(pin)
|
addr = ow.search(pin)
|
||||||
if(addr ~= nil) then
|
if(addr ~= nil) then
|
||||||
table.insert(tbl, addr)
|
table.insert(tbl, addr)
|
||||||
end
|
end
|
||||||
tmr.wdclr()
|
tmr.wdclr()
|
||||||
until (addr == nil)
|
until (addr == nil)
|
||||||
ow.reset_search(pin)
|
ow.reset_search(pin)
|
||||||
return tbl
|
return tbl
|
||||||
end
|
end
|
||||||
|
|
||||||
function readNumber(addr, unit)
|
function readNumber(addr, unit)
|
||||||
|
@ -64,71 +64,71 @@ function readNumber(addr, unit)
|
||||||
flag = false
|
flag = false
|
||||||
if(addr == nil) then
|
if(addr == nil) then
|
||||||
ow.reset_search(pin)
|
ow.reset_search(pin)
|
||||||
count = 0
|
count = 0
|
||||||
repeat
|
repeat
|
||||||
count = count + 1
|
count = count + 1
|
||||||
addr = ow.search(pin)
|
addr = ow.search(pin)
|
||||||
tmr.wdclr()
|
tmr.wdclr()
|
||||||
until((addr ~= nil) or (count > 100))
|
until((addr ~= nil) or (count > 100))
|
||||||
ow.reset_search(pin)
|
ow.reset_search(pin)
|
||||||
end
|
end
|
||||||
if(addr == nil) then
|
if(addr == nil) then
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
crc = ow.crc8(string.sub(addr,1,7))
|
crc = ow.crc8(string.sub(addr,1,7))
|
||||||
if (crc == addr:byte(8)) then
|
if (crc == addr:byte(8)) then
|
||||||
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
|
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
|
||||||
-- print("Device is a DS18S20 family device.")
|
-- print("Device is a DS18S20 family device.")
|
||||||
ow.reset(pin)
|
ow.reset(pin)
|
||||||
ow.select(pin, addr)
|
ow.select(pin, addr)
|
||||||
ow.write(pin, 0x44, 1)
|
ow.write(pin, 0x44, 1)
|
||||||
-- tmr.delay(1000000)
|
-- tmr.delay(1000000)
|
||||||
present = ow.reset(pin)
|
present = ow.reset(pin)
|
||||||
ow.select(pin, addr)
|
ow.select(pin, addr)
|
||||||
ow.write(pin,0xBE,1)
|
ow.write(pin,0xBE,1)
|
||||||
-- print("P="..present)
|
-- print("P="..present)
|
||||||
data = nil
|
data = nil
|
||||||
data = string.char(ow.read(pin))
|
data = string.char(ow.read(pin))
|
||||||
for i = 1, 8 do
|
for i = 1, 8 do
|
||||||
data = data .. string.char(ow.read(pin))
|
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
|
||||||
|
if(unit == nil or unit == C) then
|
||||||
|
t = (data:byte(1) + data:byte(2) * 256) * 625
|
||||||
|
elseif(unit == F) then
|
||||||
|
t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000
|
||||||
|
elseif(unit == K) then
|
||||||
|
t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500
|
||||||
|
else
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
-- print(data:byte(1,9))
|
t1 = t / 10000
|
||||||
crc = ow.crc8(string.sub(data,1,8))
|
t2 = t % 10000
|
||||||
-- print("CRC="..crc)
|
-- print("Temperature="..t1.."."..t2.." Centigrade")
|
||||||
if (crc == data:byte(9)) then
|
-- result = t1.."."..t2
|
||||||
if(unit == nil or unit == C) then
|
return t1, t2
|
||||||
t = (data:byte(1) + data:byte(2) * 256) * 625
|
end
|
||||||
elseif(unit == F) then
|
tmr.wdclr()
|
||||||
t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000
|
|
||||||
elseif(unit == K) then
|
|
||||||
t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500
|
|
||||||
else
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
t1 = t / 10000
|
|
||||||
t2 = t % 10000
|
|
||||||
-- print("Temperature="..t1.."."..t2.." Centigrade")
|
|
||||||
-- result = t1.."."..t2
|
|
||||||
return t1, t2
|
|
||||||
end
|
|
||||||
tmr.wdclr()
|
|
||||||
else
|
else
|
||||||
-- print("Device family is not recognized.")
|
-- print("Device family is not recognized.")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- print("CRC is not valid!")
|
-- print("CRC is not valid!")
|
||||||
end
|
end
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
function read(addr, unit)
|
function read(addr, unit)
|
||||||
t1, t2 = readNumber(addr, unit)
|
t1, t2 = readNumber(addr, unit)
|
||||||
if((t1 == nil ) or (t2 ==nil)) then
|
if((t1 == nil ) or (t2 ==nil)) then
|
||||||
return nil
|
return nil
|
||||||
else
|
else
|
||||||
return t1.."."..t2
|
return t1.."."..t2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Return module table
|
-- Return module table
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in New Issue