Optimization of original DS1620 library - calls to wait(float) changed to wait_us(int) + resolving several warnings.
Dependents: EXAMPLE_Nucleo_mbed_RTOS_test_code
Fork of DS1620 by
DS1620.cpp@2:35526d3a8a04, 2015-12-12 (annotated)
- Committer:
- dzoni
- Date:
- Sat Dec 12 19:29:43 2015 +0000
- Revision:
- 2:35526d3a8a04
- Parent:
- 0:45c93163189a
1. Warnings resolved: implicit type conversion float to double and back to float; ; 2. Optimization: wait for write operation completion changed from wait(float) to wait_ms(int)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rybancr | 0:45c93163189a | 1 | // Library for the DS1620 digital thermometer |
rybancr | 0:45c93163189a | 2 | // Copyright (C) <2015> Ryan Bancroft |
rybancr | 0:45c93163189a | 3 | // |
rybancr | 0:45c93163189a | 4 | // This program is free software: you can redistribute it and/or modify |
rybancr | 0:45c93163189a | 5 | // it under the terms of the GNU General Public License as published by |
rybancr | 0:45c93163189a | 6 | // the Free Software Foundation, either version 3 of the License, or |
rybancr | 0:45c93163189a | 7 | // (at your option) any later version. |
rybancr | 0:45c93163189a | 8 | // |
rybancr | 0:45c93163189a | 9 | // This program is distributed in the hope that it will be useful, |
rybancr | 0:45c93163189a | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
rybancr | 0:45c93163189a | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
rybancr | 0:45c93163189a | 12 | // GNU General Public License for more details. |
rybancr | 0:45c93163189a | 13 | // |
rybancr | 0:45c93163189a | 14 | // For copy of the GNU General Public License |
rybancr | 0:45c93163189a | 15 | // see <http://www.gnu.org/licenses/>. |
rybancr | 0:45c93163189a | 16 | |
rybancr | 0:45c93163189a | 17 | #include "mbed.h" |
rybancr | 0:45c93163189a | 18 | #include "DS1620.h" |
rybancr | 0:45c93163189a | 19 | |
dzoni | 2:35526d3a8a04 | 20 | #define WRITE_COMPLETE_TIME_MS (100) // time for write to complete (100 msec) |
dzoni | 2:35526d3a8a04 | 21 | |
rybancr | 0:45c93163189a | 22 | // Constructor |
rybancr | 0:45c93163189a | 23 | DS1620::DS1620(PinName dq, PinName clk, PinName rst) : _dq(dq), _clk(clk), _rst(rst) |
rybancr | 0:45c93163189a | 24 | { |
rybancr | 0:45c93163189a | 25 | // Initialize members |
rybancr | 0:45c93163189a | 26 | _dq.input(); |
rybancr | 0:45c93163189a | 27 | _clk = 1; |
rybancr | 0:45c93163189a | 28 | _rst = 0; |
rybancr | 0:45c93163189a | 29 | _clockDelay = CLOCK_DELAY; |
rybancr | 0:45c93163189a | 30 | } |
rybancr | 0:45c93163189a | 31 | |
rybancr | 0:45c93163189a | 32 | // Destructor |
rybancr | 0:45c93163189a | 33 | DS1620::~DS1620() |
rybancr | 0:45c93163189a | 34 | { |
rybancr | 0:45c93163189a | 35 | |
rybancr | 0:45c93163189a | 36 | } |
rybancr | 0:45c93163189a | 37 | |
rybancr | 0:45c93163189a | 38 | float DS1620::getTemperature() |
rybancr | 0:45c93163189a | 39 | { |
rybancr | 0:45c93163189a | 40 | float temp = 0.0; |
rybancr | 0:45c93163189a | 41 | unsigned short rawTemp = readTemperatureRaw(); |
dzoni | 2:35526d3a8a04 | 42 | temp = rawTemp / 2.0f; |
rybancr | 0:45c93163189a | 43 | if (rawTemp >= 0x100) { |
dzoni | 2:35526d3a8a04 | 44 | temp -= 256.0f; |
rybancr | 0:45c93163189a | 45 | } |
rybancr | 0:45c93163189a | 46 | return temp; |
rybancr | 0:45c93163189a | 47 | } |
rybancr | 0:45c93163189a | 48 | |
rybancr | 0:45c93163189a | 49 | float DS1620::getHighResolutionTemperature() |
rybancr | 0:45c93163189a | 50 | { |
dzoni | 2:35526d3a8a04 | 51 | float temp = 0.0f; |
rybancr | 0:45c93163189a | 52 | unsigned short rawTemp, counter, slope = 0; |
rybancr | 0:45c93163189a | 53 | rawTemp = readTemperatureRaw(); |
rybancr | 0:45c93163189a | 54 | counter = readCounter(); |
rybancr | 0:45c93163189a | 55 | loadSlope(); |
rybancr | 0:45c93163189a | 56 | slope = readCounter(); |
rybancr | 0:45c93163189a | 57 | |
dzoni | 2:35526d3a8a04 | 58 | temp = (rawTemp >> 1) - 0.25f; |
rybancr | 0:45c93163189a | 59 | if (rawTemp >= 0x100) { |
dzoni | 2:35526d3a8a04 | 60 | temp -= 256.0f; |
rybancr | 0:45c93163189a | 61 | } |
rybancr | 0:45c93163189a | 62 | temp += ((float)slope - (float)counter) / (float)slope; |
rybancr | 0:45c93163189a | 63 | return temp; |
rybancr | 0:45c93163189a | 64 | } |
rybancr | 0:45c93163189a | 65 | |
rybancr | 0:45c93163189a | 66 | unsigned short DS1620::readTemperatureRaw() |
rybancr | 0:45c93163189a | 67 | { |
rybancr | 0:45c93163189a | 68 | unsigned short temp = 0; |
rybancr | 0:45c93163189a | 69 | unsigned char tempLSB, tempMSB = 0; |
rybancr | 0:45c93163189a | 70 | |
rybancr | 0:45c93163189a | 71 | _rst = 1; |
rybancr | 0:45c93163189a | 72 | shiftOut(READ_TEMPERATURE); |
rybancr | 0:45c93163189a | 73 | tempLSB = shiftIn(); |
rybancr | 0:45c93163189a | 74 | tempMSB = shiftIn(); |
rybancr | 0:45c93163189a | 75 | _rst = 0; |
rybancr | 0:45c93163189a | 76 | temp = tempMSB * 256; |
rybancr | 0:45c93163189a | 77 | temp += tempLSB; |
rybancr | 0:45c93163189a | 78 | return temp; |
rybancr | 0:45c93163189a | 79 | } |
rybancr | 0:45c93163189a | 80 | |
rybancr | 0:45c93163189a | 81 | unsigned char DS1620::readConfig() |
rybancr | 0:45c93163189a | 82 | { |
rybancr | 0:45c93163189a | 83 | unsigned char data = 0; |
rybancr | 0:45c93163189a | 84 | _rst = 1; |
rybancr | 0:45c93163189a | 85 | shiftOut(READ_CONFIG); |
rybancr | 0:45c93163189a | 86 | data = shiftIn(); |
rybancr | 0:45c93163189a | 87 | _rst = 0; |
rybancr | 0:45c93163189a | 88 | return data; |
rybancr | 0:45c93163189a | 89 | } |
rybancr | 0:45c93163189a | 90 | |
rybancr | 0:45c93163189a | 91 | void DS1620::writeConfig(unsigned char config) |
rybancr | 0:45c93163189a | 92 | { |
rybancr | 0:45c93163189a | 93 | _rst = 1; |
rybancr | 0:45c93163189a | 94 | shiftOut(WRITE_CONFIG); |
rybancr | 0:45c93163189a | 95 | shiftOut(config); |
rybancr | 0:45c93163189a | 96 | _rst = 0; |
dzoni | 2:35526d3a8a04 | 97 | wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete |
rybancr | 0:45c93163189a | 98 | } |
rybancr | 0:45c93163189a | 99 | |
rybancr | 0:45c93163189a | 100 | unsigned short DS1620::readTLRaw() |
rybancr | 0:45c93163189a | 101 | { |
rybancr | 0:45c93163189a | 102 | unsigned short temp = 0; |
rybancr | 0:45c93163189a | 103 | unsigned char tempLSB, tempMSB = 0; |
rybancr | 0:45c93163189a | 104 | |
rybancr | 0:45c93163189a | 105 | _rst = 1; |
rybancr | 0:45c93163189a | 106 | shiftOut(READ_TL); |
rybancr | 0:45c93163189a | 107 | tempLSB = shiftIn(); |
rybancr | 0:45c93163189a | 108 | tempMSB = shiftIn(); |
rybancr | 0:45c93163189a | 109 | _rst = 0; |
rybancr | 0:45c93163189a | 110 | temp = tempMSB * 256; |
rybancr | 0:45c93163189a | 111 | temp += tempLSB; |
rybancr | 0:45c93163189a | 112 | return temp; |
rybancr | 0:45c93163189a | 113 | } |
rybancr | 0:45c93163189a | 114 | |
rybancr | 0:45c93163189a | 115 | void DS1620::writeTLRaw(unsigned short temperature) |
rybancr | 0:45c93163189a | 116 | { |
rybancr | 0:45c93163189a | 117 | unsigned char tempMSB, tempLSB = 0; |
rybancr | 0:45c93163189a | 118 | tempLSB = temperature & 0xFF; |
rybancr | 0:45c93163189a | 119 | tempMSB = (temperature >> 8) & 0xFF; |
rybancr | 0:45c93163189a | 120 | _rst = 1; |
rybancr | 0:45c93163189a | 121 | shiftOut(WRITE_TL); |
rybancr | 0:45c93163189a | 122 | shiftOut(tempLSB); |
rybancr | 0:45c93163189a | 123 | shiftOut(tempMSB); |
rybancr | 0:45c93163189a | 124 | _rst = 0; |
dzoni | 2:35526d3a8a04 | 125 | wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete |
rybancr | 0:45c93163189a | 126 | } |
rybancr | 0:45c93163189a | 127 | |
rybancr | 0:45c93163189a | 128 | |
rybancr | 0:45c93163189a | 129 | unsigned short DS1620::readTHRaw() |
rybancr | 0:45c93163189a | 130 | { |
rybancr | 0:45c93163189a | 131 | unsigned short temp = 0; |
rybancr | 0:45c93163189a | 132 | unsigned char tempLSB, tempMSB = 0; |
rybancr | 0:45c93163189a | 133 | |
rybancr | 0:45c93163189a | 134 | _rst = 1; |
rybancr | 0:45c93163189a | 135 | shiftOut(READ_TH); |
rybancr | 0:45c93163189a | 136 | tempLSB = shiftIn(); |
rybancr | 0:45c93163189a | 137 | tempMSB = shiftIn(); |
rybancr | 0:45c93163189a | 138 | _rst = 0; |
rybancr | 0:45c93163189a | 139 | temp = tempMSB * 256; |
rybancr | 0:45c93163189a | 140 | temp += tempLSB; |
rybancr | 0:45c93163189a | 141 | return temp; |
rybancr | 0:45c93163189a | 142 | } |
rybancr | 0:45c93163189a | 143 | |
rybancr | 0:45c93163189a | 144 | void DS1620::writeTHRaw(unsigned short temperature) |
rybancr | 0:45c93163189a | 145 | { |
rybancr | 0:45c93163189a | 146 | unsigned char tempMSB, tempLSB = 0; |
rybancr | 0:45c93163189a | 147 | tempLSB = temperature & 0xFF; |
rybancr | 0:45c93163189a | 148 | tempMSB = (temperature >> 8) & 0xFF; |
rybancr | 0:45c93163189a | 149 | _rst = 1; |
rybancr | 0:45c93163189a | 150 | shiftOut(WRITE_TH); |
rybancr | 0:45c93163189a | 151 | shiftOut(tempLSB); |
rybancr | 0:45c93163189a | 152 | shiftOut(tempMSB); |
rybancr | 0:45c93163189a | 153 | _rst = 0; |
dzoni | 2:35526d3a8a04 | 154 | wait_ms(WRITE_COMPLETE_TIME_MS); // wait for write to complete |
rybancr | 0:45c93163189a | 155 | } |
rybancr | 0:45c93163189a | 156 | |
rybancr | 0:45c93163189a | 157 | unsigned short DS1620::readCounter() |
rybancr | 0:45c93163189a | 158 | { |
rybancr | 0:45c93163189a | 159 | unsigned short temp = 0; |
rybancr | 0:45c93163189a | 160 | unsigned char tempLSB, tempMSB = 0; |
rybancr | 0:45c93163189a | 161 | |
rybancr | 0:45c93163189a | 162 | _rst = 1; |
rybancr | 0:45c93163189a | 163 | shiftOut(READ_COUNTER); |
rybancr | 0:45c93163189a | 164 | tempLSB = shiftIn(); |
rybancr | 0:45c93163189a | 165 | tempMSB = shiftIn(); |
rybancr | 0:45c93163189a | 166 | _rst = 0; |
rybancr | 0:45c93163189a | 167 | temp = tempMSB * 256; |
rybancr | 0:45c93163189a | 168 | temp += tempLSB; |
rybancr | 0:45c93163189a | 169 | return temp; |
rybancr | 0:45c93163189a | 170 | } |
rybancr | 0:45c93163189a | 171 | |
rybancr | 0:45c93163189a | 172 | unsigned short DS1620::readSlope() |
rybancr | 0:45c93163189a | 173 | { |
rybancr | 0:45c93163189a | 174 | unsigned short temp = 0; |
rybancr | 0:45c93163189a | 175 | unsigned char tempLSB, tempMSB = 0; |
rybancr | 0:45c93163189a | 176 | |
rybancr | 0:45c93163189a | 177 | _rst = 1; |
rybancr | 0:45c93163189a | 178 | shiftOut(READ_SLOPE); |
rybancr | 0:45c93163189a | 179 | tempLSB = shiftIn(); |
rybancr | 0:45c93163189a | 180 | tempMSB = shiftIn(); |
rybancr | 0:45c93163189a | 181 | _rst = 0; |
rybancr | 0:45c93163189a | 182 | temp = tempMSB * 256; |
rybancr | 0:45c93163189a | 183 | temp += tempLSB; |
rybancr | 0:45c93163189a | 184 | return temp; |
rybancr | 0:45c93163189a | 185 | } |
rybancr | 0:45c93163189a | 186 | |
rybancr | 0:45c93163189a | 187 | void DS1620::loadSlope() |
rybancr | 0:45c93163189a | 188 | { |
rybancr | 0:45c93163189a | 189 | _rst = 1; |
rybancr | 0:45c93163189a | 190 | shiftOut(LOAD_SLOPE); |
rybancr | 0:45c93163189a | 191 | _rst = 0; |
rybancr | 0:45c93163189a | 192 | } |
rybancr | 0:45c93163189a | 193 | |
rybancr | 0:45c93163189a | 194 | void DS1620::startConversion() |
rybancr | 0:45c93163189a | 195 | { |
rybancr | 0:45c93163189a | 196 | _rst = 1; |
rybancr | 0:45c93163189a | 197 | shiftOut(START_CONVERT); |
rybancr | 0:45c93163189a | 198 | _rst = 0; |
rybancr | 0:45c93163189a | 199 | } |
rybancr | 0:45c93163189a | 200 | |
rybancr | 0:45c93163189a | 201 | void DS1620::stopConversion() |
rybancr | 0:45c93163189a | 202 | { |
rybancr | 0:45c93163189a | 203 | _rst = 1; |
rybancr | 0:45c93163189a | 204 | shiftOut(STOP_CONVERT); |
rybancr | 0:45c93163189a | 205 | _rst = 0; |
rybancr | 0:45c93163189a | 206 | } |
rybancr | 0:45c93163189a | 207 | |
rybancr | 0:45c93163189a | 208 | void DS1620::setSerialClockFrequency(clock_frequencies_t frequency) |
rybancr | 0:45c93163189a | 209 | { |
rybancr | 0:45c93163189a | 210 | _clockDelay = frequency; |
rybancr | 0:45c93163189a | 211 | } |
rybancr | 0:45c93163189a | 212 | |
rybancr | 0:45c93163189a | 213 | unsigned char DS1620::shiftIn() |
rybancr | 0:45c93163189a | 214 | { |
rybancr | 0:45c93163189a | 215 | unsigned char temp = 0; |
rybancr | 0:45c93163189a | 216 | unsigned char data = 0; |
rybancr | 0:45c93163189a | 217 | _dq.input(); |
rybancr | 0:45c93163189a | 218 | for(int x=0;x<8;x++) { |
rybancr | 0:45c93163189a | 219 | _clk = 0; |
rybancr | 0:45c93163189a | 220 | wait_us(_clockDelay); |
rybancr | 0:45c93163189a | 221 | temp = _dq; |
rybancr | 0:45c93163189a | 222 | temp = temp << 7; |
rybancr | 0:45c93163189a | 223 | data = data >> 1; |
rybancr | 0:45c93163189a | 224 | data = data | temp; |
rybancr | 0:45c93163189a | 225 | _clk = 1; |
rybancr | 0:45c93163189a | 226 | wait_us(_clockDelay); |
rybancr | 0:45c93163189a | 227 | } |
rybancr | 0:45c93163189a | 228 | return data; |
rybancr | 0:45c93163189a | 229 | } |
rybancr | 0:45c93163189a | 230 | |
rybancr | 0:45c93163189a | 231 | void DS1620::shiftOut(unsigned char data) |
rybancr | 0:45c93163189a | 232 | { |
rybancr | 0:45c93163189a | 233 | _dq.output(); |
rybancr | 0:45c93163189a | 234 | for(int x=0;x<8;x++){ |
rybancr | 0:45c93163189a | 235 | _clk = 0; |
rybancr | 0:45c93163189a | 236 | _dq = data & 0x01; |
rybancr | 0:45c93163189a | 237 | wait_us(_clockDelay); |
rybancr | 0:45c93163189a | 238 | _clk = 1; |
rybancr | 0:45c93163189a | 239 | wait_us(_clockDelay); |
rybancr | 0:45c93163189a | 240 | data = data >> 1; |
rybancr | 0:45c93163189a | 241 | } |
rybancr | 0:45c93163189a | 242 | _dq.input(); |
rybancr | 0:45c93163189a | 243 | } |