A copy of very incomplete program for forum

Dependencies:   mbed SDFileSystem

Committer:
roselea
Date:
Sat Mar 17 14:15:20 2012 +0000
Revision:
0:bfcb5b67b1d6

        

Who changed what in which revision?

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