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