Now supports DS18B20 and DS18S20 Maxim/Dallas one-wire thermometer devices. Also supports DS18S20 in 9, 10, 11, and 12 bit resolution modes. 'Use Address' mode now checks if the correct device type is present, and informs the user which device to use. Correct temperature conversion times now used in non-parasitic mode. The device should be placed at least 6 inches (15 cm) from the mbed board in order to accurately read ambient temperature.

Dependencies:   mbed

Committer:
snatch59
Date:
Sun Jan 03 11:57:31 2010 +0000
Revision:
0:01a6a40578c9

        

Who changed what in which revision?

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