Ryan Bancroft / DS1620
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1620.h Source File

DS1620.h

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 
00018 #ifndef DS1620_H
00019 #define DS1620_H
00020 
00021 #include "mbed.h"
00022 
00023 enum clock_frequencies_t {freq500k = 1, 
00024                           freq250k = 2,
00025                           freq125k = 4,
00026                           freq20k = 25,
00027                           freq10k = 50,
00028                           freq1k = 500}; 
00029                           
00030 #define CLOCK_DELAY freq500k
00031 
00032 // Command definitions
00033 #define READ_TEMPERATURE 0xAA
00034 #define WRITE_TH         0x01
00035 #define WRITE_TL         0x02
00036 #define READ_TH          0xA1
00037 #define READ_TL          0xA2
00038 #define READ_COUNTER     0xA0
00039 #define READ_SLOPE       0xA9
00040 #define START_CONVERT    0xEE
00041 #define STOP_CONVERT     0x22
00042 #define WRITE_CONFIG     0x0C
00043 #define READ_CONFIG      0xAC
00044 #define LOAD_SLOPE       0x41
00045 
00046 /**
00047 * Example:
00048 * @code
00049 * // Send the temperature to your computer
00050 * #include "mbed.h"
00051 * #include "DS1620.h"
00052 *
00053 * DS1620 temperatureSensor(p11, p12, p13);
00054 * Serial pc(USBTX, USBRX);
00055 *
00056 * int main() {
00057 *   //Set mode to CPU & One Shot if it's not already set.
00058 *   if((temperatureSensor.readConfig() & 0x03) != 0x03) {
00059 *      temperatureSensor.writeConfig(0x03);
00060 *   }
00061 *   
00062 *   float temperature = 0.0;
00063 *   while(1) {
00064 *       temperatureSensor.startConversion();
00065 *       wait(1.0); //Wait for the temperature conversion to complete.  You can also poll the DONE flag in the STATUS REGISTER
00066 *       temperature = temperatureSensor.getHighResolutionTemperature();
00067 *       pc.printf("%.2f C\r\n", temperature);
00068 *   }       
00069 * }
00070 * @endcode
00071 */
00072 
00073 // Class declaration
00074 class DS1620
00075 {
00076     public:
00077         DS1620 (PinName dq, PinName clk, PinName rst);
00078         ~DS1620 ();
00079  
00080  
00081         /**
00082         * Get the temperature in degrees Celsius
00083         * 
00084         * @returns The temperature in Celsius with 0.5 degree resolution as a float 
00085         */
00086         float getTemperature();
00087         
00088         
00089         /**
00090         * Get the temperature in degrees Celsius
00091         * 
00092         * @returns The temperature in Celsius calculated using the COUNTER and SLOPE REGISTERS as a float
00093         * @note The DS1620 must be in 1SHOT mode for accurate results.
00094         */
00095         float getHighResolutionTemperature();
00096  
00097         /**
00098         * Read the TEMPERATURE REGISTER
00099         * 
00100         * @returns The temperature in Celsius with 0.5 degree resolution as stored in the DS1620 registers 
00101         */       
00102         unsigned short readTemperatureRaw();
00103         
00104         /**
00105         * Read the CONFIGURATION/STATUS REGISTER
00106         * 
00107         * @returns The value of the CONFIGURATION/STATUS REGISTER 
00108         */
00109         unsigned char readConfig();
00110         
00111         /**
00112         * Write to the CONFIGURATION/STATUS REGISTER
00113         * 
00114         * @param  config  The value to write to the CONFIGURATION/STATUS REGISTER 
00115         */
00116         void writeConfig(unsigned char config);
00117         
00118 
00119         /**
00120         * Read the TL (Thermostat Low Limit) REGISTER
00121         * 
00122         * @returns  The value to write to the TL (Thermostat Low Limit) REGISTER in the DS1620 format
00123         */        
00124         unsigned short readTLRaw();
00125         
00126         /**
00127         * Write to the TL (Thermostat Low Limit) REGISTER
00128         * 
00129         * @param  temperature  The value to write to the TL (Thermostat Low Limit) REGISTER in the DS1620 format
00130         */
00131         void writeTLRaw(unsigned short temperature);
00132         
00133         
00134         /**
00135         * Read the TH (Thermostat High Limit) REGISTER
00136         * 
00137         * @returns  The value to write to the TH (Thermostat High Limit) REGISTER in the DS1620 format
00138         */
00139         unsigned short readTHRaw();
00140         
00141         
00142         /**
00143         * Write to the TH (Thermostat High Limit) REGISTER
00144         * 
00145         * @param  temperature  The value to write to the TH (Thermostat High Limit) REGISTER in the DS1620 format
00146         */
00147         void writeTHRaw(unsigned short temperature);
00148         
00149         /**
00150         * Read the COUNTER REGISTER
00151         * 
00152         * @returns The COUNTER REGISTER
00153         */
00154         unsigned short readCounter();
00155         
00156         /**
00157         * Read the SLOPE REGISTER
00158         * 
00159         * @returns The SLOPE REGISTER
00160         */
00161         unsigned short readSlope();
00162         
00163         
00164         /**
00165         * Loads the SLOPE REGISTER into the COUNTER REGISTER
00166         */
00167         void loadSlope();
00168         
00169         
00170         /**
00171         * Starts a temperature conversion
00172         */        
00173         void startConversion();
00174         
00175         
00176         /**
00177         * Stops a temperature conversion
00178         */
00179         void stopConversion();
00180         
00181         /**
00182         * Sets the clock frequency for the serial interface
00183         *
00184         * @param frequency (freq500k, freq250k, freq125k, freq20k, freq10k, freq1k)
00185         * @note Defaults to 500KHz (freq500k)
00186         */
00187         void setSerialClockFrequency(clock_frequencies_t frequency);
00188         
00189 
00190     private:
00191         void shiftOut(unsigned char data);
00192         unsigned char shiftIn();
00193         
00194         clock_frequencies_t _clockDelay;
00195         
00196         DigitalInOut _dq;
00197         DigitalOut _clk;
00198         DigitalOut _rst;
00199         
00200 };
00201 
00202 #endif