Library for reading temperature from DS1820, DS18B20 and DS1822

Dependencies:   LinkedList

Dependents:   heatmap BLE_Temperature BLE_Temperature_Exercise F334andDS18B20 ... more

Fork of DS1820 by David Pairman

HelloWorld: http://mbed.org/users/Sissors/code/DS1820_HelloWorld/

Library should currently work on all mbed targets, let me know if there is an issue. First however make sure you have latest version of mbed library and this library.

Committer:
Sissors
Date:
Wed Apr 02 19:10:34 2014 +0000
Revision:
6:abfdd851218a
Parent:
5:2cd4928e8147
Child:
7:58b61681818f
set default convert temperature to all devices

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michael_ 2:ee820a991b95 1 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
Michael_ 2:ee820a991b95 2 * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
Michael_ 2:ee820a991b95 3 *
Michael_ 2:ee820a991b95 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Michael_ 2:ee820a991b95 5 * of this software and associated documentation files (the "Software"), to deal
Michael_ 2:ee820a991b95 6 * in the Software without restriction, including without limitation the rights
Michael_ 2:ee820a991b95 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michael_ 2:ee820a991b95 8 * copies of the Software, and to permit persons to whom the Software is
Michael_ 2:ee820a991b95 9 * furnished to do so, subject to the following conditions:
Michael_ 2:ee820a991b95 10 *
Michael_ 2:ee820a991b95 11 * The above copyright notice and this permission notice shall be included in
Michael_ 2:ee820a991b95 12 * all copies or substantial portions of the Software.
Michael_ 2:ee820a991b95 13 *
Michael_ 2:ee820a991b95 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michael_ 2:ee820a991b95 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michael_ 2:ee820a991b95 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michael_ 2:ee820a991b95 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michael_ 2:ee820a991b95 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Michael_ 2:ee820a991b95 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michael_ 2:ee820a991b95 20 * THE SOFTWARE.
Michael_ 2:ee820a991b95 21 */
Michael_ 2:ee820a991b95 22
Michael_ 2:ee820a991b95 23 #ifndef MBED_DS1820_H
Michael_ 2:ee820a991b95 24 #define MBED_DS1820_H
Michael_ 2:ee820a991b95 25
Michael_ 2:ee820a991b95 26 #include "mbed.h"
Sissors 5:2cd4928e8147 27 #include "LinkedList.h"
Michael_ 2:ee820a991b95 28
Sissors 5:2cd4928e8147 29 #define FAMILY_CODE _ROM[0]
Sissors 5:2cd4928e8147 30 #define FAMILY_CODE_DS1820 0x10
Sissors 5:2cd4928e8147 31 #define FAMILY_CODE_DS18B20 0x28
Sissors 5:2cd4928e8147 32 #define FAMILY_CODE_DS1822 0x22
Michael_ 2:ee820a991b95 33
Michael_ 2:ee820a991b95 34 /** DS1820 Dallas 1-Wire Temperature Probe
Michael_ 2:ee820a991b95 35 *
Michael_ 2:ee820a991b95 36 * Example:
Michael_ 2:ee820a991b95 37 * @code
Michael_ 2:ee820a991b95 38 * #include "mbed.h"
Michael_ 2:ee820a991b95 39 * #include "DS1820.h"
Michael_ 2:ee820a991b95 40 *
Sissors 5:2cd4928e8147 41 * DS1820 probe(DATA_PIN);
Sissors 5:2cd4928e8147 42 *
Michael_ 2:ee820a991b95 43 * int main() {
Sissors 5:2cd4928e8147 44 * while(1) {
Sissors 5:2cd4928e8147 45 * probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
Sissors 5:2cd4928e8147 46 * printf("It is %3.1foC\r\n", probe.temperature());
Sissors 5:2cd4928e8147 47 * wait(1);
Michael_ 2:ee820a991b95 48 * }
Michael_ 2:ee820a991b95 49 * }
Michael_ 2:ee820a991b95 50 * @endcode
Michael_ 2:ee820a991b95 51 */
Michael_ 2:ee820a991b95 52 class DS1820 {
Michael_ 2:ee820a991b95 53 public:
Michael_ 2:ee820a991b95 54 enum devices{
Michael_ 2:ee820a991b95 55 this_device, // command applies to only this device
Michael_ 2:ee820a991b95 56 all_devices }; // command applies to all devices
Michael_ 2:ee820a991b95 57
Michael_ 2:ee820a991b95 58 /** Create a probe object connected to the specified pins
Sissors 5:2cd4928e8147 59 *
Sissors 5:2cd4928e8147 60 * The probe might either by regular powered or parasite powered. If it is parasite
Sissors 5:2cd4928e8147 61 * powered and power_pin is set, that pin will be used to switch an external mosfet connecting
Sissors 5:2cd4928e8147 62 * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin
Sissors 5:2cd4928e8147 63 * is used to supply extra power when required. This will be sufficient as long as the
Sissors 5:2cd4928e8147 64 * number of probes is limitted.
Michael_ 2:ee820a991b95 65 *
Michael_ 2:ee820a991b95 66 * @param data_pin DigitalInOut pin for the data bus
Sissors 5:2cd4928e8147 67 * @param power_pin DigitalOut (optional) pin to control the power MOSFET
Sissors 5:2cd4928e8147 68 * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high)
Michael_ 2:ee820a991b95 69 */
Sissors 5:2cd4928e8147 70 DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin
Sissors 5:2cd4928e8147 71 ~DS1820();
Michael_ 2:ee820a991b95 72
Sissors 5:2cd4928e8147 73 /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object
Sissors 5:2cd4928e8147 74 *
Sissors 5:2cd4928e8147 75 * @return - true if there are one or more unassigned devices, otherwise false
Michael_ 2:ee820a991b95 76 */
Sissors 5:2cd4928e8147 77 static bool unassignedProbe(PinName pin);
Michael_ 2:ee820a991b95 78
Michael_ 2:ee820a991b95 79 /** This routine will initiate the temperature conversion within
Sissors 5:2cd4928e8147 80 * one or all DS1820 probes.
Michael_ 2:ee820a991b95 81 *
pairmand 3:8f2b7f4940b5 82 * @param wait if true or parisitic power is used, waits up to 750 ms for
pairmand 3:8f2b7f4940b5 83 * conversion otherwise returns immediatly.
pairmand 3:8f2b7f4940b5 84 * @param device allows the function to apply to a specific device or
Michael_ 2:ee820a991b95 85 * to all devices on the 1-Wire bus.
pairmand 3:8f2b7f4940b5 86 * @returns milliseconds untill conversion will complete.
Michael_ 2:ee820a991b95 87 */
Sissors 6:abfdd851218a 88 int convertTemperature(bool wait, devices device=all_devices);
Michael_ 2:ee820a991b95 89
pairmand 3:8f2b7f4940b5 90 /** This function will return the probe temperature. Approximately 10ms per
pairmand 3:8f2b7f4940b5 91 * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
Michael_ 2:ee820a991b95 92 *
Michael_ 2:ee820a991b95 93 * @param scale, may be either 'c' or 'f'
pairmand 3:8f2b7f4940b5 94 * @returns temperature for that scale, or -1000.0 if CRC error detected.
Michael_ 2:ee820a991b95 95 */
Michael_ 2:ee820a991b95 96 float temperature(char scale='c');
Michael_ 2:ee820a991b95 97
pairmand 3:8f2b7f4940b5 98 /** This function sets the temperature resolution for the DS18B20
pairmand 3:8f2b7f4940b5 99 * in the configuration register.
pairmand 3:8f2b7f4940b5 100 *
pairmand 3:8f2b7f4940b5 101 * @param a number between 9 and 12 to specify resolution
pairmand 3:8f2b7f4940b5 102 * @returns true if successful
pairmand 3:8f2b7f4940b5 103 */
Sissors 5:2cd4928e8147 104 bool setResolution(unsigned int resolution);
Michael_ 2:ee820a991b95 105
Michael_ 2:ee820a991b95 106 private:
Michael_ 2:ee820a991b95 107 bool _parasite_power;
Sissors 5:2cd4928e8147 108 bool _power_mosfet;
Sissors 5:2cd4928e8147 109 bool _power_polarity;
Sissors 5:2cd4928e8147 110
Sissors 5:2cd4928e8147 111 static char CRC_byte (char CRC, char byte );
Sissors 5:2cd4928e8147 112 static bool onewire_reset(DigitalInOut *pin);
Michael_ 2:ee820a991b95 113 void match_ROM();
Michael_ 2:ee820a991b95 114 void skip_ROM();
Sissors 5:2cd4928e8147 115 static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address);
Sissors 5:2cd4928e8147 116 static void onewire_bit_out (DigitalInOut *pin, bool bit_data);
Michael_ 2:ee820a991b95 117 void onewire_byte_out(char data);
Sissors 5:2cd4928e8147 118 static bool onewire_bit_in(DigitalInOut *pin);
Michael_ 2:ee820a991b95 119 char onewire_byte_in();
Sissors 5:2cd4928e8147 120 static bool ROM_checksum_error(char *_ROM_address);
Sissors 5:2cd4928e8147 121 bool RAM_checksum_error();
Sissors 5:2cd4928e8147 122 void read_RAM();
Sissors 5:2cd4928e8147 123 static bool unassignedProbe(DigitalInOut *pin, char *ROM_address);
Sissors 5:2cd4928e8147 124 void write_scratchpad(int data);
Sissors 5:2cd4928e8147 125 bool read_power_supply(devices device=this_device);
Michael_ 2:ee820a991b95 126
Michael_ 2:ee820a991b95 127 DigitalInOut _datapin;
Michael_ 2:ee820a991b95 128 DigitalOut _parasitepin;
Sissors 5:2cd4928e8147 129
Sissors 5:2cd4928e8147 130 char _ROM[8];
Sissors 5:2cd4928e8147 131 char RAM[9];
Sissors 5:2cd4928e8147 132
Sissors 5:2cd4928e8147 133 static LinkedList<node> probes;
Michael_ 2:ee820a991b95 134 };
Michael_ 2:ee820a991b95 135
Michael_ 2:ee820a991b95 136
Michael_ 2:ee820a991b95 137 #endif