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 "Software/Serial.h"
6 #include "assert.h"
7 
8 Software::Serial<BOARD::D0> Serial;
10 DS18B20 sensor(owi);
11 
12 void setup()
13 {
14  Serial.begin(57600);
15  while (!Serial);
16 }
17 
18 void loop()
19 {
20  // Broadcast a convert request to all thermometer sensors
21  // Print list of sensors, rom code, and temperature
22 
23  if (!sensor.convert_request(true)) return;
24  delay(sensor.conversion_time());
25 
26  int8_t last = owi.FIRST;
27  uint8_t* rom = sensor.rom();
28  int id = 0;
29  do {
30  // Search for the next digital thermometer
31  last = owi.search_rom(sensor.FAMILY_CODE, rom, last);
32  if (last == owi.ERROR) break;
33 
34  // Read the scratchpad with current temperature, tiggers, etc
35  ASSERT(sensor.read_scratchpad(false));
36  int8_t low, high;
37  sensor.get_trigger(low, high);
38 
39  // Print sequence number
40  Serial.print(id++);
41 
42  // Print family code
43  Serial.print(F(":family="));
44  Serial.print(rom[0], HEX);
45 
46  // Print serial number
47  Serial.print(F(",sn="));
48  size_t i = 1;
49  do {
50  if (rom[i] < 0x10) Serial.print(0);
51  Serial.print(rom[i], HEX);
52  i += 1;
53  } while (i < owi.ROM_MAX - 1);
54 
55  // Print cyclic redundancy check sum
56  Serial.print(F(",crc="));
57  if (rom[i] < 0x10) Serial.print(0);
58  Serial.print(rom[i], HEX);
59 
60  // Print conversion resolution
61  Serial.print(F(",resolution="));
62  Serial.print(sensor.resolution());
63 
64  // Print alarm trigger threshols
65  Serial.print(F(",trigger=["));
66  Serial.print(low);
67  Serial.print(F(".."));
68  Serial.print(high);
69 
70  // And temperature
71  Serial.print(F("],temperature="));
72  Serial.println(sensor.temperature());
73  } while (last != owi.LAST);
74 
75  Serial.println();
76  delay(4000);
77 }
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
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
uint16_t conversion_time()
Definition: DS18B20.h:143
void loop()
Definition: DS18B20.ino:18
void setup()
Definition: DS18B20.ino:12