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
Hotboards_temp.h
00001 /* 00002 Hotboards_temp.h - Driver to read and control a serial (i2c) temperature sensor, The Microchip 00003 MCP9808 is the digital sensor to control, can be read it, set its resolution, shutdown and also 00004 set alarms. 00005 Hotboards eeprom board (http://hotboards.org) 00006 Created by Diego Perez, March 19, 2016. 00007 Released into the public domain. 00008 */ 00009 #ifndef Hotboards_Temp_h 00010 #define Hotboards_Temp_h 00011 00012 #include "mbed.h" 00013 00014 #define HT_SENSOR_OFF 0 00015 #define HT_SENSOR_ON 1 00016 00017 typedef enum 00018 { 00019 Sensor_0 = 0, 00020 Sensor_1, 00021 Sensor_2, 00022 Sensor_3, 00023 Sensor_4, 00024 Sensor_5, 00025 Sensor_6, 00026 Sensor_7 00027 } _eSensors; 00028 00029 typedef enum 00030 { 00031 _0p5C = 0, 00032 _0p25C, 00033 _0p125C, 00034 _0p0625C 00035 } _eResolution; 00036 00037 /** Hotboards_temp class. 00038 * Used to read and config Microchip MCP9808 i2c temp sensor 00039 * 00040 * Example: 00041 * @code 00042 * #include "Hotboards_temp.h" 00043 * 00044 * Hotboards_temp sensor( Sensor_7 ); 00045 * 00046 * int main( void ) 00047 * { 00048 * Wire.begin( ); 00049 * sensor.begin( ); 00050 * while(1){ 00051 * float val = sensor.read( ); 00052 * printf( "temp. %f", val ); 00053 * wait(1); 00054 * } 00055 * } 00056 * @endcode 00057 */ 00058 class Hotboards_temp 00059 { 00060 public : 00061 00062 /** Create Hotboards_temp instance 00063 * @param i2c iic peripheral to use with the temp sensor 00064 * @param address sensor address (A0,A1 and A2 values) 00065 * @param resolution default temperature resoluion (use values from _eResolution enumeration) 00066 * 00067 * Example: 00068 * @code 00069 * // instance temp sensor with address 7 and 0.5 resolution (default) 00070 * Hotboards_temp sensor( Sensor_7 ); 00071 * @endcode 00072 */ 00073 Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution = _0p5C ); 00074 00075 /** Detect if the sensor is conected and also set default resolution 00076 * @return '1' if a sensor is connected 00077 * 00078 * Example: 00079 * @code 00080 * // init sensor and also check if is conected 00081 * if( sensor.begin( ) == 1 ){ 00082 * // sensor is detected, do something 00083 * } 00084 * @endcode 00085 */ 00086 bool init( void ); 00087 00088 /** Get temperature from sensor in Celsius degrees 00089 * @return The current temperature 00090 * 00091 * Example: 00092 * @code 00093 * // init temp sensor (assume sensor is connected) 00094 * sensor.begin( ); 00095 * // read the current temperaure 00096 * float temp = sensor.read( ); 00097 * @endcode 00098 */ 00099 float read( void ); 00100 /** Set Tlower and Tupper values and also active ALERT output pin 00101 * @param lower lower temperaure value to set ALERT pin 00102 * @param upper upper temperaure value to set ALERT pin 00103 * 00104 * Example: 00105 * @code 00106 * // set lower nd upper alarm (on setup function) 00107 * sensor.setAlarm( 0.0, 10.0 ); 00108 * // somewhere else on loop function 00109 * // assume we conected pin 5 (arduino) to ALERT pin (sensor) 00110 * if( readDigital(5) == 0 ){ 00111 * // temp value under lower valur or upper value 00112 * } 00113 * @endcode 00114 */ 00115 void setAlarms( float lower, float upper ); 00116 00117 /** Disable alarma activation, the function does not clear the previus 00118 * alarm values, just disable the ALERT signal 00119 * 00120 * Example: 00121 * @code 00122 * // set lower nd upper alarm (on setup function) 00123 * sensor.setAlarm( 0.0, 10.0 ); 00124 * // disable alarm, ALERt signal is disable 00125 * sensor.disableAlarms( ); 00126 * @endcode 00127 */ 00128 void disableAlarms( void ); 00129 00130 /** Turn Off/On sensor (low power), after turn it on again, is necesary wait 00131 * from 30ms to 250ms (depends on resoluion) to read a valid temperature 00132 * @param state shutdown (HT_SENSOR_OFF) or turn on (HT_SENSOR_ON) 00133 * 00134 * Example: 00135 * @code 00136 * // lets assume sensor is initialized, so shutdown 00137 * sensor.shutdown( HT_SENSOR_OFF ); 00138 * // the we can turn it on 00139 * sensor.shutdown( HT_SENSOR_ON ); 00140 * @endcode 00141 */ 00142 void shutdown( bool state ); 00143 00144 /** set a new resolution, possible values are 0.0625, 0.125, 0.25 and 0.5 00145 * @param resolution use values from _eResolution enumeration (_0p5C, _0p25C, _0p125C and _0p0625C) 00146 * 00147 * Example: 00148 * @code 00149 * // lets assume sensor is initialized 00150 * sensor.setResolution( _0p0625C ); 00151 * // read temperature with the new resolution 00152 * float temp = sensor.read( ); 00153 * @endcode 00154 */ 00155 void setResolution( uint8_t resolution ); 00156 00157 /** Function to convert Celsius to Farenheit degrees 00158 * @param celsius temperature in celsius degrees 00159 * 00160 * Example: 00161 * @code 00162 * // read temperature in celsius degrees 00163 * float tempC = sensor.read( ); 00164 * // convert to farenheit 00165 * float tempF = sensor.CelsiusToFarenheit( tempC ); 00166 * @endcode 00167 */ 00168 float CelsiusToFarenheit( float celsius ); 00169 00170 /** Function to convert Farenheit to Celsius degrees 00171 * @param farenheit temperature in farenheit degrees 00172 * 00173 * Example: 00174 * @code 00175 * // convert to celsius a previus farenheit temperature 00176 * float tempC = sensor.FarenheitToCelsius( 98.5 ); 00177 * @endcode 00178 */ 00179 float FarenheitToCelsius( float farenheit ); 00180 00181 private : 00182 uint16_t readReg( uint8_t reg ); 00183 void writeReg( uint8_t reg, uint16_t val ); 00184 void writeAlarm( uint16_t reg, float temp ); 00185 I2C _i2c; 00186 uint8_t _address; 00187 uint8_t _resolution; 00188 }; 00189 00190 #endif
Generated on Sun Jul 17 2022 03:25:46 by 1.7.2