Driver to read and control a serial (i2c) temperature sensor, The Microchip MCP9808 is the digital sensor to control, can be read it, set its resolution, shutdown and also set alarms.

Dependents:   Hotboards_temp_alarms Hotboards_temp_fahrenheit Hotboards_temp_reading_temperature LCD_Temperatura

Committer:
Hotboards
Date:
Thu Mar 17 04:57:00 2016 +0000
Revision:
0:83da47b7ed26
Child:
1:f850ee1083ba
first release (needs to be tested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:83da47b7ed26 1 /*
Hotboards 0:83da47b7ed26 2 Hotboards_temp.h - Driver to read and control a serial (i2c) temperature sensor, The Microchip
Hotboards 0:83da47b7ed26 3 MCP9808 is the digital sensor to control, can be read it, set its resolution, shutdown and also
Hotboards 0:83da47b7ed26 4 set alarms.
Hotboards 0:83da47b7ed26 5 Hotboards eeprom board (http://hotboards.org)
Hotboards 0:83da47b7ed26 6 Created by Diego Perez, March 19, 2016.
Hotboards 0:83da47b7ed26 7 Released into the public domain.
Hotboards 0:83da47b7ed26 8 */
Hotboards 0:83da47b7ed26 9 #ifndef Hotboards_Temp_h
Hotboards 0:83da47b7ed26 10 #define Hotboards_Temp_h
Hotboards 0:83da47b7ed26 11
Hotboards 0:83da47b7ed26 12 #include "mbed.h"
Hotboards 0:83da47b7ed26 13
Hotboards 0:83da47b7ed26 14 #define HT_SENSOR_OFF 0
Hotboards 0:83da47b7ed26 15 #define HT_SENSOR_ON 1
Hotboards 0:83da47b7ed26 16
Hotboards 0:83da47b7ed26 17 typedef enum
Hotboards 0:83da47b7ed26 18 {
Hotboards 0:83da47b7ed26 19 Sensor_0 = 0,
Hotboards 0:83da47b7ed26 20 Sensor_1,
Hotboards 0:83da47b7ed26 21 Sensor_2,
Hotboards 0:83da47b7ed26 22 Sensor_3,
Hotboards 0:83da47b7ed26 23 Sensor_4,
Hotboards 0:83da47b7ed26 24 Sensor_5,
Hotboards 0:83da47b7ed26 25 Sensor_6,
Hotboards 0:83da47b7ed26 26 Sensor_7
Hotboards 0:83da47b7ed26 27 } _eSensors;
Hotboards 0:83da47b7ed26 28
Hotboards 0:83da47b7ed26 29 typedef enum
Hotboards 0:83da47b7ed26 30 {
Hotboards 0:83da47b7ed26 31 _0p5C = 0,
Hotboards 0:83da47b7ed26 32 _0p25C,
Hotboards 0:83da47b7ed26 33 _0p125C,
Hotboards 0:83da47b7ed26 34 _0p0625C
Hotboards 0:83da47b7ed26 35 } _eResolution;
Hotboards 0:83da47b7ed26 36
Hotboards 0:83da47b7ed26 37 /** Hotboards_temp class.
Hotboards 0:83da47b7ed26 38 * Used to read and config Microchip MCP9808 i2c temp sensor
Hotboards 0:83da47b7ed26 39 *
Hotboards 0:83da47b7ed26 40 * Example:
Hotboards 0:83da47b7ed26 41 * @code
Hotboards 0:83da47b7ed26 42 * #include "Hotboards_temp.h"
Hotboards 0:83da47b7ed26 43 *
Hotboards 0:83da47b7ed26 44 * Hotboards_temp sensor( Sensor_7 );
Hotboards 0:83da47b7ed26 45 *
Hotboards 0:83da47b7ed26 46 * int main( void )
Hotboards 0:83da47b7ed26 47 * {
Hotboards 0:83da47b7ed26 48 * Wire.begin( );
Hotboards 0:83da47b7ed26 49 * sensor.begin( );
Hotboards 0:83da47b7ed26 50 * while(1){
Hotboards 0:83da47b7ed26 51 * float val = sensor.read( );
Hotboards 0:83da47b7ed26 52 * printf( "temp. %f", val );
Hotboards 0:83da47b7ed26 53 * wait(1);
Hotboards 0:83da47b7ed26 54 * }
Hotboards 0:83da47b7ed26 55 * }
Hotboards 0:83da47b7ed26 56 * @endcode
Hotboards 0:83da47b7ed26 57 */
Hotboards 0:83da47b7ed26 58 class Hotboards_temp
Hotboards 0:83da47b7ed26 59 {
Hotboards 0:83da47b7ed26 60 public :
Hotboards 0:83da47b7ed26 61 /** Create Hotboards_temp instance
Hotboards 0:83da47b7ed26 62 * @param i2c iic peripheral to use with the temp sensor
Hotboards 0:83da47b7ed26 63 * @param address sensor address (A0,A1 and A2 values)
Hotboards 0:83da47b7ed26 64 * @param resolution default temperature resoluion (use values from _eResolution enumeration)
Hotboards 0:83da47b7ed26 65 *
Hotboards 0:83da47b7ed26 66 * Example:
Hotboards 0:83da47b7ed26 67 * @code
Hotboards 0:83da47b7ed26 68 * // instance temp sensor with address 7 and 0.5 resolution (default)
Hotboards 0:83da47b7ed26 69 * Hotboards_temp sensor( Sensor_7 );
Hotboards 0:83da47b7ed26 70 * @endcode
Hotboards 0:83da47b7ed26 71 */
Hotboards 0:83da47b7ed26 72 Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution = _0p5C );
Hotboards 0:83da47b7ed26 73
Hotboards 0:83da47b7ed26 74 /** Detect if the sensor is conected and also set default resolution
Hotboards 0:83da47b7ed26 75 * @return '1' if a sensor is connected
Hotboards 0:83da47b7ed26 76 *
Hotboards 0:83da47b7ed26 77 * Example:
Hotboards 0:83da47b7ed26 78 * @code
Hotboards 0:83da47b7ed26 79 * // init sensor and also check if is conected
Hotboards 0:83da47b7ed26 80 * if( sensor.begin( ) == 1 ){
Hotboards 0:83da47b7ed26 81 * // sensor is detected, do something
Hotboards 0:83da47b7ed26 82 * }
Hotboards 0:83da47b7ed26 83 * @endcode
Hotboards 0:83da47b7ed26 84 */
Hotboards 0:83da47b7ed26 85 bool init( void );
Hotboards 0:83da47b7ed26 86
Hotboards 0:83da47b7ed26 87 /** Get temperature from sensor in Celsius degrees
Hotboards 0:83da47b7ed26 88 * @return The current temperature
Hotboards 0:83da47b7ed26 89 *
Hotboards 0:83da47b7ed26 90 * Example:
Hotboards 0:83da47b7ed26 91 * @code
Hotboards 0:83da47b7ed26 92 * // init temp sensor (assume sensor is connected)
Hotboards 0:83da47b7ed26 93 * sensor.begin( );
Hotboards 0:83da47b7ed26 94 * // read the current temperaure
Hotboards 0:83da47b7ed26 95 * float temp = sensor.read( );
Hotboards 0:83da47b7ed26 96 * @endcode
Hotboards 0:83da47b7ed26 97 */
Hotboards 0:83da47b7ed26 98 float read( void );
Hotboards 0:83da47b7ed26 99
Hotboards 0:83da47b7ed26 100 /** Set Tlower and Tupper values and also active ALERT output pin
Hotboards 0:83da47b7ed26 101 * @param lower lower temperaure value to set ALERT pin
Hotboards 0:83da47b7ed26 102 * @param upper upper temperaure value to set ALERT pin
Hotboards 0:83da47b7ed26 103 *
Hotboards 0:83da47b7ed26 104 * Example:
Hotboards 0:83da47b7ed26 105 * @code
Hotboards 0:83da47b7ed26 106 * // set lower nd upper alarm (on setup function)
Hotboards 0:83da47b7ed26 107 * sensor.setAlarm( 0.0, 10.0 );
Hotboards 0:83da47b7ed26 108 * // somewhere else on loop function
Hotboards 0:83da47b7ed26 109 * // assume we conected pin 5 (arduino) to ALERT pin (sensor)
Hotboards 0:83da47b7ed26 110 * if( readDigital(5) == 0 ){
Hotboards 0:83da47b7ed26 111 * // temp value under lower valur or upper value
Hotboards 0:83da47b7ed26 112 * }
Hotboards 0:83da47b7ed26 113 * @endcode
Hotboards 0:83da47b7ed26 114 */
Hotboards 0:83da47b7ed26 115 void setAlarms( float lower, float upper );
Hotboards 0:83da47b7ed26 116
Hotboards 0:83da47b7ed26 117 /** Disable alarma activation, the function does not clear the previus
Hotboards 0:83da47b7ed26 118 * alarm values, just disable the ALERT signal
Hotboards 0:83da47b7ed26 119 *
Hotboards 0:83da47b7ed26 120 * Example:
Hotboards 0:83da47b7ed26 121 * @code
Hotboards 0:83da47b7ed26 122 * // set lower nd upper alarm (on setup function)
Hotboards 0:83da47b7ed26 123 * sensor.setAlarm( 0.0, 10.0 );
Hotboards 0:83da47b7ed26 124 * // disable alarm, ALERt signal is disable
Hotboards 0:83da47b7ed26 125 * sensor.disableAlarms( );
Hotboards 0:83da47b7ed26 126 * @endcode
Hotboards 0:83da47b7ed26 127 */
Hotboards 0:83da47b7ed26 128 void disableAlarms( void );
Hotboards 0:83da47b7ed26 129
Hotboards 0:83da47b7ed26 130 /** Turn Off/On sensor (low power), after turn it on again, is necesary wait
Hotboards 0:83da47b7ed26 131 * from 30ms to 250ms (depends on resoluion) to read a valid temperature
Hotboards 0:83da47b7ed26 132 * @param state shutdown (HT_SENSOR_OFF) or turn on (HT_SENSOR_ON)
Hotboards 0:83da47b7ed26 133 *
Hotboards 0:83da47b7ed26 134 * Example:
Hotboards 0:83da47b7ed26 135 * @code
Hotboards 0:83da47b7ed26 136 * // lets assume sensor is initialized, so shutdown
Hotboards 0:83da47b7ed26 137 * sensor.shutdown( HT_SENSOR_OFF );
Hotboards 0:83da47b7ed26 138 * // the we can turn it on
Hotboards 0:83da47b7ed26 139 * sensor.shutdown( HT_SENSOR_ON );
Hotboards 0:83da47b7ed26 140 * @endcode
Hotboards 0:83da47b7ed26 141 */
Hotboards 0:83da47b7ed26 142 void shutdown( bool state );
Hotboards 0:83da47b7ed26 143
Hotboards 0:83da47b7ed26 144 /** set a new resolution, possible values are 0.0625, 0.125, 0.25 and 0.5
Hotboards 0:83da47b7ed26 145 * @param resolution use values from _eResolution enumeration (_0p5C, _0p25C, _0p125C and _0p0625C)
Hotboards 0:83da47b7ed26 146 *
Hotboards 0:83da47b7ed26 147 * Example:
Hotboards 0:83da47b7ed26 148 * @code
Hotboards 0:83da47b7ed26 149 * // lets assume sensor is initialized
Hotboards 0:83da47b7ed26 150 * sensor.setResolution( _0p0625C );
Hotboards 0:83da47b7ed26 151 * // read temperature with the new resolution
Hotboards 0:83da47b7ed26 152 * float temp = sensor.read( );
Hotboards 0:83da47b7ed26 153 * @endcode
Hotboards 0:83da47b7ed26 154 */
Hotboards 0:83da47b7ed26 155 void setResolution( uint8_t resolution );
Hotboards 0:83da47b7ed26 156
Hotboards 0:83da47b7ed26 157 /** Function to convert Celsius to Farenheit degrees
Hotboards 0:83da47b7ed26 158 * @param celsius temperature in celsius degrees
Hotboards 0:83da47b7ed26 159 *
Hotboards 0:83da47b7ed26 160 * Example:
Hotboards 0:83da47b7ed26 161 * @code
Hotboards 0:83da47b7ed26 162 * // read temperature in celsius degrees
Hotboards 0:83da47b7ed26 163 * float tempC = sensor.read( );
Hotboards 0:83da47b7ed26 164 * // convert to farenheit
Hotboards 0:83da47b7ed26 165 * float tempF = sensor.CelsiusToFarenheit( tempC );
Hotboards 0:83da47b7ed26 166 * @endcode
Hotboards 0:83da47b7ed26 167 */
Hotboards 0:83da47b7ed26 168 float CelsiusToFarenheit( float celsius );
Hotboards 0:83da47b7ed26 169
Hotboards 0:83da47b7ed26 170 /** Function to convert Farenheit to Celsius degrees
Hotboards 0:83da47b7ed26 171 * @param farenheit temperature in farenheit degrees
Hotboards 0:83da47b7ed26 172 *
Hotboards 0:83da47b7ed26 173 * Example:
Hotboards 0:83da47b7ed26 174 * @code
Hotboards 0:83da47b7ed26 175 * // convert to celsius a previus farenheit temperature
Hotboards 0:83da47b7ed26 176 * float tempC = sensor.FarenheitToCelsius( 98.5 );
Hotboards 0:83da47b7ed26 177 * @endcode
Hotboards 0:83da47b7ed26 178 */
Hotboards 0:83da47b7ed26 179 float FarenheitToCelsius( float farenheit );
Hotboards 0:83da47b7ed26 180
Hotboards 0:83da47b7ed26 181 private :
Hotboards 0:83da47b7ed26 182 uint16_t readReg( uint8_t reg );
Hotboards 0:83da47b7ed26 183 void writeReg( uint8_t reg, uint16_t val );
Hotboards 0:83da47b7ed26 184 void writeAlarm( uint16_t reg, float temp );
Hotboards 0:83da47b7ed26 185 I2C _i2c;
Hotboards 0:83da47b7ed26 186 uint8_t _address;
Hotboards 0:83da47b7ed26 187 uint8_t _resolution;
Hotboards 0:83da47b7ed26 188 };
Hotboards 0:83da47b7ed26 189
Hotboards 0:83da47b7ed26 190 #endif