Arduino-OWI
One-Wire Interface (OWI) library for Arduino
DS18B20.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "OWI.h"
3 #include "Software/OWI.h"
4 #include "Driver/DS18B20.h"
5 #include "assert.h"
6 
8 DS18B20 sensor(owi);
9 
10 void setup()
11 {
12  Serial.begin(57600);
13  while (!Serial);
14 }
15 
16 void loop()
17 {
18  // Broadcast a convert request to all thermometer sensors
19  // Print list of sensors, rom code, and temperature
20 
21  if (!sensor.convert_request(true)) return;
23 
24  int8_t last = owi.FIRST;
25  uint8_t* rom = sensor.rom();
26  int id = 0;
27  do {
28  // Search for the next digital thermometer
29  last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
30  if (last == owi.ERROR) break;
31 
32  // Read the scratchpad with current temperature, tiggers, etc
33  sensor.read_scratchpad(false);
34 
35  // Print sequence number
36  Serial.print(id++);
37 
38  // Print family code
39  Serial.print(F(":family="));
40  Serial.print(rom[0], HEX);
41 
42  // Print serial number
43  Serial.print(F(",sn="));
44  size_t i = 1;
45  do {
46  if (rom[i] < 0x10) Serial.print(0);
47  Serial.print(rom[i], HEX);
48  i += 1;
49  } while (i < owi.ROM_MAX - 1);
50 
51  // Print cyclic redundancy check sum
52  Serial.print(F(",crc="));
53  if (rom[i] < 0x10) Serial.print(0);
54  Serial.print(rom[i], HEX);
55 
56  // Print conversion resolution
57  Serial.print(F(",resolution="));
58  Serial.print(sensor.resolution());
59 
60  // Print alarm trigger threshols
61  int8_t low, high;
62  sensor.get_trigger(low, high);
63  Serial.print(F(",trigger="));
64  Serial.print(low);
65  Serial.print(F(".."));
66  Serial.print(high);
67 
68  // And temperature
69  Serial.print(F(",temperature="));
70  Serial.println(sensor.temperature());
71  } while (last != owi.LAST);
72 
73  Serial.println();
74  delay(2000);
75 }
static const uint8_t FAMILY_CODE
Definition: DS18B20.h:44
Error during search.
Definition: OWI.h:192
Software::Serial< BOARD::D0 > Serial
Definition: DS18B20.ino:8
Software::OWI< BOARD::D1 > owi
Definition: DS18B20.ino:9
void get_trigger(int8_t &low, int8_t &high) const
Definition: DS18B20.h:113
bool read_scratchpad(bool match=true)
Definition: DS18B20.h:185
int8_t search_rom(uint8_t family, uint8_t *code, int8_t last=FIRST)
Definition: OWI.h:204
bool convert_request(bool broadcast=false)
Definition: DS18B20.h:125
void resolution(uint8_t bits)
Definition: DS18B20.h:70
DS18B20 sensor(owi)
void rom(const uint8_t *rom)
Definition: OWI.h:303
bool convert_await()
Definition: DS18B20.h:170
Last position, search completed.
Definition: OWI.h:193
Start position of search.
Definition: OWI.h:191
static const size_t ROM_MAX
Definition: OWI.h:32
float temperature() const
Definition: DS18B20.h:94
void loop()
Definition: DS18B20.ino:18
void setup()
Definition: DS18B20.ino:12