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