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: EXAMPLE_Nucleo_mbed_RTOS_test_code
Fork of DS1620 by
DS1620.cpp
00001 // Library for the DS1620 digital thermometer 00002 // Copyright (C) <2015> Ryan Bancroft 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // For copy of the GNU General Public License 00015 // see <http://www.gnu.org/licenses/>. 00016 00017 #include "mbed.h" 00018 #include "DS1620.h" 00019 00020 #define WRITE_COMPLETE_TIME_MS (100) // time for write to complete (100 msec) 00021 00022 // Constructor 00023 DS1620::DS1620(PinName dq, PinName clk, PinName rst) : _dq(dq), _clk(clk), _rst(rst) 00024 { 00025 // Initialize members 00026 _dq.input(); 00027 _clk = 1; 00028 _rst = 0; 00029 _clockDelay = CLOCK_DELAY; 00030 } 00031 00032 // Destructor 00033 DS1620::~DS1620() 00034 { 00035 00036 } 00037 00038 float DS1620::getTemperature() 00039 { 00040 float temp = 0.0; 00041 unsigned short rawTemp = readTemperatureRaw(); 00042 temp = rawTemp / 2.0f; 00043 if (rawTemp >= 0x100) { 00044 temp -= 256.0f; 00045 } 00046 return temp; 00047 } 00048 00049 float DS1620::getHighResolutionTemperature() 00050 { 00051 float temp = 0.0f; 00052 unsigned short rawTemp, counter, slope = 0; 00053 rawTemp = readTemperatureRaw(); 00054 counter = readCounter(); 00055 loadSlope(); 00056 slope = readCounter(); 00057 00058 temp = (rawTemp >> 1) - 0.25f; 00059 if (rawTemp >= 0x100) { 00060 temp -= 256.0f; 00061 } 00062 temp += ((float)slope - (float)counter) / (float)slope; 00063 return temp; 00064 } 00065 00066 unsigned short DS1620::readTemperatureRaw() 00067 { 00068 unsigned short temp = 0; 00069 unsigned char tempLSB, tempMSB = 0; 00070 00071 _rst = 1; 00072 shiftOut(READ_TEMPERATURE); 00073 tempLSB = shiftIn(); 00074 tempMSB = shiftIn(); 00075 _rst = 0; 00076 temp = tempMSB * 256; 00077 temp += tempLSB; 00078 return temp; 00079 } 00080 00081 unsigned char DS1620::readConfig() 00082 { 00083 unsigned char data = 0; 00084 _rst = 1; 00085 shiftOut(READ_CONFIG); 00086 data = shiftIn(); 00087 _rst = 0; 00088 return data; 00089 } 00090 00091 void DS1620::writeConfig(unsigned char config) 00092 { 00093 _rst = 1; 00094 shiftOut(WRITE_CONFIG); 00095 shiftOut(config); 00096 _rst = 0; 00097 wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete 00098 } 00099 00100 unsigned short DS1620::readTLRaw() 00101 { 00102 unsigned short temp = 0; 00103 unsigned char tempLSB, tempMSB = 0; 00104 00105 _rst = 1; 00106 shiftOut(READ_TL); 00107 tempLSB = shiftIn(); 00108 tempMSB = shiftIn(); 00109 _rst = 0; 00110 temp = tempMSB * 256; 00111 temp += tempLSB; 00112 return temp; 00113 } 00114 00115 void DS1620::writeTLRaw(unsigned short temperature) 00116 { 00117 unsigned char tempMSB, tempLSB = 0; 00118 tempLSB = temperature & 0xFF; 00119 tempMSB = (temperature >> 8) & 0xFF; 00120 _rst = 1; 00121 shiftOut(WRITE_TL); 00122 shiftOut(tempLSB); 00123 shiftOut(tempMSB); 00124 _rst = 0; 00125 wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete 00126 } 00127 00128 00129 unsigned short DS1620::readTHRaw() 00130 { 00131 unsigned short temp = 0; 00132 unsigned char tempLSB, tempMSB = 0; 00133 00134 _rst = 1; 00135 shiftOut(READ_TH); 00136 tempLSB = shiftIn(); 00137 tempMSB = shiftIn(); 00138 _rst = 0; 00139 temp = tempMSB * 256; 00140 temp += tempLSB; 00141 return temp; 00142 } 00143 00144 void DS1620::writeTHRaw(unsigned short temperature) 00145 { 00146 unsigned char tempMSB, tempLSB = 0; 00147 tempLSB = temperature & 0xFF; 00148 tempMSB = (temperature >> 8) & 0xFF; 00149 _rst = 1; 00150 shiftOut(WRITE_TH); 00151 shiftOut(tempLSB); 00152 shiftOut(tempMSB); 00153 _rst = 0; 00154 wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete 00155 } 00156 00157 unsigned short DS1620::readCounter() 00158 { 00159 unsigned short temp = 0; 00160 unsigned char tempLSB, tempMSB = 0; 00161 00162 _rst = 1; 00163 shiftOut(READ_COUNTER); 00164 tempLSB = shiftIn(); 00165 tempMSB = shiftIn(); 00166 _rst = 0; 00167 temp = tempMSB * 256; 00168 temp += tempLSB; 00169 return temp; 00170 } 00171 00172 unsigned short DS1620::readSlope() 00173 { 00174 unsigned short temp = 0; 00175 unsigned char tempLSB, tempMSB = 0; 00176 00177 _rst = 1; 00178 shiftOut(READ_SLOPE); 00179 tempLSB = shiftIn(); 00180 tempMSB = shiftIn(); 00181 _rst = 0; 00182 temp = tempMSB * 256; 00183 temp += tempLSB; 00184 return temp; 00185 } 00186 00187 void DS1620::loadSlope() 00188 { 00189 _rst = 1; 00190 shiftOut(LOAD_SLOPE); 00191 _rst = 0; 00192 } 00193 00194 void DS1620::startConversion() 00195 { 00196 _rst = 1; 00197 shiftOut(START_CONVERT); 00198 _rst = 0; 00199 } 00200 00201 void DS1620::stopConversion() 00202 { 00203 _rst = 1; 00204 shiftOut(STOP_CONVERT); 00205 _rst = 0; 00206 } 00207 00208 void DS1620::setSerialClockFrequency(clock_frequencies_t frequency) 00209 { 00210 _clockDelay = frequency; 00211 } 00212 00213 unsigned char DS1620::shiftIn() 00214 { 00215 unsigned char temp = 0; 00216 unsigned char data = 0; 00217 _dq.input(); 00218 for(int x=0;x<8;x++) { 00219 _clk = 0; 00220 wait_us(_clockDelay); 00221 temp = _dq; 00222 temp = temp << 7; 00223 data = data >> 1; 00224 data = data | temp; 00225 _clk = 1; 00226 wait_us(_clockDelay); 00227 } 00228 return data; 00229 } 00230 00231 void DS1620::shiftOut(unsigned char data) 00232 { 00233 _dq.output(); 00234 for(int x=0;x<8;x++){ 00235 _clk = 0; 00236 _dq = data & 0x01; 00237 wait_us(_clockDelay); 00238 _clk = 1; 00239 wait_us(_clockDelay); 00240 data = data >> 1; 00241 } 00242 _dq.input(); 00243 }
Generated on Tue Jul 12 2022 16:58:32 by
1.7.2
