User | Revision | Line number | New contents of line |
gbeardall |
0:2f78e3dca55d
|
1
|
/*
|
gbeardall |
0:2f78e3dca55d
|
2
|
* DS18B20. Maxim DS18B20 One-Wire Thermometer.
|
gbeardall |
0:2f78e3dca55d
|
3
|
* Uses the OneWireCRC library.
|
gbeardall |
0:2f78e3dca55d
|
4
|
*
|
gbeardall |
0:2f78e3dca55d
|
5
|
* Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
|
gbeardall |
0:2f78e3dca55d
|
6
|
*
|
gbeardall |
0:2f78e3dca55d
|
7
|
* This file is part of OneWireThermometer.
|
gbeardall |
0:2f78e3dca55d
|
8
|
*
|
gbeardall |
0:2f78e3dca55d
|
9
|
* OneWireThermometer is free software: you can redistribute it and/or modify
|
gbeardall |
0:2f78e3dca55d
|
10
|
* it under the terms of the GNU General Public License as published by
|
gbeardall |
0:2f78e3dca55d
|
11
|
* the Free Software Foundation, either version 3 of the License, or
|
gbeardall |
0:2f78e3dca55d
|
12
|
* (at your option) any later version.
|
gbeardall |
0:2f78e3dca55d
|
13
|
*
|
gbeardall |
0:2f78e3dca55d
|
14
|
* OneWireThermometer is distributed in the hope that it will be useful,
|
gbeardall |
0:2f78e3dca55d
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
gbeardall |
0:2f78e3dca55d
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
gbeardall |
0:2f78e3dca55d
|
17
|
* GNU General Public License for more details.
|
gbeardall |
0:2f78e3dca55d
|
18
|
*
|
gbeardall |
0:2f78e3dca55d
|
19
|
* You should have received a copy of the GNU General Public License
|
gbeardall |
0:2f78e3dca55d
|
20
|
* along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>.
|
gbeardall |
0:2f78e3dca55d
|
21
|
*/
|
gbeardall |
0:2f78e3dca55d
|
22
|
|
gbeardall |
0:2f78e3dca55d
|
23
|
#include "DS18B20.h"
|
gbeardall |
0:2f78e3dca55d
|
24
|
#include "DebugTrace.h"
|
gbeardall |
0:2f78e3dca55d
|
25
|
|
gbeardall |
0:2f78e3dca55d
|
26
|
DebugTrace pc_ds18B20(ON, TO_SERIAL);
|
gbeardall |
0:2f78e3dca55d
|
27
|
|
gbeardall |
0:2f78e3dca55d
|
28
|
DS18B20::DS18B20(bool crcOn, bool useAddr, bool parasitic, PinName pin) :
|
gbeardall |
0:2f78e3dca55d
|
29
|
OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18B20_ID)
|
gbeardall |
0:2f78e3dca55d
|
30
|
{
|
gbeardall |
0:2f78e3dca55d
|
31
|
}
|
gbeardall |
0:2f78e3dca55d
|
32
|
|
gbeardall |
0:2f78e3dca55d
|
33
|
void DS18B20::setResolution(eResolution resln)
|
gbeardall |
0:2f78e3dca55d
|
34
|
{
|
gbeardall |
0:2f78e3dca55d
|
35
|
// as the write to the configuration register involves a write to the
|
gbeardall |
0:2f78e3dca55d
|
36
|
// high and low alarm bytes, need to read these registers first
|
gbeardall |
0:2f78e3dca55d
|
37
|
// and copy them back on the write
|
gbeardall |
0:2f78e3dca55d
|
38
|
|
gbeardall |
0:2f78e3dca55d
|
39
|
BYTE read_data[THERMOM_SCRATCHPAD_SIZE];
|
gbeardall |
0:2f78e3dca55d
|
40
|
BYTE write_data[ALARM_CONFIG_SIZE];
|
gbeardall |
0:2f78e3dca55d
|
41
|
|
gbeardall |
0:2f78e3dca55d
|
42
|
if (readAndValidateData(read_data))
|
gbeardall |
0:2f78e3dca55d
|
43
|
{
|
gbeardall |
0:2f78e3dca55d
|
44
|
// copy alarm and config data to write data
|
gbeardall |
0:2f78e3dca55d
|
45
|
for (int k = 2; k < 5; k++)
|
gbeardall |
0:2f78e3dca55d
|
46
|
{
|
gbeardall |
0:2f78e3dca55d
|
47
|
write_data[k - 2] = read_data[k];
|
gbeardall |
0:2f78e3dca55d
|
48
|
}
|
gbeardall |
0:2f78e3dca55d
|
49
|
int config = write_data[2];
|
gbeardall |
0:2f78e3dca55d
|
50
|
config &= 0x9F;
|
gbeardall |
0:2f78e3dca55d
|
51
|
config ^= (resln << 5);
|
gbeardall |
0:2f78e3dca55d
|
52
|
write_data[2] = config;
|
gbeardall |
0:2f78e3dca55d
|
53
|
|
gbeardall |
0:2f78e3dca55d
|
54
|
resetAndAddress();
|
gbeardall |
0:2f78e3dca55d
|
55
|
oneWire.writeByte(WRITESCRATCH);
|
gbeardall |
0:2f78e3dca55d
|
56
|
for (int k = 0; k < 3; k++)
|
gbeardall |
0:2f78e3dca55d
|
57
|
{
|
gbeardall |
0:2f78e3dca55d
|
58
|
oneWire.writeByte(write_data[k]);
|
gbeardall |
0:2f78e3dca55d
|
59
|
}
|
gbeardall |
0:2f78e3dca55d
|
60
|
|
gbeardall |
0:2f78e3dca55d
|
61
|
// remember it so we can use the correct delay in reading the temperature
|
gbeardall |
0:2f78e3dca55d
|
62
|
// for parasitic power
|
gbeardall |
0:2f78e3dca55d
|
63
|
resolution = resln;
|
gbeardall |
0:2f78e3dca55d
|
64
|
}
|
gbeardall |
0:2f78e3dca55d
|
65
|
}
|
gbeardall |
0:2f78e3dca55d
|
66
|
|
gbeardall |
0:2f78e3dca55d
|
67
|
float DS18B20::calculateTemperature(BYTE* data)
|
gbeardall |
0:2f78e3dca55d
|
68
|
{
|
gbeardall |
0:2f78e3dca55d
|
69
|
bool signBit = false;
|
gbeardall |
0:2f78e3dca55d
|
70
|
if (data[TEMPERATURE_MSB] & 0x80) signBit = true;
|
gbeardall |
0:2f78e3dca55d
|
71
|
|
gbeardall |
0:2f78e3dca55d
|
72
|
int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB];
|
gbeardall |
0:2f78e3dca55d
|
73
|
if (signBit)
|
gbeardall |
0:2f78e3dca55d
|
74
|
{
|
gbeardall |
0:2f78e3dca55d
|
75
|
read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement
|
gbeardall |
0:2f78e3dca55d
|
76
|
read_temp *= -1;
|
gbeardall |
0:2f78e3dca55d
|
77
|
}
|
gbeardall |
0:2f78e3dca55d
|
78
|
|
gbeardall |
0:2f78e3dca55d
|
79
|
int resolution = (data[CONFIG_REG_BYTE] & 0x60) >> 5; // mask off bits 6,5 and move to 1,0
|
gbeardall |
0:2f78e3dca55d
|
80
|
switch (resolution)
|
gbeardall |
0:2f78e3dca55d
|
81
|
{
|
gbeardall |
0:2f78e3dca55d
|
82
|
case nineBit: // 0.5 deg C increments
|
gbeardall |
0:2f78e3dca55d
|
83
|
read_temp &= 0xFFF8; // bits 2,1,0 are undefined
|
gbeardall |
0:2f78e3dca55d
|
84
|
pc_ds18B20.traceOut("9 bit resolution ...\r\n");
|
gbeardall |
0:2f78e3dca55d
|
85
|
break;
|
gbeardall |
0:2f78e3dca55d
|
86
|
case tenBit: // 0.25 deg C increments
|
gbeardall |
0:2f78e3dca55d
|
87
|
read_temp &= 0xFFFC; // bits 1,0 are undefined
|
gbeardall |
0:2f78e3dca55d
|
88
|
pc_ds18B20.traceOut("10 bit resolution ...\r\n");
|
gbeardall |
0:2f78e3dca55d
|
89
|
break;
|
gbeardall |
0:2f78e3dca55d
|
90
|
case elevenBit: // 0.125 deg C increments
|
gbeardall |
0:2f78e3dca55d
|
91
|
read_temp &= 0xFFFE; // bit 0 is undefined
|
gbeardall |
0:2f78e3dca55d
|
92
|
pc_ds18B20.traceOut("11 bit resolution ...\r\n");
|
gbeardall |
0:2f78e3dca55d
|
93
|
break;
|
gbeardall |
0:2f78e3dca55d
|
94
|
case twelveBit: // 0.0625 deg C increments
|
gbeardall |
0:2f78e3dca55d
|
95
|
pc_ds18B20.traceOut("12 bit resolution ...\r\n");
|
gbeardall |
0:2f78e3dca55d
|
96
|
break;
|
gbeardall |
0:2f78e3dca55d
|
97
|
}
|
gbeardall |
0:2f78e3dca55d
|
98
|
float realTemp = (float)read_temp/16 ;
|
gbeardall |
0:2f78e3dca55d
|
99
|
|
gbeardall |
0:2f78e3dca55d
|
100
|
pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp);
|
gbeardall |
0:2f78e3dca55d
|
101
|
|
gbeardall |
0:2f78e3dca55d
|
102
|
return realTemp;
|
gbeardall |
0:2f78e3dca55d
|
103
|
} |