Wim De Roeve / DS18B20

Dependents:   DallasTemperature project1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20.cpp Source File

DS18B20.cpp

00001 /*
00002 * DS18B20. Maxim DS18B20 One-Wire Thermometer.
00003 * Uses the OneWireCRC library.
00004 *
00005 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
00006 *
00007 * This file is part of OneWireThermometer.
00008 *
00009 * OneWireThermometer is free software: you can redistribute it and/or modify
00010 * it under the terms of the GNU General Public License as published by
00011 * the Free Software Foundation, either version 3 of the License, or
00012 * (at your option) any later version.
00013 *
00014 * OneWireThermometer is distributed in the hope that it will be useful,
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 * GNU General Public License for more details.
00018 *
00019 * You should have received a copy of the GNU General Public License
00020 * along with OneWireThermometer.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include "DS18B20.h"
00024 #include "DebugTrace.h"
00025 
00026 DebugTrace pc_ds18B20(OFF, TO_SERIAL);
00027 
00028 DS18B20::DS18B20(PinName pin,bool crcOn, bool useAddr, bool parasitic, unsigned char *Address) :
00029         OneWireThermometer(pin, crcOn, useAddr, parasitic, DS18B20_ID, Address) {
00030 }
00031 
00032 void DS18B20::setResolution(eResolution resln) {
00033     // as the write to the configuration register involves a write to the
00034     // high and low alarm bytes, need to read these registers first
00035     // and copy them back on the write
00036 
00037     BYTE read_data[THERMOM_SCRATCHPAD_SIZE];
00038     BYTE write_data[ALARM_CONFIG_SIZE];
00039 
00040     if (readAndValidateData(read_data)) {
00041         // copy alarm and config data to write data
00042         for (int k = 2; k < 5; k++) {
00043             write_data[k - 2] = read_data[k];
00044         }
00045         int config = write_data[2];
00046         config &= 0x9F;
00047         config ^= (resln << 5);
00048         write_data[2] = config;
00049 
00050         resetAndAddress();
00051         oneWire.writeByte(DS18X20_WRITESCRATCH);
00052         for (int k = 0; k < 3; k++) {
00053             oneWire.writeByte(write_data[k]);
00054         }
00055 
00056         // remember it so we can use the correct delay in reading the temperature
00057         // for parasitic power
00058         _resolution = resln;
00059     }
00060 }
00061 
00062 float DS18B20::calculateTemperature(BYTE* data) {
00063     bool signBit = false;
00064     if (data[TEMPERATURE_MSB] & 0x80) signBit = true;
00065 
00066     int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB];
00067     if (signBit) {
00068         read_temp = (read_temp ^ 0xFFFF) + 1;    // two's complement
00069         read_temp *= -1;
00070     }
00071 
00072     int resolution = (data[CONFIG_REG_BYTE] & 0x60) >> 5; // mask off bits 6,5 and move to 1,0
00073     switch (resolution) {
00074         case nineBit:    // 0.5 deg C increments
00075             read_temp &= 0xFFF8;                // bits 2,1,0 are undefined
00076             pc_ds18B20.traceOut("9 bit resolution ...\r\n");
00077             break;
00078         case tenBit:     // 0.25 deg C increments
00079             read_temp &= 0xFFFC;                // bits 1,0 are undefined
00080             pc_ds18B20.traceOut("10 bit resolution ...\r\n");
00081             break;
00082         case elevenBit:  // 0.125 deg C increments
00083             read_temp &= 0xFFFE;                // bit 0 is undefined
00084             pc_ds18B20.traceOut("11 bit resolution ...\r\n");
00085             break;
00086         case twelveBit:  // 0.0625 deg C increments
00087             pc_ds18B20.traceOut("12 bit resolution ...\r\n");
00088             break;
00089     }
00090     float realTemp = (float)read_temp/16 ;
00091 
00092     pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp);
00093 
00094     return realTemp;
00095 }