no chance

Dependencies:   LinkedList

Dependents:   10_ETH_SD_JPG

Fork of DS1820 by Erik -

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 #include "LinkedList.h"
00028 
00029 #define FAMILY_CODE _ROM[0]
00030 #define FAMILY_CODE_DS1820 0x10
00031 #define FAMILY_CODE_DS18B20 0x28
00032 #define FAMILY_CODE_DS1822  0x22
00033 
00034 /** DS1820 Dallas 1-Wire Temperature Probe
00035  *
00036  * Example:
00037  * @code
00038  * #include "mbed.h"
00039  * #include "DS1820.h"
00040  *
00041  * DS1820 probe(DATA_PIN);
00042  *  
00043  * int main() {
00044  *     while(1) {
00045  *         probe.convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
00046  *         printf("It is %3.1foC\r\n", probe.temperature());
00047  *         wait(1);
00048  *     }
00049  * }
00050  * @endcode
00051  */
00052 class DS1820 {
00053 public:
00054     enum devices{
00055         this_device,     // command applies to only this device
00056         all_devices };   // command applies to all devices
00057 
00058     /** Create a probe object connected to the specified pins
00059     *
00060     * The probe might either by regular powered or parasite powered. If it is parasite
00061     * powered and power_pin is set, that pin will be used to switch an external mosfet connecting
00062     * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin
00063     * is used to supply extra power when required. This will be sufficient as long as the 
00064     * number of probes is limitted.
00065      *
00066      * @param data_pin DigitalInOut pin for the data bus
00067      * @param power_pin DigitalOut (optional) pin to control the power MOSFET
00068      * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high)
00069      */
00070     DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin
00071     ~DS1820();
00072 
00073     /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object
00074     *
00075     * @return - true if there are one or more unassigned devices, otherwise false
00076       */
00077     static bool unassignedProbe(PinName pin);
00078 
00079     /** This routine will initiate the temperature conversion within
00080       * one or all DS1820 probes. 
00081       *
00082       * @param wait if true or parisitic power is used, waits up to 750 ms for 
00083       * conversion otherwise returns immediatly.
00084       * @param device allows the function to apply to a specific device or
00085       * to all devices on the 1-Wire bus.
00086       * @returns milliseconds untill conversion will complete.
00087       */
00088     int convertTemperature(bool wait, devices device=this_device);
00089 
00090     /** This function will return the probe temperature. Approximately 10ms per
00091       * probe to read its RAM, do CRC check and convert temperature on the LPC1768.
00092       *
00093       * @param scale, may be either 'c' or 'f'
00094       * @returns temperature for that scale, or -1000.0 if CRC error detected.
00095       */
00096     float temperature(char scale='c');
00097 
00098     /** This function sets the temperature resolution for the DS18B20
00099       * in the configuration register.
00100       *
00101       * @param a number between 9 and 12 to specify resolution
00102       * @returns true if successful
00103       */ 
00104     bool setResolution(unsigned int resolution);       
00105 
00106 private:
00107     bool _parasite_power;
00108     bool _power_mosfet;
00109     bool _power_polarity;
00110     
00111     static char CRC_byte (char CRC, char byte );
00112     static bool onewire_reset(DigitalInOut *pin);
00113     void match_ROM();
00114     void skip_ROM();
00115     static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address);
00116     static void onewire_bit_out (DigitalInOut *pin, bool bit_data);
00117     void onewire_byte_out(char data);
00118     static bool onewire_bit_in(DigitalInOut *pin);
00119     char onewire_byte_in();
00120     static bool ROM_checksum_error(char *_ROM_address);
00121     bool RAM_checksum_error();
00122     void read_RAM();
00123     static bool unassignedProbe(DigitalInOut *pin, char *ROM_address);
00124     void write_scratchpad(int data);
00125     bool read_power_supply(devices device=this_device);
00126 
00127     DigitalInOut _datapin;
00128     DigitalOut _parasitepin;
00129     
00130     char _ROM[8];
00131     char RAM[9];
00132     
00133     static LinkedList<node> probes;
00134 };
00135 
00136 
00137 #endif