Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
169699b9
Commit
169699b9
authored
Jan 31, 2015
by
Vladimir Dronnikov
Browse files
ds18b20: negatives handled
parent
278e3541
Changes
1
Hide whitespace changes
Inline
Side-by-side
lua_examples/yet-another-ds18b20.lua
View file @
169699b9
...
@@ -5,10 +5,11 @@
...
@@ -5,10 +5,11 @@
-- Vladimir Dronnikov <dronnikov@gmail.com>
-- Vladimir Dronnikov <dronnikov@gmail.com>
--
--
-- Example:
-- Example:
--
for k, v in pairs(
require("ds18b20").read(4
)
) do print(k, v) end
-- require("ds18b20").read(4
, function(r) for k, v in pairs(r
) do print(k, v) end
end)
------------------------------------------------------------------------------
------------------------------------------------------------------------------
local
M
local
M
do
do
local
bit
=
bit
local
format_addr
=
function
(
a
)
local
format_addr
=
function
(
a
)
return
(
"%02x-%02x%02x%02x%02x%02x%02x"
):
format
(
return
(
"%02x-%02x%02x%02x%02x%02x%02x"
):
format
(
a
:
byte
(
1
),
a
:
byte
(
1
),
...
@@ -16,7 +17,7 @@ do
...
@@ -16,7 +17,7 @@ do
a
:
byte
(
4
),
a
:
byte
(
3
),
a
:
byte
(
2
)
a
:
byte
(
4
),
a
:
byte
(
3
),
a
:
byte
(
2
)
)
)
end
end
local
read
=
function
(
pin
,
delay
)
local
read
=
function
(
pin
,
cb
,
delay
)
local
ow
=
require
(
"ow"
)
local
ow
=
require
(
"ow"
)
-- get list of relevant devices
-- get list of relevant devices
local
d
=
{
}
local
d
=
{
}
...
@@ -37,25 +38,32 @@ do
...
@@ -37,25 +38,32 @@ do
ow
.
skip
(
pin
)
ow
.
skip
(
pin
)
ow
.
write
(
pin
,
0x44
,
1
)
ow
.
write
(
pin
,
0x44
,
1
)
-- wait a bit
-- wait a bit
tmr
.
delay
(
delay
or
100
000
)
tmr
.
alarm
(
0
,
delay
or
100
,
0
,
function
(
)
-- iterate over devices
-- iterate over devices
local
r
=
{
}
local
r
=
{
}
for
i
=
1
,
#
d
do
for
i
=
1
,
#
d
do
tmr
.
wdclr
()
tmr
.
wdclr
()
-- read rom command
-- read rom command
ow
.
reset
(
pin
)
ow
.
reset
(
pin
)
ow
.
select
(
pin
,
d
[
i
])
ow
.
select
(
pin
,
d
[
i
])
ow
.
write
(
pin
,
0xBE
,
1
)
ow
.
write
(
pin
,
0xBE
,
1
)
-- read data
-- read data
local
x
=
ow
.
read_bytes
(
pin
,
9
)
local
x
=
ow
.
read_bytes
(
pin
,
9
)
if
ow
.
crc8
(
x
)
==
0
then
if
ow
.
crc8
(
x
)
==
0
then
local
t
=
(
x
:
byte
(
1
)
+
x
:
byte
(
2
)
*
256
)
*
625
local
t
=
(
x
:
byte
(
1
)
+
x
:
byte
(
2
)
*
256
)
-- NB: temperature in Celcius * 10^4
-- negatives?
r
[
format_addr
(
d
[
i
])]
=
t
if
bit
.
isset
(
t
,
15
)
then
t
=
1
-
bit
.
bxor
(
t
,
0xffff
)
end
d
[
i
]
=
nil
-- NB: temperature in Celsius * 10^4
t
=
t
*
625
-- NB: due 850000 means bad pullup. ignore
if
t
~=
850000
then
r
[
format_addr
(
d
[
i
])]
=
t
end
d
[
i
]
=
nil
end
end
end
end
cb
(
r
)
return
r
end
)
end
end
-- expose
-- expose
M
=
{
M
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment