lukeweston / artemis

Open-source (and open hardware) Arduino-based telemetry and instrumentation module for amateur rocketry

This URL has Read+Write access

artemis / DS18B20.cpp
100644 47 lines (37 sloc) 1.418 kb
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
#include "DS18B20.h"
 
#define ONE_WIRE_DEVICE_18B20 0x28
 
#define ONE_WIRE_COMMAND_READ_SCRATCHPAD 0xBE
#define ONE_WIRE_COMMAND_START_CONVERSION 0x44
#define ONE_WIRE_COMMAND_MATCH_ROM 0x55
#define ONE_WIRE_COMMAND_SKIP_ROM 0xCC
 
DS18B20::DS18B20(OneWire &oneWire) {
  oneWire_ = &oneWire;
  isSetUp_ = false;
}
 
void DS18B20::setup() {
  deviceFound_ = false;
  oneWire_->reset_search();
  while (!deviceFound_ && oneWire_->search(address_)) {
    // FIXME: CRC check the address
    if (address_[0] == ONE_WIRE_DEVICE_18B20) deviceFound_ = true;
  }
  
  isSetUp_ = true;
}
 
void DS18B20::startConversion() {
  if (!isSetUp_) setup();
  oneWire_->reset(); // time: 1 millisecond
  oneWire_->select(address_); // time: 5 milliseconds
  oneWire_->write(ONE_WIRE_COMMAND_START_CONVERSION, 1); // time: 1 millisecond
}
 
void DS18B20::read() {
  if (!isSetUp_) setup();
  oneWire_->reset(); // time: 1 millisecond
  oneWire_->select(address_); // time: 5 milliseconds
  oneWire_->write(ONE_WIRE_COMMAND_READ_SCRATCHPAD); // time: 1 millisecond
  
  for (int i = 0; i < 9; i++) data_[i] = oneWire_->read();
}
 
float DS18B20::temperature() {
  int sign = data_[1] & 0xF0 ? -1 : 1;
  int value = ((int)(data_[1] & 0x07) << 8) | data_[0];
  return ((float)(value * sign)) / 16.0;
}