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 "Slave/OWI.h"
4 
6 static const uint8_t FAMILY_CODE = 0x28;
7 
11 enum {
12  CONVERT_T = 0x44,
13  READ_SCRATCHPAD = 0xBE,
15  COPY_SCRATCHPAD = 0x48,
16  RECALL_E = 0xB8,
18 } __attribute__((packed));
19 
23 struct scratchpad_t {
24  int16_t temperature;
25  int8_t high_trigger;
26  int8_t low_trigger;
27  uint8_t configuration;
28  uint8_t reserved[3];
29 } __attribute__((packed));
30 
31 // Slave device one wire access; use random rom code
33 
34 // Scratchpad with temperature, triggers and configuration
36  0x0550,
37  75,
38  70,
39  0x3f,
40  { 0, 0, 0 }
41 };
42 
43 // Analog pin used for emulated temperature reading
44 const int pin = A0;
45 
46 // Use builtin led; active during conversion delay
47 GPIO<BOARD::D13> led;
48 
49 void setup()
50 {
51  led.output();
52 }
53 
54 // This sketch uses approx. 1500 bytes (Uno) of program storage space,
55 // and less than 40 bytes for global variables (random access memory)
56 void loop()
57 {
58  // Application could do something in the background before
59  // handling incoming commands
60  if (!owi.rom_command()) return;
61 
62  // DS18B20 emulation with analog read; mapping from 0..1023
63  // to -128.00..127.75 C, 10-bits resolution. Set alarm according
64  // to low and high thresholds
65  int16_t value;
66  switch (owi.read()) {
67  case CONVERT_T:
68  value = analogRead(pin) - 512;
69  scratchpad.temperature = (value << 2);
70  value >>= 2;
71  owi.alarm(value >= scratchpad.high_trigger ||
72  value <= scratchpad.low_trigger);
73  led.high();
74  break;
75  case READ_SCRATCHPAD:
76  owi.write(&scratchpad, sizeof(scratchpad));
77  led.low();
78  break;
79  case WRITE_SCRATCHPAD:
80  owi.read(&scratchpad.high_trigger, 3);
81  break;
82  case COPY_SCRATCHPAD:
83  case RECALL_E:
84  case READ_POWER_SUPPLY:
85  break;
86  }
87 }
Copy configuration register to EEPROM.
Definition: DS18B20.ino:15
uint8_t configuration
Configuration; resolution, alarm.
Definition: DS18B20.ino:27
Read scratchpad including crc byte.
Definition: DS18B20.ino:13
Definition: OWI.h:33
Software::OWI< BOARD::D1 > owi
Definition: DS18B20.ino:9
Write data to scratchpad.
Definition: DS18B20.ino:14
Signal power supply mode.
Definition: DS18B20.ino:17
GPIO< BOARD::D13 > led
Definition: DS18B20.ino:47
const int pin
Definition: DS18B20.ino:44
int8_t high_trigger
High temperature trigger.
Definition: DS18B20.ino:25
Initiate temperature conversion.
Definition: DS18B20.ino:12
static const uint8_t FAMILY_CODE
Definition: DS18B20.ino:6
int16_t temperature
Temperature reading (9-12 bits).
Definition: DS18B20.ino:24
uint8_t reserved[3]
Reserved.
Definition: DS18B20.ino:28
int8_t low_trigger
Low temperature trigger.
Definition: DS18B20.ino:26
void loop()
Definition: DS18B20.ino:18
Recall configuration data from EEPROM.
Definition: DS18B20.ino:16
scratchpad_t scratchpad
Definition: DS18B20.ino:35
void setup()
Definition: DS18B20.ino:12