David Pairman / DS1820

Fork of DS1820 by Michael Hagberg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.h Source File

DS1820.h

00001 /* mbed DS1820 Library, for the Dallas (Maxim) 1-Wire Digital Thermometer
00002  * Copyright (c) 2010, Michael Hagberg Michael@RedBoxCode.com
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef MBED_DS1820_H
00024 #define MBED_DS1820_H
00025 
00026 #include "mbed.h"
00027 
00028 // ****** THIS GLOBAL VARIABLES MUST BE DEFINED IN main.cpp
00029 
00030 // Global variables shared between all DS1820 objects
00031 //bool DS1820_done_flag;
00032 //int  DS1820_last_descrepancy;
00033 //char DS1820_search_ROM[8];
00034 
00035 /** DS1820 Dallas 1-Wire Temperature Probe
00036  *
00037  * Example:
00038  * @code
00039  * #include "mbed.h"
00040  *
00041  * #include "TextLCD.h"
00042  * #include "DS1820.h"
00043  *
00044  * TextLCD lcd(p25, p26, p21, p22, p23, p24, TextLCD::LCD16x2); // rs, e, d0-d3, layout
00045  *
00046  * const int MAX_PROBES = 16;
00047  * DS1820* probe[MAX_PROBES];
00048  *
00049  * int main() {
00050  *     int i;
00051  *     int devices_found=0;
00052  *     // Initialize the probe array to DS1820 objects
00053  *     for (i = 0; i < MAX_PROBES; i++)
00054  *         probe[i] = new DS1820(p27);
00055  *     // Initialize global state variables
00056  *     probe[0]->search_ROM_setup();
00057  *     // Loop to find all devices on the data line
00058  *     while (probe[devices_found]->search_ROM() and devices_found<MAX_PROBES-1)
00059  *         devices_found++;
00060  *     // If maximum number of probes are found, 
00061  *     // bump the counter to include the last array entry
00062  *     if (probe[devices_found]->ROM[0] != 0xFF)
00063  *         devices_found++;
00064  * 
00065  *     lcd.cls();
00066  *     if (devices_found==0)
00067  *         lcd.printf("No devices found");
00068  *     else {
00069  *         while (true) {
00070  *             probe[0]->convert_temperature(true, DS1820::all_devices);
00071  *             lcd.cls();
00072  *             for (i=0; i<devices_found; i++) {
00073  *                 lcd.printf("%3.1f ",probe[i]->temperature('f'));
00074  *             }
00075  *         }
00076  *     }
00077  * }
00078  * @endcode
00079  */
00080  
00081 class DS1820 {
00082 public:
00083     enum devices{
00084         this_device,     // command applies to only this device
00085         all_devices };   // command applies to all devices
00086 
00087     /** Create a probe object connected to the specified pins
00088      *
00089      * @param data_pin DigitalInOut pin for the data bus
00090      * @param power_pin DigitalOut pin to control the power MOSFET
00091      */
00092     DS1820(PinName data_pin, PinName power_pin); // Constructor with parasite power pin
00093 
00094     /** Create a probe object connected to the specified pin
00095      *  this is used when all probes are externally powered
00096      *
00097      * @param data_pin DigitalInOut pin for the data bus
00098      */
00099     DS1820(PinName data_pin);
00100 
00101     /** ROM is a copy of the internal DS1820's ROM
00102       * It is created during the search_ROM() or search_alarm() commands
00103       *
00104       * ROM[0] is the Dallas Family Code
00105       * ROM[1] thru ROM[6] is the 48-bit unique serial number
00106       * ROM[7] is the device CRC
00107       */
00108     char ROM[8];
00109     #define FAMILY_CODE ROM[0]
00110     #define FAMILY_CODE_DS1820 0x10
00111     #define FAMILY_CODE_DS18S20 0x10
00112     #define FAMILY_CODE_DS18B20 0x28
00113     
00114     /** RAM is a copy of the internal DS1820's RAM
00115       * It's updated during the read_RAM() command
00116       * which is automaticaly called from any function
00117       * using the RAM values except RAM_checksum_error.
00118       */
00119     char RAM[9];
00120     
00121     /** This function copies the DS1820's RAM into the object's
00122       * RAM[]. It is automaticaly called by temperature(), 
00123       * read_scratchpad(), and recall_scratchpad() of a single probe.
00124       */
00125     void read_RAM();
00126 
00127     /** This routine initializes the global variables used in
00128       * the search_ROM() and search_alarm() funtions. It should
00129       * be called once before looping to find devices.
00130       */
00131     void search_ROM_setup();
00132 
00133     /** This routine will search for an unidentified device
00134       * on the bus. It uses the variables in search_ROM_setup
00135       * to remember the pervious ROM address found.
00136       * It will return FALSE if there were no new devices
00137       * discovered on the bus.
00138       */
00139     bool search_ROM();
00140 
00141     /** This routine will search for an unidentified device
00142       * which has the temperature alarm bit set. It uses the 
00143       * variables in search_ROM_setup to remember the pervious 
00144       * ROM address found. It will return FALSE if there were 
00145       * no new devices with alarms discovered on the bus.
00146       */
00147     bool search_alarm();
00148 
00149     /** This routine will read the ROM (Family code, serial number
00150       * and Checksum) from a dedicated device on the bus.
00151       *
00152       * NOTE: This command can only be used when there is only one 
00153       *       DS1820 on the bus. If this command is used when there 
00154       *       is more than one slave present on the bus, a data 
00155       *       collision will occur when all the DS1820s attempt to 
00156       *       respond at the same time.
00157       */
00158     void read_ROM();
00159 
00160     /** This routine will initiate the temperature conversion within
00161       * one or all DS1820 probes. There is an optional built in delay 
00162       * (up to 750ms) to allow the conversion to complete.
00163       *
00164       * To update all probes on the bus, use a statement such as this:
00165       * probe[0]->convert_temperature(true, DS1820::all_devices);
00166       *
00167       * @param wait if true or parisitic power is used, waits up to 750 ms for 
00168       * conversion otherwise returns immediatly.
00169       * @param device allows the function to apply to a specific device or
00170       * to all devices on the 1-Wire bus.
00171       * @returns milliseconds untill conversion will complete.
00172       */
00173     int convert_temperature(bool wait, devices device=this_device);
00174 
00175     /** This function will return the probe temperature. Approximately 10ms per
00176       * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
00177       * This function uses the count remainding values to interpolate the temperature
00178       * to about 1/150th of a degree. Whereas the probe is not spec to
00179       * that precision. It does seem to give a smooth reading to the
00180       * tenth of a degree.
00181       *
00182       * @param scale, may be either 'c' or 'f'
00183       * @returns temperature for that scale, or -1000.0 if CRC error detected.
00184       */
00185     float temperature(char scale='c');
00186     
00187     /** This function calculates the ROM checksum and compares it to the
00188       * CRC value stored in ROM[7].
00189       *
00190       * @returns true if the checksums mis-match, otherwise false.
00191       */
00192     bool ROM_checksum_error();
00193 
00194     /** This function calculates the RAM checksum and compares it to the
00195       * CRC value stored in RAM[8]. Approx 10 us on LPC1768.
00196       *
00197       * @returns true if the checksums mis-matche, otherwise false.
00198       */
00199     bool RAM_checksum_error();
00200 
00201     /** This function sets the temperature resolution for the DS18B20
00202       * in the configuration register.
00203       *
00204       * @param a number between 9 and 12 to specify resolution
00205       * @returns true if successful
00206       */ 
00207     bool set_configuration_bits(unsigned int resolution);
00208     
00209     /** This function returns the values stored in the temperature
00210       * alarm registers. 
00211       *
00212       * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
00213       */
00214     int read_scratchpad();
00215     
00216     /** This function will store the passed data into the DS1820's RAM.
00217       * Note: It does NOT save the data to the EEPROM for retention
00218       * during cycling the power off and on.
00219       *
00220       * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
00221       */ 
00222     void write_scratchpad(int data);
00223     
00224     /** This function will transfer the TH and TL registers from the
00225       * DS1820's RAM into the EEPROM.
00226       * Note: There is a built in 10ms delay to allow for the
00227       * completion of the EEPROM write cycle.
00228       *
00229       * @param allows the fnction to apply to a specific device or
00230       * to all devices on the 1-Wire bus.
00231       */ 
00232     void store_scratchpad(devices device=this_device);
00233 
00234     /** This function will copy the stored values from the EEPROM
00235       * into the DS1820's RAM locations for TH and TL.
00236       *
00237       * @param allows the function to apply to a specific device or
00238       * to all devices on the 1-Wire bus.
00239       */
00240     int recall_scratchpad(devices device=this_device);
00241 
00242     /** This function will return the type of power supply for
00243       * a specific device. It can also be used to query all devices
00244       * looking for any device that is parasite powered.
00245       *
00246       * @returns true if the device (or all devices) are Vcc powered,
00247       * returns false if the device (or ANY device) is parasite powered.
00248       */
00249     bool read_power_supply(devices device=this_device);
00250 
00251 private:
00252     bool _parasite_power;
00253     char CRC_byte (char CRC, char byte );
00254     bool onewire_reset();
00255     void match_ROM();
00256     void skip_ROM();
00257     bool search_ROM_routine(char command);
00258     void onewire_bit_out (bool bit_data);
00259     void onewire_byte_out(char data);
00260     bool onewire_bit_in();
00261     char onewire_byte_in();
00262 
00263 protected:
00264     DigitalInOut _datapin;
00265     DigitalOut _parasitepin;
00266 };
00267 
00268 
00269 #endif