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
Revision 0:83da47b7ed26, committed 2016-03-17
- Comitter:
- Hotboards
- Date:
- Thu Mar 17 04:57:00 2016 +0000
- Child:
- 1:f850ee1083ba
- Commit message:
- first release (needs to be tested)
Changed in this revision
| Hotboards_temp.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Hotboards_temp.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_temp.cpp Thu Mar 17 04:57:00 2016 +0000
@@ -0,0 +1,145 @@
+/*
+ Hotboards_temp.cpp - 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.
+ Hotboards eeprom board (http://hotboards.org)
+ Created by Diego Perez, March 19, 2016.
+ Released into the public domain.
+*/
+#include "Hotboards_temp.h"
+
+#define SENSOR_ADDR (uint8_t)0x3E
+#define REG_CONFIG (uint8_t)0x01
+#define REG_ALERT_UPPER (uint8_t)0x02
+#define REG_ALERT_LOWER (uint8_t)0x03
+#define REG_CRITICAL_TEMP (uint8_t)0x04
+#define REG_TEMPERATURE (uint8_t)0x05
+#define REG_MANU_ID (uint8_t)0x06
+#define REG_DEVICE_ID (uint8_t)0x07
+#define REG_RESOLUTION (uint8_t)0x08
+
+Hotboards_temp::Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution )
+ : _i2c(i2c)
+{
+ _address = address | 0x18;
+ _resolution = resolution;
+}
+
+bool Hotboards_temp::init( void )
+{
+ bool flag = 0; // lets assume device is not here
+ uint16_t val = readReg( REG_MANU_ID );
+ if( val == 0x0054 )
+ {
+ // device is presence, default set resolution
+ writeReg( REG_RESOLUTION, _resolution );
+ // clear configuration register (alarms included)
+ writeReg( REG_CONFIG, 0x00 );
+ flag = 1;
+ }
+ return flag;
+}
+
+float Hotboards_temp::read( void )
+{
+ uint16_t val;
+ float temp;
+
+ // read the Ta register
+ val = readReg( REG_TEMPERATURE );
+
+ // small algorithm to calculate tmeperature in Celcius
+ // borrowed from https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
+ temp = val & 0x0FFF;
+ temp /= (float)16.0;
+ // check if a negative temperature
+ if( val & 0x1000 ) temp -= 256;
+
+ return temp;
+}
+
+void Hotboards_temp::setAlarms( float lower, float upper )
+{
+ // set alarm values
+ writeAlarm( REG_ALERT_UPPER, upper );
+ writeAlarm( REG_CRITICAL_TEMP, upper );
+ writeAlarm( REG_ALERT_LOWER, lower );
+
+ uint16_t val = readReg( REG_CONFIG );
+ // set alarm only in comparator mode with LAERT pin set to LOW
+ writeReg( REG_CONFIG, val | 0x0008 );
+}
+
+void Hotboards_temp::disableAlarms( void )
+{
+ uint16_t val = readReg( REG_CONFIG );
+ // just clear the Alert Output Control bit
+ writeReg( REG_CONFIG, val ^ 0x0008 );
+}
+
+void Hotboards_temp::shutdown( bool state )
+{
+ uint16_t val = readReg( REG_CONFIG );
+
+ if( state == HT_SENSOR_OFF )
+ {// shutdown, curretn under 1uA, and disable convertions
+ writeReg( REG_CONFIG, val | 0x0100 );
+ }
+ else
+ {// power on
+ writeReg( REG_CONFIG, val ^ 0x0100 );
+ }
+}
+
+void Hotboards_temp::setResolution( uint8_t resolution )
+{
+ resolution &= 0x03;
+ writeReg( REG_RESOLUTION, resolution << 8 );
+}
+
+float Hotboards_temp::CelsiusToFarenheit( float celsius )
+{
+ return celsius * (float)9.0 / (float)5.0 + 32;
+}
+
+float Hotboards_temp::FarenheitToCelsius( float farenheit )
+{
+ return ( farenheit - 32 ) * (float)5.0 / (float)9.0;
+}
+
+uint16_t Hotboards_temp::readReg( uint8_t reg )
+{
+ int val;
+ char buffer[3];
+
+ buffer[0] = reg;
+ _i2c.write( _address, buffer, 1, true );
+ _i2c.read( _address, buffer, 2, false );
+
+ val = buffer[0] << 8;
+ val |= buffer[1];
+
+ return val;
+}
+
+void Hotboards_temp::writeReg( uint8_t reg, uint16_t val )
+{
+ char buffer[3] = { reg, val >> 8, val & 0x00FF };
+
+ _i2c.write( _address, buffer, 3 );
+}
+
+void Hotboards_temp::writeAlarm( uint16_t reg, float temp )
+{
+ uint16_t val = 0x0000;
+ // check if negative temp
+ if( temp < 0 )
+ {
+ temp += (float)256.0;
+ // set sign bit
+ val = 0x1000;
+ }
+ // convert to binary
+ val |= (uint16_t)( temp *= (float)16.0 );
+ writeReg( reg, val );
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_temp.h Thu Mar 17 04:57:00 2016 +0000
@@ -0,0 +1,190 @@
+/*
+ Hotboards_temp.h - 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.
+ Hotboards eeprom board (http://hotboards.org)
+ Created by Diego Perez, March 19, 2016.
+ Released into the public domain.
+*/
+#ifndef Hotboards_Temp_h
+#define Hotboards_Temp_h
+
+#include "mbed.h"
+
+#define HT_SENSOR_OFF 0
+#define HT_SENSOR_ON 1
+
+typedef enum
+{
+ Sensor_0 = 0,
+ Sensor_1,
+ Sensor_2,
+ Sensor_3,
+ Sensor_4,
+ Sensor_5,
+ Sensor_6,
+ Sensor_7
+} _eSensors;
+
+typedef enum
+{
+ _0p5C = 0,
+ _0p25C,
+ _0p125C,
+ _0p0625C
+} _eResolution;
+
+/** Hotboards_temp class.
+ * Used to read and config Microchip MCP9808 i2c temp sensor
+ *
+ * Example:
+ * @code
+ * #include "Hotboards_temp.h"
+ *
+ * Hotboards_temp sensor( Sensor_7 );
+ *
+ * int main( void )
+ * {
+ * Wire.begin( );
+ * sensor.begin( );
+ * while(1){
+ * float val = sensor.read( );
+ * printf( "temp. %f", val );
+ * wait(1);
+ * }
+ * }
+ * @endcode
+ */
+class Hotboards_temp
+{
+ public :
+ /** Create Hotboards_temp instance
+ * @param i2c iic peripheral to use with the temp sensor
+ * @param address sensor address (A0,A1 and A2 values)
+ * @param resolution default temperature resoluion (use values from _eResolution enumeration)
+ *
+ * Example:
+ * @code
+ * // instance temp sensor with address 7 and 0.5 resolution (default)
+ * Hotboards_temp sensor( Sensor_7 );
+ * @endcode
+ */
+ Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution = _0p5C );
+
+ /** Detect if the sensor is conected and also set default resolution
+ * @return '1' if a sensor is connected
+ *
+ * Example:
+ * @code
+ * // init sensor and also check if is conected
+ * if( sensor.begin( ) == 1 ){
+ * // sensor is detected, do something
+ * }
+ * @endcode
+ */
+ bool init( void );
+
+ /** Get temperature from sensor in Celsius degrees
+ * @return The current temperature
+ *
+ * Example:
+ * @code
+ * // init temp sensor (assume sensor is connected)
+ * sensor.begin( );
+ * // read the current temperaure
+ * float temp = sensor.read( );
+ * @endcode
+ */
+ float read( void );
+
+ /** Set Tlower and Tupper values and also active ALERT output pin
+ * @param lower lower temperaure value to set ALERT pin
+ * @param upper upper temperaure value to set ALERT pin
+ *
+ * Example:
+ * @code
+ * // set lower nd upper alarm (on setup function)
+ * sensor.setAlarm( 0.0, 10.0 );
+ * // somewhere else on loop function
+ * // assume we conected pin 5 (arduino) to ALERT pin (sensor)
+ * if( readDigital(5) == 0 ){
+ * // temp value under lower valur or upper value
+ * }
+ * @endcode
+ */
+ void setAlarms( float lower, float upper );
+
+ /** Disable alarma activation, the function does not clear the previus
+ * alarm values, just disable the ALERT signal
+ *
+ * Example:
+ * @code
+ * // set lower nd upper alarm (on setup function)
+ * sensor.setAlarm( 0.0, 10.0 );
+ * // disable alarm, ALERt signal is disable
+ * sensor.disableAlarms( );
+ * @endcode
+ */
+ void disableAlarms( void );
+
+ /** Turn Off/On sensor (low power), after turn it on again, is necesary wait
+ * from 30ms to 250ms (depends on resoluion) to read a valid temperature
+ * @param state shutdown (HT_SENSOR_OFF) or turn on (HT_SENSOR_ON)
+ *
+ * Example:
+ * @code
+ * // lets assume sensor is initialized, so shutdown
+ * sensor.shutdown( HT_SENSOR_OFF );
+ * // the we can turn it on
+ * sensor.shutdown( HT_SENSOR_ON );
+ * @endcode
+ */
+ void shutdown( bool state );
+
+ /** set a new resolution, possible values are 0.0625, 0.125, 0.25 and 0.5
+ * @param resolution use values from _eResolution enumeration (_0p5C, _0p25C, _0p125C and _0p0625C)
+ *
+ * Example:
+ * @code
+ * // lets assume sensor is initialized
+ * sensor.setResolution( _0p0625C );
+ * // read temperature with the new resolution
+ * float temp = sensor.read( );
+ * @endcode
+ */
+ void setResolution( uint8_t resolution );
+
+ /** Function to convert Celsius to Farenheit degrees
+ * @param celsius temperature in celsius degrees
+ *
+ * Example:
+ * @code
+ * // read temperature in celsius degrees
+ * float tempC = sensor.read( );
+ * // convert to farenheit
+ * float tempF = sensor.CelsiusToFarenheit( tempC );
+ * @endcode
+ */
+ float CelsiusToFarenheit( float celsius );
+
+ /** Function to convert Farenheit to Celsius degrees
+ * @param farenheit temperature in farenheit degrees
+ *
+ * Example:
+ * @code
+ * // convert to celsius a previus farenheit temperature
+ * float tempC = sensor.FarenheitToCelsius( 98.5 );
+ * @endcode
+ */
+ float FarenheitToCelsius( float farenheit );
+
+ private :
+ uint16_t readReg( uint8_t reg );
+ void writeReg( uint8_t reg, uint16_t val );
+ void writeAlarm( uint16_t reg, float temp );
+ I2C _i2c;
+ uint8_t _address;
+ uint8_t _resolution;
+};
+
+#endif
Hotboards Temp