libpruw1  0.4
One wire (W1) driver for Beaglebone hardware based on libpruio
onlyDS18S20.c
Go to the documentation of this file.
1 
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "libpruio/pruio.h"
23 #include "libpruio/pruio_pins.h" // libruio pin numbering
24 #include "../c_include/pruw1.h" // library header (local)
25 //#include "libpruw1/pruw1.h" // library header when installed
26 
28 int main(int argc, char **argv)
29 {
30  int i, n;
31  UInt8 crc, typ, *res;
32  float val;
33  char *txt;
34  UInt64 id;
35  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
36  do {
37  if (io->Errr) {
38  printf("libpruio CTOR failed (%s)\n", io->Errr); break;}
39  // uncomment the following lines in order to use function PruW1::getIn()
40  //if (io->config()) {
41  //printf("libpruio config failed (%s)\n", io->Errr); break;}
42 
43  // Create new libpruw1 instance.
44  pruw1 *w1 = pruw1_new(io, P9_15, PRUW1_PULLUP);
45  if (w1->Errr) {
46  printf("libpruw1 CTOR failed (%s)\n", w1->Errr); break;}
47  // Scan the bus for device IDs
48  printf("\ntrying to scan devices ...");
49  if (pruw1_scanBus(w1)) {
50  printf("scanBus failed (%s)\n", w1->Errr); break;}
51  for (i = 0; i <= pruw1_getSlotMax(w1); i++)
52  {
53  printf("\nfound device %d, ID: %llX", i, pruw1_getId(w1, i));
54  }
55  res = (UInt8 *) w1->DRam + 16;
56  printf("\n");
57  // Perform some measurements
58  for (n = 0; n <= 10; n++) {
59  // Start measurement, send the presense pulse (0 = OK).
60  if (pruw1_resetBus(w1)) { printf("no devices"); break;}
61  pruw1_sendByte(w1, 0xCC); // SKIP_ROM command -> broadcast message
62  pruw1_sendByte(w1, 0x44); //convert T command -> all sensors triggered
63  usleep(750000);
64 
65  // Fetch the data from sensor scratch pads
66  for (i = 0; i <= pruw1_getSlotMax(w1); i++) {
67  id = pruw1_getId(w1, i);
68 
69  typ = id & 0xFF;
70  if (typ != 0x10) continue;
71 
72  // Start reading, send the presense pulse (0 = OK).
73  if (pruw1_resetBus(w1)) { printf("no devices"); break;}
74  pruw1_sendByte(w1, 0x55); // ROM_MATCH command -> adress single sensor
75  pruw1_sendRom(w1, id); // send sensor ID -> select sensor
76  pruw1_sendByte(w1, 0xBE); //READ_SCRATCH command -> sensor sends scratchpad
77  pruw1_recvBlock(w1, 9); // read data block (64 bit scratchpad and CRC byte)
78 
79  // Output result
80  crc = pruw1_calcCrc(w1, 9);
81  if (crc) {
82  txt = "error";
83  val = -99;
84  } else {
85  txt = "OK:";
86  val = (float) T_FAM10(res) / 256;
87  }
88  printf("\nsensor %llX --> %s %3.4f °C", id, txt, val);
89  }
90  printf("\n");
91  }
93  } while (0);
94 
95 /* we're done */
96 
97  pruio_destroy(io); /* destroy driver structure */
98  return 0;
99 }
100 
101 
102 //DO
103  //' Perform some measurements
104  //FOR i AS INTEGER = 0 TO 10 ' output 11 blocks of data
105  //FOR s AS INTEGER = 0 TO UBOUND(.Slots)
106  //SELECT CASE AS CONST PEEK(UBYTE, @.Slots(s)) ' check type
107  //CASE &h10, &h20, &h22, &h28, &h3B, &h42 ' a Dallas sensor type
108  //CASE ELSE /' no dallas sensor -> skip '/ : CONTINUE FOR
109  //END SELECT
110 
111 
112  //NEXT
113  //NEXT
114  //END WITH : LOOP UNTIL 1
115  //DELETE w1 ' destroy w1 UDT
116 //LOOP UNTIL 1
117 //DELETE io ' destroy libpruio UDT
VAR res
pointer to measurement data
Definition: dallas.bas:51
VAR io
Pointer to libpruio instance.
Definition: dallas.bas:25
VAR crc
The checksum (0 = OK).
Definition: dallas.bas:74
VAR w1
Create new libpruw1 instance.
Definition: dallas.bas:34
int main(int argc, char **argv)
The main function.
Definition: onlyDS18S20.c:28
@ PRUW1_PULLUP
Activate internal pull up resistor (default)
Definition: pruw1.bi:30
Int16 T_FAM10(UInt8 *Rom)
Compute the temperature for a series 10 sensor (old format).
unsigned char UInt8
8 bit unsigned integer data type.
Definition: pruw1.h:22
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_recvBlock(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 N)
Wrapper function for PruW1::recvBlock().
SUB_CDECL_ALIAS pruw1_sendRom(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt64 V)
Wrapper function for PruW1::sendRom().
FUNCTION_CDECL_ALIAS_AS_UInt64 pruw1_getId(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt32 N)
Function to get ID from the array PruW1::Slots from C.
SUB_CDECL_ALIAS pruw1_destroy(BYVAL_AS_PruW1_PTR W1)
Wrapper function for destructor PruW1::~PruW1.
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_resetBus(BYVAL_AS_PruW1_PTR W1)
Wrapper function for PruW1::resetBus().
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_calcCrc(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 N)
Wrapper function for PruW1::calcCrc().
SUB_CDECL_ALIAS pruw1_sendByte(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 V)
Wrapper function for PruW1::sendByte().
FUNCTION_CDECL_ALIAS_AS_Int32 pruw1_getSlotMax(BYVAL_AS_PruW1_PTR W1)
Property to get size of array PruW1::Slots from C.
FUNCTION_CDECL_ALIAS_AS_PruW1_PTR pruw1_new(BYVAL_AS_PruIo_PTR P, BYVAL_AS_Uint8 B, BYVAL_AS_Uint8 M)
Wrapper function for constructor PruW1::PruW1().
The PruW1 C wrapper structure.
Definition: pruw1.h:85