Program to read the temperature from multiple DS18B20 sensors on the same pin. The original library/code came from Petras Saduikis (see http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56), but I've modified it to remember the address of multiple sensors all connected to the same data pin. My sample program displays the temperature of each device in turn. If you want to see more of what's going on behind the scenes turn on the debug by setting DebugTrace pc(OFF, TO_SERIAL); to ON (instead of OFF) in the couple of places it's used - it will log the device addresses as they are found etc. The addresses are set in the devices at the factory - I don't think they can be changed, the search always seems to find them in the same order, but this won't be anything to do with the way you've plugged them in. I've had a play with up to 7 sensors (the code has a limit of 10 hardwired in it, but this would be easy to change)

Dependencies:   mbed

Committer:
tonymudd
Date:
Sun Jul 17 15:56:49 2011 +0000
Revision:
0:fb8b6da96a8b

        

Who changed what in which revision?

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