libpruw1
0.4
One wire (W1) driver for Beaglebone hardware based on libpruio
onlyDS18S20.bas
Go to the documentation of this file.
1
19
20
#INCLUDE ONCE
"BBB/pruio.bi"
' header for mandatroy libpruio
21
#INCLUDE ONCE
"BBB/pruio_pins.bi"
' libruio pin numbering
22
'#INCLUDE ONCE "BBB/pruw1.bi" ' library header (after `make install`)
23
#INCLUDE ONCE
"../bas/pruw1.bi"
' library header
24
25
VAR
io
=
NEW
PruIo()
26
DO
27
IF
io
->Errr
THEN ?
"io CTOR failed ("
& *
io
->Errr &
")"
:
EXIT DO
28
29
' uncomment the following lines in order to use function PruW1::getIn()
30
'IF io->config() THEN _
31
'PRINT "libpruio config failed (" & *.Errr & ")" : exit do
32
33
34
VAR
w1
=
NEW
PruW1
(
io
, P9_15)
35
DO
:
WITH
*
w1
36
IF
.Errr
THEN
_
37
?
"w1 CTOR failed ("
& *.Errr &
"/"
& *
io
->Errr &
")"
:
EXIT DO
38
' Scan the bus for device IDs
39
?
"trying to scan bus ..."
40
IF
.scanBus()
THEN PRINT
"scanBus failed ("
& *.Errr &
")"
:
EXIT DO
41
?
' print them out
42
FOR
i
AS INTEGER
= 0
TO UBOUND
(.Slots)
' output slot# and sensor IDs
43
?
"found device "
& i &
", ID: "
&
HEX
(.Slots(i), 16)
44
NEXT
45
46
VAR
res
=
CAST
(
UBYTE PTR
, @.DRam[4])
47
' Perform some measurements
48
FOR
i
AS INTEGER
= 0
TO 10
' output 11 blocks of data
49
' Start measurement, send the presense pulse (0 = OK).
50
IF
.resetBus()
THEN ?
"no devices"
:
EXIT DO
51
.sendByte(&hCC)
' SKIP_ROM command -> broadcast message
52
.sendByte(&h44)
' convert T command -> all sensors triggered
53
SLEEP 750
:
?
' wait for conversation
54
' Fetch the data from sensor scratch pads
55
FOR
s
AS INTEGER
= 0
TO UBOUND
(.Slots)
56
IF PEEK
(
UBYTE
, @.Slots(s)) <> &h10
THEN CONTINUE FOR
' series 10 only
57
58
' Start reading, send the presense pulse (0 = OK).
59
IF
.resetBus()
THEN ?
"no devices"
:
EXIT FOR
60
.sendByte(&h55)
' ROM_MATCH command -> adress single sensor
61
.sendRom(.Slots(s))
' send sensor ID -> select sensor
62
.sendByte(&hBE)
'READ_SCRATCH command -> sensor sends scratchpad
63
.recvBlock(9)
' read data block (64 bit scratchpad and CRC byte)
64
65
' output result
66
VAR
crc
= .calcCrc(9)
67
?
"sensor "
&
HEX
(.Slots(s), 16) &
" --> CRC "
;
68
69
IF
crc
THEN ?
"error!"
_
70
ELSE ?
"OK: "
& T_fam10(
res
) / 256 &
" °C"
71
NEXT
72
NEXT
73
END WITH
:
LOOP UNTIL 1
74
DELETE
w1
' destroy w1 UDT
75
LOOP UNTIL 1
76
DELETE
io
' destroy libpruio UDT
77
libpruw1
src
examples
onlyDS18S20.bas
Generated on Mon Aug 21 2023 22:06:14 for libpruw1 by
1.9.1