Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 enum { 00059 invalid_conversion = -1000 00060 }; 00061 00062 /** Create a probe object connected to the specified pins 00063 * 00064 * The probe might either by regular powered or parasite powered. If it is parasite 00065 * powered and power_pin is set, that pin will be used to switch an external mosfet connecting 00066 * data to Vdd. If it is parasite powered and the pin is not set, the regular data pin 00067 * is used to supply extra power when required. This will be sufficient as long as the 00068 * number of probes is limitted. 00069 * 00070 * @param data_pin DigitalInOut pin for the data bus 00071 * @param power_pin DigitalOut (optional) pin to control the power MOSFET 00072 * @param power_polarity bool (optional) which sets active state (0 for active low (default), 1 for active high) 00073 */ 00074 DS1820(PinName data_pin, PinName power_pin = NC, bool power_polarity = 0); // Constructor with parasite power pin 00075 ~DS1820(); 00076 00077 /** Function to see if there are DS1820 devices left on a pin which do not have a corresponding DS1820 object 00078 * 00079 * @return - true if there are one or more unassigned devices, otherwise false 00080 */ 00081 static bool unassignedProbe(PinName pin); 00082 00083 /** This routine will initiate the temperature conversion within 00084 * one or all DS1820 probes. 00085 * 00086 * @param wait if true or parisitic power is used, waits up to 750 ms for 00087 * conversion otherwise returns immediatly. 00088 * @param device allows the function to apply to a specific device or 00089 * to all devices on the 1-Wire bus. 00090 * @returns milliseconds untill conversion will complete. 00091 */ 00092 int convertTemperature(bool wait, devices device=all_devices); 00093 00094 /** This function will return the probe temperature. Approximately 10ms per 00095 * probe to read its RAM, do CRC check and convert temperature on the LPC1768. 00096 * 00097 * @param scale, may be either 'c' or 'f' 00098 * @returns temperature for that scale, or DS1820::invalid_conversion (-1000) if CRC error detected. 00099 */ 00100 float temperature(char scale='c'); 00101 00102 /** This function sets the temperature resolution for the DS18B20 00103 * in the configuration register. 00104 * 00105 * @param a number between 9 and 12 to specify resolution 00106 * @returns true if successful 00107 */ 00108 bool setResolution(unsigned int resolution); 00109 00110 private: 00111 bool _parasite_power; 00112 bool _power_mosfet; 00113 bool _power_polarity; 00114 00115 static char CRC_byte(char _CRC, char byte ); 00116 static bool onewire_reset(DigitalInOut *pin); 00117 void match_ROM(); 00118 void skip_ROM(); 00119 static bool search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address); 00120 static void onewire_bit_out (DigitalInOut *pin, bool bit_data); 00121 void onewire_byte_out(char data); 00122 static bool onewire_bit_in(DigitalInOut *pin); 00123 char onewire_byte_in(); 00124 static bool ROM_checksum_error(char *_ROM_address); 00125 bool RAM_checksum_error(); 00126 void read_RAM(); 00127 static bool unassignedProbe(DigitalInOut *pin, char *ROM_address); 00128 void write_scratchpad(int data); 00129 bool read_power_supply(devices device=this_device); 00130 00131 DigitalInOut _datapin; 00132 DigitalOut _parasitepin; 00133 00134 char _ROM[8]; 00135 char RAM[9]; 00136 00137 static LinkedList<node> probes; 00138 }; 00139 00140 00141 #endif
Generated on Wed Aug 17 2022 05:29:14 by
1.7.2