First Publish

Dependencies:   BridgeDriver2 FrontPanelButtons MAX31855 MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Mon Nov 10 22:59:49 2014 +0000
Revision:
1:9954bf6d7d25
Parent:
0:20e78c9d2ea9
First Commit

Who changed what in which revision?

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