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.cpp - 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 #include "Hotboards_temp.h"
Hotboards 0:83da47b7ed26 10
Hotboards 0:83da47b7ed26 11 #define SENSOR_ADDR (uint8_t)0x3E
Hotboards 0:83da47b7ed26 12 #define REG_CONFIG (uint8_t)0x01
Hotboards 0:83da47b7ed26 13 #define REG_ALERT_UPPER (uint8_t)0x02
Hotboards 0:83da47b7ed26 14 #define REG_ALERT_LOWER (uint8_t)0x03
Hotboards 0:83da47b7ed26 15 #define REG_CRITICAL_TEMP (uint8_t)0x04
Hotboards 0:83da47b7ed26 16 #define REG_TEMPERATURE (uint8_t)0x05
Hotboards 0:83da47b7ed26 17 #define REG_MANU_ID (uint8_t)0x06
Hotboards 0:83da47b7ed26 18 #define REG_DEVICE_ID (uint8_t)0x07
Hotboards 0:83da47b7ed26 19 #define REG_RESOLUTION (uint8_t)0x08
Hotboards 0:83da47b7ed26 20
Hotboards 0:83da47b7ed26 21 Hotboards_temp::Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution )
Hotboards 0:83da47b7ed26 22 : _i2c(i2c)
Hotboards 0:83da47b7ed26 23 {
Hotboards 0:83da47b7ed26 24 _address = address | 0x18;
Hotboards 0:83da47b7ed26 25 _resolution = resolution;
Hotboards 0:83da47b7ed26 26 }
Hotboards 0:83da47b7ed26 27
Hotboards 0:83da47b7ed26 28 bool Hotboards_temp::init( void )
Hotboards 0:83da47b7ed26 29 {
Hotboards 0:83da47b7ed26 30 bool flag = 0; // lets assume device is not here
Hotboards 0:83da47b7ed26 31 uint16_t val = readReg( REG_MANU_ID );
Hotboards 0:83da47b7ed26 32 if( val == 0x0054 )
Hotboards 0:83da47b7ed26 33 {
Hotboards 0:83da47b7ed26 34 // device is presence, default set resolution
Hotboards 0:83da47b7ed26 35 writeReg( REG_RESOLUTION, _resolution );
Hotboards 0:83da47b7ed26 36 // clear configuration register (alarms included)
Hotboards 0:83da47b7ed26 37 writeReg( REG_CONFIG, 0x00 );
Hotboards 0:83da47b7ed26 38 flag = 1;
Hotboards 0:83da47b7ed26 39 }
Hotboards 0:83da47b7ed26 40 return flag;
Hotboards 0:83da47b7ed26 41 }
Hotboards 0:83da47b7ed26 42
Hotboards 0:83da47b7ed26 43 float Hotboards_temp::read( void )
Hotboards 0:83da47b7ed26 44 {
Hotboards 0:83da47b7ed26 45 uint16_t val;
Hotboards 0:83da47b7ed26 46 float temp;
Hotboards 0:83da47b7ed26 47
Hotboards 0:83da47b7ed26 48 // read the Ta register
Hotboards 0:83da47b7ed26 49 val = readReg( REG_TEMPERATURE );
Hotboards 0:83da47b7ed26 50
Hotboards 0:83da47b7ed26 51 // small algorithm to calculate tmeperature in Celcius
Hotboards 0:83da47b7ed26 52 // borrowed from https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
Hotboards 0:83da47b7ed26 53 temp = val & 0x0FFF;
Hotboards 0:83da47b7ed26 54 temp /= (float)16.0;
Hotboards 0:83da47b7ed26 55 // check if a negative temperature
Hotboards 0:83da47b7ed26 56 if( val & 0x1000 ) temp -= 256;
Hotboards 0:83da47b7ed26 57
Hotboards 0:83da47b7ed26 58 return temp;
Hotboards 0:83da47b7ed26 59 }
Hotboards 0:83da47b7ed26 60
Hotboards 0:83da47b7ed26 61 void Hotboards_temp::setAlarms( float lower, float upper )
Hotboards 0:83da47b7ed26 62 {
Hotboards 0:83da47b7ed26 63 // set alarm values
Hotboards 0:83da47b7ed26 64 writeAlarm( REG_ALERT_UPPER, upper );
Hotboards 0:83da47b7ed26 65 writeAlarm( REG_CRITICAL_TEMP, upper );
Hotboards 0:83da47b7ed26 66 writeAlarm( REG_ALERT_LOWER, lower );
Hotboards 0:83da47b7ed26 67
Hotboards 0:83da47b7ed26 68 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 69 // set alarm only in comparator mode with LAERT pin set to LOW
Hotboards 0:83da47b7ed26 70 writeReg( REG_CONFIG, val | 0x0008 );
Hotboards 0:83da47b7ed26 71 }
Hotboards 0:83da47b7ed26 72
Hotboards 0:83da47b7ed26 73 void Hotboards_temp::disableAlarms( void )
Hotboards 0:83da47b7ed26 74 {
Hotboards 0:83da47b7ed26 75 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 76 // just clear the Alert Output Control bit
Hotboards 0:83da47b7ed26 77 writeReg( REG_CONFIG, val ^ 0x0008 );
Hotboards 0:83da47b7ed26 78 }
Hotboards 0:83da47b7ed26 79
Hotboards 0:83da47b7ed26 80 void Hotboards_temp::shutdown( bool state )
Hotboards 0:83da47b7ed26 81 {
Hotboards 0:83da47b7ed26 82 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 83
Hotboards 0:83da47b7ed26 84 if( state == HT_SENSOR_OFF )
Hotboards 0:83da47b7ed26 85 {// shutdown, curretn under 1uA, and disable convertions
Hotboards 0:83da47b7ed26 86 writeReg( REG_CONFIG, val | 0x0100 );
Hotboards 0:83da47b7ed26 87 }
Hotboards 0:83da47b7ed26 88 else
Hotboards 0:83da47b7ed26 89 {// power on
Hotboards 0:83da47b7ed26 90 writeReg( REG_CONFIG, val ^ 0x0100 );
Hotboards 0:83da47b7ed26 91 }
Hotboards 0:83da47b7ed26 92 }
Hotboards 0:83da47b7ed26 93
Hotboards 0:83da47b7ed26 94 void Hotboards_temp::setResolution( uint8_t resolution )
Hotboards 0:83da47b7ed26 95 {
Hotboards 0:83da47b7ed26 96 resolution &= 0x03;
Hotboards 0:83da47b7ed26 97 writeReg( REG_RESOLUTION, resolution << 8 );
Hotboards 0:83da47b7ed26 98 }
Hotboards 0:83da47b7ed26 99
Hotboards 0:83da47b7ed26 100 float Hotboards_temp::CelsiusToFarenheit( float celsius )
Hotboards 0:83da47b7ed26 101 {
Hotboards 0:83da47b7ed26 102 return celsius * (float)9.0 / (float)5.0 + 32;
Hotboards 0:83da47b7ed26 103 }
Hotboards 0:83da47b7ed26 104
Hotboards 0:83da47b7ed26 105 float Hotboards_temp::FarenheitToCelsius( float farenheit )
Hotboards 0:83da47b7ed26 106 {
Hotboards 0:83da47b7ed26 107 return ( farenheit - 32 ) * (float)5.0 / (float)9.0;
Hotboards 0:83da47b7ed26 108 }
Hotboards 0:83da47b7ed26 109
Hotboards 0:83da47b7ed26 110 uint16_t Hotboards_temp::readReg( uint8_t reg )
Hotboards 0:83da47b7ed26 111 {
Hotboards 0:83da47b7ed26 112 int val;
Hotboards 0:83da47b7ed26 113 char buffer[3];
Hotboards 0:83da47b7ed26 114
Hotboards 0:83da47b7ed26 115 buffer[0] = reg;
Hotboards 0:83da47b7ed26 116 _i2c.write( _address, buffer, 1, true );
Hotboards 0:83da47b7ed26 117 _i2c.read( _address, buffer, 2, false );
Hotboards 0:83da47b7ed26 118
Hotboards 0:83da47b7ed26 119 val = buffer[0] << 8;
Hotboards 0:83da47b7ed26 120 val |= buffer[1];
Hotboards 0:83da47b7ed26 121
Hotboards 0:83da47b7ed26 122 return val;
Hotboards 0:83da47b7ed26 123 }
Hotboards 0:83da47b7ed26 124
Hotboards 0:83da47b7ed26 125 void Hotboards_temp::writeReg( uint8_t reg, uint16_t val )
Hotboards 0:83da47b7ed26 126 {
Hotboards 0:83da47b7ed26 127 char buffer[3] = { reg, val >> 8, val & 0x00FF };
Hotboards 0:83da47b7ed26 128
Hotboards 0:83da47b7ed26 129 _i2c.write( _address, buffer, 3 );
Hotboards 0:83da47b7ed26 130 }
Hotboards 0:83da47b7ed26 131
Hotboards 0:83da47b7ed26 132 void Hotboards_temp::writeAlarm( uint16_t reg, float temp )
Hotboards 0:83da47b7ed26 133 {
Hotboards 0:83da47b7ed26 134 uint16_t val = 0x0000;
Hotboards 0:83da47b7ed26 135 // check if negative temp
Hotboards 0:83da47b7ed26 136 if( temp < 0 )
Hotboards 0:83da47b7ed26 137 {
Hotboards 0:83da47b7ed26 138 temp += (float)256.0;
Hotboards 0:83da47b7ed26 139 // set sign bit
Hotboards 0:83da47b7ed26 140 val = 0x1000;
Hotboards 0:83da47b7ed26 141 }
Hotboards 0:83da47b7ed26 142 // convert to binary
Hotboards 0:83da47b7ed26 143 val |= (uint16_t)( temp *= (float)16.0 );
Hotboards 0:83da47b7ed26 144 writeReg( reg, val );
Hotboards 0:83da47b7ed26 145 }