HM Yoong / DS1820

Dependents:   BMS_BMUCore_Max_DummyData

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(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's 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     
00110     /** RAM is a copy of the internal DS1820's RAM
00111       * It's updated during the read_RAM() command
00112       * which is automaticaly called from any function
00113       * using the RAM values.
00114       */
00115     char RAM[9];
00116     
00117     /* This function copies the DS1820's RAM into the object's
00118      * RAM[].
00119      */
00120     void read_RAM();
00121 
00122     /** This routine initializes the global variables used in
00123       * the search_ROM() and search_alarm() funtions. It should
00124       * be called once before looping to find devices.
00125       */
00126     void search_ROM_setup();
00127 
00128     /** This routine will search for an unidentified device
00129       * on the bus. It uses the variables in search_ROM_setup
00130       * to remember the pervious ROM address found.
00131       * It will return FALSE if there were no new devices
00132       * discovered on the bus.
00133       */
00134     bool search_ROM();
00135 
00136     /** This routine will search for an unidentified device
00137       * which has the temperature alarm bit set. It uses the 
00138       * variables in search_ROM_setup to remember the pervious 
00139       * ROM address found. It will return FALSE if there were 
00140       * no new devices with alarms discovered on the bus.
00141       */
00142     bool search_alarm();
00143 
00144     /** This routine will read the ROM (Family code, serial number
00145       * and Checksum) from a dedicated device on the bus.
00146       *
00147       * NOTE: This command can only be used when there is only one 
00148       *       DS1820 on the bus. If this command is used when there 
00149       *       is more than one slave present on the bus, a data 
00150       *       collision will occur when all the DS1820s attempt to 
00151       *       respond at the same time.
00152       */
00153     void read_ROM();
00154 
00155     /** This routine will initiate the temperature conversion within
00156       * a DS1820. There is a built in 750ms delay to allow the 
00157       * conversion to complete.
00158       *
00159       * To update all probes on the bus, use a statement such as this:
00160       * probe[0]->convert_temperature(DS1820::all_devices);
00161       *
00162       * @param allows the fnction to apply to a specific device or
00163       * to all devices on the 1-Wire bus.
00164       */
00165     void convert_temperature(devices device=this_device);
00166 
00167     /** This function will return the probe temperature. This function
00168       * uses the count remainding values to interpolate the temperature
00169       * to about 1/150th of a degree. Whereas the probe is not spec to
00170       * that precision. It does seem to give a smooth reading to the
00171       * tenth of a degree.
00172       *
00173       * @param scale, may be either 'c' or 'f'
00174       * @returns temperature for that scale
00175       */
00176     float temperature(char scale='c');
00177     
00178     /** This function calculates the ROM checksum and compares it to the
00179       * CRC value stored in ROM[7].
00180       *
00181       * @returns true if the checksum matches, otherwise false.
00182       */
00183     bool ROM_checksum_error();
00184 
00185     /** This function calculates the RAM checksum and compares it to the
00186       * CRC value stored in RAM[8].
00187       *
00188       * @returns true if the checksum matches, otherwise false.
00189       */
00190     bool RAM_checksum_error();
00191 
00192     /** This funtion returns the values stored in the temperature
00193       * alarm registers. 
00194       *
00195       * @returns a 16 bit integer of TH (upper byte) and TL (lower byte).
00196       */
00197     int read_scratchpad();
00198     
00199     /** This function will store the passed data into the DS1820's RAM.
00200       * Note: It does NOT save the data to the EEPROM for retention
00201       * during cycling the power off and on.
00202       *
00203       * @param a 16 bit integer of TH (upper byte) and TL (lower byte).
00204       */
00205     void write_scratchpad(int data);
00206     
00207     /** This function will transfer the TH and TL registers from the
00208       * DS1820's RAM into the EEPROM.
00209       * Note: There is a built in 10ms delay to allow for the
00210       * completion of the EEPROM write cycle.
00211       *
00212       * @param allows the fnction to apply to a specific device or
00213       * to all devices on the 1-Wire bus.
00214       */
00215     void store_scratchpad(devices device=this_device);
00216 
00217     /** This function will copy the stored values from the EEPROM
00218       * into the DS1820's RAM locations for TH and TL.
00219       *
00220       * @param allows the fnction to apply to a specific device or
00221       * to all devices on the 1-Wire bus.
00222       */
00223     int recall_scratchpad(devices device=this_device);
00224 
00225     /** This function will return the type of power supply for
00226       * a specific device. It can also be used to query all devices
00227       * looking for any device that is parasite powered.
00228       *
00229       * @returns true if the device (or all devices) are Vcc powered,
00230       * returns false if the device (or ANY device) is parasite powered.
00231       */
00232     bool read_power_supply(devices device=this_device);
00233 
00234 private:
00235     bool _parasite_power;
00236     char CRC_byte (char CRC, char byte );
00237     bool onewire_reset();
00238     void match_ROM();
00239     void skip_ROM();
00240     bool search_ROM_routine(char command);
00241     void onewire_bit_out (bool bit_data);
00242     void onewire_byte_out(char data);
00243     bool onewire_bit_in();
00244     char onewire_byte_in();
00245 
00246 protected:
00247     DigitalInOut _datapin;
00248     DigitalOut _parasitepin;
00249 };
00250 
00251 
00252 #endif