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.
Dependents: MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more
DS18B20.cpp
00001 /******************************************************************//** 00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 **********************************************************************/ 00032 00033 00034 #include "Slaves/Sensors/DS18B20/DS18B20.h" 00035 #include "wait_api.h" 00036 00037 00038 using namespace OneWire; 00039 using namespace OneWire::crc; 00040 00041 00042 enum DS18B20_CMDS 00043 { 00044 WRITE_SCRATCHPAD = 0x4E, 00045 READ_SCRATCHPAD = 0xBE, 00046 COPY_SCRATCHPAD = 0x48, 00047 CONV_TEMPERATURE = 0x44, 00048 READ_POWER_SUPPY = 0xB4, 00049 RECALL = 0xB8 00050 }; 00051 00052 00053 /**********************************************************************/ 00054 DS18B20::DS18B20(RandomAccessRomIterator &selector):OneWireSlave(selector) 00055 { 00056 } 00057 00058 00059 /**********************************************************************/ 00060 OneWireSlave::CmdResult DS18B20::writeScratchPad(uint8_t th, uint8_t tl, Resolution res) 00061 { 00062 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00063 00064 OneWireMaster::CmdResult owmResult = selectDevice(); 00065 00066 if (owmResult == OneWireMaster::Success) 00067 { 00068 uint8_t sendBlock[] = {WRITE_SCRATCHPAD, th, tl, res}; 00069 00070 owmResult = master().OWWriteBlock(sendBlock, 4); 00071 if (owmResult == OneWireMaster::Success) 00072 { 00073 deviceResult = OneWireSlave::Success; 00074 } 00075 else 00076 { 00077 deviceResult = OneWireSlave::CommunicationError; 00078 } 00079 } 00080 00081 return deviceResult; 00082 } 00083 00084 00085 /**********************************************************************/ 00086 OneWireSlave::CmdResult DS18B20::readScratchPad(uint8_t * scratchPadBuff) 00087 { 00088 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00089 00090 OneWireMaster::CmdResult owmResult = selectDevice(); 00091 00092 if (owmResult == OneWireMaster::Success) 00093 { 00094 owmResult = master().OWWriteByteSetLevel(READ_SCRATCHPAD, OneWireMaster::NormalLevel); 00095 if (owmResult == OneWireMaster::Success) 00096 { 00097 uint8_t rxBlock[9]; 00098 owmResult = master().OWReadBlock(rxBlock, 9); 00099 00100 uint8_t crcCheck = calculateCrc8(rxBlock, 8); 00101 if ((owmResult == OneWireMaster::Success) && (crcCheck == rxBlock[8])) 00102 { 00103 std::memcpy(scratchPadBuff, rxBlock, 8); 00104 deviceResult = OneWireSlave::Success; 00105 } 00106 else 00107 { 00108 deviceResult = OneWireSlave::CommunicationError; 00109 } 00110 } 00111 else 00112 { 00113 deviceResult = OneWireSlave::CommunicationError; 00114 } 00115 } 00116 00117 return deviceResult; 00118 } 00119 00120 00121 /**********************************************************************/ 00122 OneWireSlave::CmdResult DS18B20::readPowerSupply(bool & localPower) 00123 { 00124 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00125 00126 OneWireMaster::CmdResult owmResult = selectDevice(); 00127 if (owmResult == OneWireMaster::Success) 00128 { 00129 owmResult = master().OWWriteByteSetLevel(READ_POWER_SUPPY, OneWireMaster::NormalLevel); 00130 if(owmResult == OneWireMaster::Success) 00131 { 00132 uint8_t rtnBit = 0; 00133 00134 owmResult = master().OWTouchBitSetLevel(rtnBit, OneWireMaster::NormalLevel); 00135 if(owmResult == OneWireMaster::Success) 00136 { 00137 localPower = (rtnBit & 0x01); 00138 deviceResult = OneWireSlave::Success; 00139 } 00140 else 00141 { 00142 deviceResult = OneWireSlave::CommunicationError; 00143 } 00144 } 00145 else 00146 { 00147 deviceResult = OneWireSlave::CommunicationError; 00148 } 00149 } 00150 00151 return deviceResult; 00152 } 00153 00154 /**********************************************************************/ 00155 OneWireSlave::CmdResult DS18B20::copyScratchPad( void ) 00156 { 00157 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00158 00159 bool hasLocalPower = false; 00160 deviceResult = this->readPowerSupply(hasLocalPower); 00161 00162 if(deviceResult == OneWireSlave::Success) 00163 { 00164 OneWireMaster::CmdResult owmResult = selectDevice(); 00165 00166 if (owmResult == OneWireMaster::Success) 00167 { 00168 if(hasLocalPower) 00169 { 00170 owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::NormalLevel); 00171 if (owmResult == OneWireMaster::Success) 00172 { 00173 uint8_t recvbit = 0; 00174 do 00175 { 00176 owmResult = master().OWTouchBitSetLevel(recvbit, OneWireMaster::NormalLevel); 00177 } 00178 while((!recvbit) && (owmResult == OneWireMaster::Success)); 00179 00180 if ((owmResult == OneWireMaster::Success) && (recvbit & 1)) 00181 { 00182 deviceResult = OneWireSlave::Success; 00183 } 00184 else 00185 { 00186 deviceResult = OneWireSlave::TimeoutError; 00187 } 00188 } 00189 else 00190 { 00191 deviceResult = OneWireSlave::CommunicationError; 00192 } 00193 } 00194 else 00195 { 00196 owmResult = master().OWWriteByteSetLevel(COPY_SCRATCHPAD, OneWireMaster::StrongLevel); 00197 if (owmResult == OneWireMaster::Success) 00198 { 00199 wait_ms(10); 00200 00201 owmResult = master().OWSetLevel(OneWireMaster::NormalLevel); 00202 if (owmResult == OneWireMaster::Success) 00203 { 00204 deviceResult = OneWireSlave::Success; 00205 } 00206 else 00207 { 00208 deviceResult = OneWireSlave::CommunicationError; 00209 } 00210 } 00211 else 00212 { 00213 deviceResult = OneWireSlave::CommunicationError; 00214 } 00215 } 00216 } 00217 else 00218 { 00219 deviceResult = OneWireSlave::OperationFailure; 00220 } 00221 } 00222 00223 return deviceResult; 00224 } 00225 00226 /**********************************************************************/ 00227 OneWireSlave::CmdResult DS18B20::convertTemperature(float & temp) 00228 { 00229 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00230 00231 bool hasLocalPower = false; 00232 deviceResult = this->readPowerSupply(hasLocalPower); 00233 00234 uint8_t scratchPadBuff[8]; 00235 00236 if (deviceResult == OneWireSlave::Success) 00237 { 00238 OneWireMaster::CmdResult owmResult = selectDevice(); 00239 if(owmResult == OneWireMaster::Success) 00240 { 00241 if(hasLocalPower) 00242 { 00243 owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::NormalLevel); 00244 if (owmResult == OneWireMaster::Success) 00245 { 00246 uint8_t recvbit = 0; 00247 do 00248 { 00249 owmResult = master().OWTouchBitSetLevel(recvbit, OneWireMaster::NormalLevel); 00250 } 00251 while((owmResult == OneWireMaster::Success) && (!recvbit)); 00252 00253 if((owmResult == OneWireMaster::Success) && (recvbit & 1)) 00254 { 00255 deviceResult = this->readScratchPad(scratchPadBuff); 00256 } 00257 else 00258 { 00259 deviceResult = OneWireSlave::TimeoutError; 00260 } 00261 } 00262 else 00263 { 00264 deviceResult = OneWireSlave::CommunicationError; 00265 } 00266 } 00267 else 00268 { 00269 owmResult = master().OWWriteByteSetLevel(CONV_TEMPERATURE, OneWireMaster::StrongLevel); 00270 if (owmResult == OneWireMaster::Success) 00271 { 00272 wait_ms(750); 00273 00274 owmResult = master().OWSetLevel(OneWireMaster::NormalLevel); 00275 if (owmResult == OneWireMaster::Success) 00276 { 00277 deviceResult = this->readScratchPad(scratchPadBuff); 00278 } 00279 else 00280 { 00281 deviceResult = OneWireSlave::CommunicationError; 00282 } 00283 } 00284 else 00285 { 00286 deviceResult = OneWireSlave::CommunicationError; 00287 } 00288 } 00289 } 00290 else 00291 { 00292 deviceResult = OneWireSlave::OperationFailure; 00293 } 00294 } 00295 00296 if(deviceResult == OneWireSlave::Success) 00297 { 00298 int16_t intTemp = ((scratchPadBuff[1] << 8) | scratchPadBuff[0]); 00299 00300 switch(scratchPadBuff[4]) 00301 { 00302 case DS18B20::NineBit: 00303 temp = (intTemp * 0.5F); 00304 break; 00305 00306 case DS18B20::TenBit: 00307 temp = (intTemp * 0.25F); 00308 break; 00309 00310 case DS18B20::ElevenBit: 00311 temp = (intTemp * 0.125F); 00312 break; 00313 00314 case DS18B20::TwelveBit: 00315 temp = (intTemp * 0.0625F); 00316 break; 00317 00318 default: 00319 deviceResult = OneWireSlave::OperationFailure; 00320 break; 00321 } 00322 } 00323 00324 return deviceResult; 00325 } 00326 00327 00328 /**********************************************************************/ 00329 OneWireSlave::CmdResult DS18B20::recallEEPROM( void ) 00330 { 00331 OneWireSlave::CmdResult deviceResult = OneWireSlave::OperationFailure; 00332 00333 OneWireMaster::CmdResult owmResult = selectDevice(); 00334 00335 if (owmResult == OneWireMaster::Success) 00336 { 00337 uint8_t cmd = RECALL; 00338 00339 owmResult = master().OWWriteBlock(&cmd, 1); 00340 if (owmResult == OneWireMaster::Success) 00341 { 00342 deviceResult = OneWireSlave::Success; 00343 } 00344 else 00345 { 00346 deviceResult = OneWireSlave::CommunicationError; 00347 } 00348 } 00349 00350 return deviceResult; 00351 }
Generated on Tue Jul 12 2022 15:46:20 by
