libpruw1  0.4
One wire (W1) driver for Beaglebone hardware based on libpruio
dallas.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, PRUW1_PARPOW + PRUW1_PULLUP) ' 2 wire (VDD=GND)
35  'VAR w1 = NEW PruW1(io, P9_15) ' external VDD and pullup
36 
37  DO : WITH *w1
38  IF .Errr THEN _
39  ?"w1 CTOR failed (" & *.Errr & "/" & *.Errr & ")" : EXIT DO
40  ' Scan the bus for parasite powered devices
41  ?*iif(.checkPara(), @"At least one", @"No") _
42  & " parasite powered device."
43  ' Scan the bus for device IDs
44  ?"trying to scan bus ..."
45  IF .scanBus() THEN PRINT"scanBus failed (" & *.Errr & ")" : EXIT DO
46  ? ' print them out
47  FOR i AS INTEGER = 0 TO UBOUND(.Slots) ' output slot# and sensor IDs
48  ?"found device " & i & ", ID: " & HEX(.Slots(i), 16)
49  NEXT
50 
51  VAR res = CAST(UBYTE PTR, @.DRam[4])
52  ' Perform some measurements
53  FOR i AS INTEGER = 0 TO 10 ' output 11 blocks of data
54  ' Start measurement, send the presense pulse (0 = OK).
55  IF .resetBus() THEN ?"no devices" : EXIT DO
56  .sendByte(&hCC) ' SKIP_ROM command -> broadcast message
57  .sendByte(&h44) ' convert T command -> all sensors triggered
58  SLEEP 750 : ? ' wait for conversation
59  ' Fetch the data from sensor scratch pads
60  FOR s AS INTEGER = 0 TO UBOUND(.Slots)
61  SELECT CASE AS CONST PEEK(UBYTE, @.Slots(s)) ' check type
62  CASE &h10, &h20, &h22, &h28, &h3B, &h42 ' a Dallas sensor type
63  CASE ELSE /' no dallas sensor -> skip '/ : CONTINUE FOR
64  END SELECT
65 
66  ' Start reading, send the presense pulse (0 = OK).
67  IF .resetBus() THEN ?"no devices" : EXIT FOR
68  .sendByte(&h55) ' ROM_MATCH command -> adress single sensor
69  .sendRom(.Slots(s)) ' send sensor ID -> select sensor
70  .sendByte(&hBE) 'READ_SCRATCH command -> sensor sends scratchpad
71  .recvBlock(9) ' read data block (64 bit scratchpad and CRC byte)
72 
73  ' output result
74  VAR crc = .calcCrc(9)
75  ?"sensor " & HEX(.Slots(s), 16) & " --> CRC ";
76  IF crc THEN ?"error!" _
77  ELSE ?"OK: " _
78  & IIF(PEEK(UBYTE, @.Slots(s)) = &h10 _
79  , T_fam10(res) _ ' old format
80  , T_fam20(res) _ ' new format
81  ) / 256 _
82  & " °C"
83  NEXT
84  NEXT
85  END WITH : LOOP UNTIL 1
86  DELETE w1 ' destroy w1 UDT
87 LOOP UNTIL 1
88 DELETE io ' destroy libpruio UDT
89