Diltech RM Consultant / Mbed OS Double_I2C_Sample

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18B20.cpp Source File

DS18B20.cpp

00001 /*  MIT License
00002 *
00003 *   Copyright (c) 2020 Lukas Gessner
00004 *
00005 *   Permission is hereby granted, free of charge, to any person obtaining a copy
00006 *   of this software and associated documentation files (the "Software"), to deal
00007 *   in the Software without restriction, including without limitation the rights
00008 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 *   copies of the Software, and to permit persons to whom the Software is
00010 *   furnished to do so, subject to the following conditions:
00011 *
00012 *   The above copyright notice and this permission notice shall be included in all
00013 *   copies or substantial portions of the Software.
00014 *
00015 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021 *   SOFTWARE.
00022 */
00023 
00024 #include "DS18B20.h"
00025 
00026 OWI::OWI(PinName pin):owi_io(pin)
00027 {
00028     
00029 }
00030 
00031 void OWI::write0()
00032 {
00033      owi_io.output();  // timeslot = 70us
00034      owi_io.write(0);  // owi = 0;
00035      wait_us(60);
00036      owi_io.input();
00037      wait_us(10);       
00038 }
00039 
00040 void OWI::write1()    // timeslot = 70us
00041 {
00042      owi_io.output();
00043      owi_io.write(0);  // owi = 0;
00044      wait_us(6);
00045      owi_io.input();
00046      wait_us(64);   
00047 }
00048 
00049 unsigned char OWI::readBit()
00050 {
00051      unsigned char readbit;
00052      
00053      owi_io.output();
00054      owi_io.write(0);  // owi = 0;
00055      wait_us(6);
00056      owi_io.input();
00057      wait_us(9);
00058      readbit = owi_io.read();
00059      wait_us(55);
00060      
00061      return readbit;
00062 }
00063 
00064 unsigned char OWI::detectPresence()
00065 {
00066      core_util_critical_section_enter();
00067      
00068      unsigned char presencebit;
00069      
00070      owi_io.output();
00071      owi_io.write(0);  // owi = 0;
00072      wait_us(480);
00073      owi_io.input();
00074      wait_us(70);
00075      presencebit = !owi_io.read();
00076      wait_us(410);
00077      
00078      core_util_critical_section_exit();
00079      
00080      return presencebit;
00081 }
00082 
00083 void OWI::sendByte(unsigned char data)
00084 {
00085      core_util_critical_section_enter();
00086      
00087      unsigned char temp;
00088      unsigned char i;
00089      for (i = 0; i < 8; i++)
00090      {
00091          temp = data & 0x01;
00092          if (temp)
00093          {
00094               this->write1();
00095          }
00096          else
00097          {
00098               this->write0();
00099          } 
00100          data >>= 1;
00101      }
00102      
00103      core_util_critical_section_exit();
00104 }
00105 
00106 unsigned char OWI::receiveByte()
00107 {
00108      core_util_critical_section_enter();
00109      
00110      unsigned char data;
00111      unsigned char i;
00112      data = 0x00;
00113      for (i = 0; i < 8; i++)
00114      {
00115          data >>= 1;
00116          if (this->readBit())
00117          {
00118                data |= 0x80;
00119          }
00120      }
00121      
00122      core_util_critical_section_exit();
00123      return data;
00124 }
00125 
00126 
00127 DS18B20::DS18B20(PinName pin):DS18B20_OWI(pin)
00128 {
00129 
00130 }
00131 
00132 float DS18B20::readTemp()
00133 {
00134     unsigned char msb,lsb;
00135     int itemp;
00136     float ftemp;
00137     
00138     DS18B20_OWI.detectPresence();
00139     DS18B20_OWI.sendByte(OWI_SKIP_ROM);
00140     DS18B20_OWI.sendByte(DS18B20_START);
00141     
00142     #if MBED_MAJOR_VERSION >= 6
00143         ThisThread::sleep_for(750ms);
00144     #elif MBED_MAJOR_VERSION < 5
00145         wait(0.75);
00146     #else
00147         ThisThread::sleep_for(750);
00148     #endif
00149         
00150     DS18B20_OWI.detectPresence();
00151     DS18B20_OWI.sendByte(OWI_SKIP_ROM);
00152     DS18B20_OWI.sendByte(DS18B20_READ_SCRATCH_PAD);
00153     lsb = DS18B20_OWI.receiveByte();
00154     msb = DS18B20_OWI.receiveByte();
00155         
00156     itemp = (msb<<8)+lsb;
00157     ftemp = (float)itemp / 16.0f;      
00158     
00159     return ftemp;
00160 }
00161 
00162 
00163