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.h@2:35526d3a8a04, 2015-12-12 (annotated)
- Committer:
- dzoni
- Date:
- Sat Dec 12 19:29:43 2015 +0000
- Revision:
- 2:35526d3a8a04
- Parent:
- 1:f6fbf299550b
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 | |
rybancr | 0:45c93163189a | 18 | #ifndef DS1620_H |
rybancr | 0:45c93163189a | 19 | #define DS1620_H |
rybancr | 0:45c93163189a | 20 | |
rybancr | 0:45c93163189a | 21 | #include "mbed.h" |
rybancr | 0:45c93163189a | 22 | |
rybancr | 0:45c93163189a | 23 | enum clock_frequencies_t {freq500k = 1, |
rybancr | 0:45c93163189a | 24 | freq250k = 2, |
rybancr | 0:45c93163189a | 25 | freq125k = 4, |
rybancr | 0:45c93163189a | 26 | freq20k = 25, |
rybancr | 0:45c93163189a | 27 | freq10k = 50, |
rybancr | 0:45c93163189a | 28 | freq1k = 500}; |
rybancr | 0:45c93163189a | 29 | |
rybancr | 0:45c93163189a | 30 | #define CLOCK_DELAY freq500k |
rybancr | 0:45c93163189a | 31 | |
rybancr | 0:45c93163189a | 32 | // Command definitions |
rybancr | 0:45c93163189a | 33 | #define READ_TEMPERATURE 0xAA |
rybancr | 0:45c93163189a | 34 | #define WRITE_TH 0x01 |
rybancr | 0:45c93163189a | 35 | #define WRITE_TL 0x02 |
rybancr | 0:45c93163189a | 36 | #define READ_TH 0xA1 |
rybancr | 0:45c93163189a | 37 | #define READ_TL 0xA2 |
rybancr | 0:45c93163189a | 38 | #define READ_COUNTER 0xA0 |
rybancr | 0:45c93163189a | 39 | #define READ_SLOPE 0xA9 |
rybancr | 0:45c93163189a | 40 | #define START_CONVERT 0xEE |
rybancr | 0:45c93163189a | 41 | #define STOP_CONVERT 0x22 |
rybancr | 0:45c93163189a | 42 | #define WRITE_CONFIG 0x0C |
rybancr | 0:45c93163189a | 43 | #define READ_CONFIG 0xAC |
rybancr | 0:45c93163189a | 44 | #define LOAD_SLOPE 0x41 |
rybancr | 0:45c93163189a | 45 | |
rybancr | 0:45c93163189a | 46 | /** |
rybancr | 0:45c93163189a | 47 | * Example: |
rybancr | 0:45c93163189a | 48 | * @code |
rybancr | 0:45c93163189a | 49 | * // Send the temperature to your computer |
rybancr | 0:45c93163189a | 50 | * #include "mbed.h" |
rybancr | 0:45c93163189a | 51 | * #include "DS1620.h" |
rybancr | 0:45c93163189a | 52 | * |
rybancr | 0:45c93163189a | 53 | * DS1620 temperatureSensor(p11, p12, p13); |
rybancr | 0:45c93163189a | 54 | * Serial pc(USBTX, USBRX); |
rybancr | 0:45c93163189a | 55 | * |
rybancr | 0:45c93163189a | 56 | * int main() { |
rybancr | 0:45c93163189a | 57 | * //Set mode to CPU & One Shot if it's not already set. |
rybancr | 0:45c93163189a | 58 | * if((temperatureSensor.readConfig() & 0x03) != 0x03) { |
rybancr | 0:45c93163189a | 59 | * temperatureSensor.writeConfig(0x03); |
rybancr | 0:45c93163189a | 60 | * } |
rybancr | 0:45c93163189a | 61 | * |
rybancr | 0:45c93163189a | 62 | * float temperature = 0.0; |
rybancr | 0:45c93163189a | 63 | * while(1) { |
rybancr | 0:45c93163189a | 64 | * temperatureSensor.startConversion(); |
rybancr | 0:45c93163189a | 65 | * wait(1.0); //Wait for the temperature conversion to complete. You can also poll the DONE flag in the STATUS REGISTER |
rybancr | 0:45c93163189a | 66 | * temperature = temperatureSensor.getHighResolutionTemperature(); |
rybancr | 0:45c93163189a | 67 | * pc.printf("%.2f C\r\n", temperature); |
rybancr | 0:45c93163189a | 68 | * } |
rybancr | 0:45c93163189a | 69 | * } |
rybancr | 0:45c93163189a | 70 | * @endcode |
rybancr | 0:45c93163189a | 71 | */ |
rybancr | 0:45c93163189a | 72 | |
rybancr | 0:45c93163189a | 73 | // Class declaration |
rybancr | 0:45c93163189a | 74 | class DS1620 |
rybancr | 0:45c93163189a | 75 | { |
rybancr | 0:45c93163189a | 76 | public: |
rybancr | 0:45c93163189a | 77 | DS1620 (PinName dq, PinName clk, PinName rst); |
rybancr | 0:45c93163189a | 78 | ~DS1620 (); |
rybancr | 0:45c93163189a | 79 | |
rybancr | 0:45c93163189a | 80 | |
rybancr | 0:45c93163189a | 81 | /** |
rybancr | 0:45c93163189a | 82 | * Get the temperature in degrees Celsius |
rybancr | 0:45c93163189a | 83 | * |
rybancr | 0:45c93163189a | 84 | * @returns The temperature in Celsius with 0.5 degree resolution as a float |
rybancr | 0:45c93163189a | 85 | */ |
rybancr | 0:45c93163189a | 86 | float getTemperature(); |
rybancr | 0:45c93163189a | 87 | |
rybancr | 0:45c93163189a | 88 | |
rybancr | 0:45c93163189a | 89 | /** |
rybancr | 0:45c93163189a | 90 | * Get the temperature in degrees Celsius |
rybancr | 0:45c93163189a | 91 | * |
rybancr | 0:45c93163189a | 92 | * @returns The temperature in Celsius calculated using the COUNTER and SLOPE REGISTERS as a float |
rybancr | 0:45c93163189a | 93 | * @note The DS1620 must be in 1SHOT mode for accurate results. |
rybancr | 0:45c93163189a | 94 | */ |
rybancr | 0:45c93163189a | 95 | float getHighResolutionTemperature(); |
rybancr | 0:45c93163189a | 96 | |
rybancr | 0:45c93163189a | 97 | /** |
rybancr | 0:45c93163189a | 98 | * Read the TEMPERATURE REGISTER |
rybancr | 0:45c93163189a | 99 | * |
rybancr | 0:45c93163189a | 100 | * @returns The temperature in Celsius with 0.5 degree resolution as stored in the DS1620 registers |
rybancr | 0:45c93163189a | 101 | */ |
rybancr | 0:45c93163189a | 102 | unsigned short readTemperatureRaw(); |
rybancr | 0:45c93163189a | 103 | |
rybancr | 0:45c93163189a | 104 | /** |
rybancr | 0:45c93163189a | 105 | * Read the CONFIGURATION/STATUS REGISTER |
rybancr | 0:45c93163189a | 106 | * |
rybancr | 0:45c93163189a | 107 | * @returns The value of the CONFIGURATION/STATUS REGISTER |
rybancr | 0:45c93163189a | 108 | */ |
rybancr | 0:45c93163189a | 109 | unsigned char readConfig(); |
rybancr | 0:45c93163189a | 110 | |
rybancr | 0:45c93163189a | 111 | /** |
rybancr | 0:45c93163189a | 112 | * Write to the CONFIGURATION/STATUS REGISTER |
rybancr | 0:45c93163189a | 113 | * |
rybancr | 0:45c93163189a | 114 | * @param config The value to write to the CONFIGURATION/STATUS REGISTER |
rybancr | 0:45c93163189a | 115 | */ |
rybancr | 0:45c93163189a | 116 | void writeConfig(unsigned char config); |
rybancr | 0:45c93163189a | 117 | |
rybancr | 0:45c93163189a | 118 | |
rybancr | 0:45c93163189a | 119 | /** |
rybancr | 0:45c93163189a | 120 | * Read the TL (Thermostat Low Limit) REGISTER |
rybancr | 0:45c93163189a | 121 | * |
rybancr | 0:45c93163189a | 122 | * @returns The value to write to the TL (Thermostat Low Limit) REGISTER in the DS1620 format |
rybancr | 0:45c93163189a | 123 | */ |
rybancr | 0:45c93163189a | 124 | unsigned short readTLRaw(); |
rybancr | 0:45c93163189a | 125 | |
rybancr | 0:45c93163189a | 126 | /** |
rybancr | 0:45c93163189a | 127 | * Write to the TL (Thermostat Low Limit) REGISTER |
rybancr | 0:45c93163189a | 128 | * |
rybancr | 0:45c93163189a | 129 | * @param temperature The value to write to the TL (Thermostat Low Limit) REGISTER in the DS1620 format |
rybancr | 0:45c93163189a | 130 | */ |
rybancr | 0:45c93163189a | 131 | void writeTLRaw(unsigned short temperature); |
rybancr | 0:45c93163189a | 132 | |
rybancr | 0:45c93163189a | 133 | |
rybancr | 0:45c93163189a | 134 | /** |
rybancr | 0:45c93163189a | 135 | * Read the TH (Thermostat High Limit) REGISTER |
rybancr | 0:45c93163189a | 136 | * |
rybancr | 0:45c93163189a | 137 | * @returns The value to write to the TH (Thermostat High Limit) REGISTER in the DS1620 format |
rybancr | 0:45c93163189a | 138 | */ |
rybancr | 0:45c93163189a | 139 | unsigned short readTHRaw(); |
rybancr | 0:45c93163189a | 140 | |
rybancr | 0:45c93163189a | 141 | |
rybancr | 0:45c93163189a | 142 | /** |
rybancr | 0:45c93163189a | 143 | * Write to the TH (Thermostat High Limit) REGISTER |
rybancr | 0:45c93163189a | 144 | * |
rybancr | 0:45c93163189a | 145 | * @param temperature The value to write to the TH (Thermostat High Limit) REGISTER in the DS1620 format |
rybancr | 0:45c93163189a | 146 | */ |
rybancr | 0:45c93163189a | 147 | void writeTHRaw(unsigned short temperature); |
rybancr | 0:45c93163189a | 148 | |
rybancr | 0:45c93163189a | 149 | /** |
rybancr | 0:45c93163189a | 150 | * Read the COUNTER REGISTER |
rybancr | 0:45c93163189a | 151 | * |
rybancr | 0:45c93163189a | 152 | * @returns The COUNTER REGISTER |
rybancr | 0:45c93163189a | 153 | */ |
rybancr | 0:45c93163189a | 154 | unsigned short readCounter(); |
rybancr | 0:45c93163189a | 155 | |
rybancr | 0:45c93163189a | 156 | /** |
rybancr | 0:45c93163189a | 157 | * Read the SLOPE REGISTER |
rybancr | 0:45c93163189a | 158 | * |
rybancr | 0:45c93163189a | 159 | * @returns The SLOPE REGISTER |
rybancr | 0:45c93163189a | 160 | */ |
rybancr | 0:45c93163189a | 161 | unsigned short readSlope(); |
rybancr | 0:45c93163189a | 162 | |
rybancr | 0:45c93163189a | 163 | |
rybancr | 0:45c93163189a | 164 | /** |
rybancr | 0:45c93163189a | 165 | * Loads the SLOPE REGISTER into the COUNTER REGISTER |
rybancr | 0:45c93163189a | 166 | */ |
rybancr | 0:45c93163189a | 167 | void loadSlope(); |
rybancr | 0:45c93163189a | 168 | |
rybancr | 0:45c93163189a | 169 | |
rybancr | 0:45c93163189a | 170 | /** |
rybancr | 0:45c93163189a | 171 | * Starts a temperature conversion |
rybancr | 0:45c93163189a | 172 | */ |
rybancr | 0:45c93163189a | 173 | void startConversion(); |
rybancr | 0:45c93163189a | 174 | |
rybancr | 0:45c93163189a | 175 | |
rybancr | 0:45c93163189a | 176 | /** |
rybancr | 0:45c93163189a | 177 | * Stops a temperature conversion |
rybancr | 0:45c93163189a | 178 | */ |
rybancr | 0:45c93163189a | 179 | void stopConversion(); |
rybancr | 0:45c93163189a | 180 | |
rybancr | 0:45c93163189a | 181 | /** |
rybancr | 0:45c93163189a | 182 | * Sets the clock frequency for the serial interface |
rybancr | 0:45c93163189a | 183 | * |
rybancr | 0:45c93163189a | 184 | * @param frequency (freq500k, freq250k, freq125k, freq20k, freq10k, freq1k) |
rybancr | 0:45c93163189a | 185 | * @note Defaults to 500KHz (freq500k) |
rybancr | 0:45c93163189a | 186 | */ |
rybancr | 0:45c93163189a | 187 | void setSerialClockFrequency(clock_frequencies_t frequency); |
rybancr | 0:45c93163189a | 188 | |
rybancr | 0:45c93163189a | 189 | |
rybancr | 0:45c93163189a | 190 | private: |
rybancr | 0:45c93163189a | 191 | void shiftOut(unsigned char data); |
rybancr | 0:45c93163189a | 192 | unsigned char shiftIn(); |
rybancr | 0:45c93163189a | 193 | |
rybancr | 0:45c93163189a | 194 | clock_frequencies_t _clockDelay; |
rybancr | 0:45c93163189a | 195 | |
rybancr | 0:45c93163189a | 196 | DigitalInOut _dq; |
rybancr | 0:45c93163189a | 197 | DigitalOut _clk; |
rybancr | 0:45c93163189a | 198 | DigitalOut _rst; |
rybancr | 0:45c93163189a | 199 | |
rybancr | 0:45c93163189a | 200 | }; |
rybancr | 0:45c93163189a | 201 | |
rybancr | 0:45c93163189a | 202 | #endif |