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:
Tue Mar 22 20:39:49 2016 +0000
Revision:
1:f850ee1083ba
Parent:
0:83da47b7ed26
solved some issues related to read/write bit

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 1:f850ee1083ba 24 _address = (address<<1)|0x30;
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 1:f850ee1083ba 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 1:f850ee1083ba 43
Hotboards 0:83da47b7ed26 44 float Hotboards_temp::read( void )
Hotboards 0:83da47b7ed26 45 {
Hotboards 0:83da47b7ed26 46 uint16_t val;
Hotboards 0:83da47b7ed26 47 float temp;
Hotboards 0:83da47b7ed26 48
Hotboards 0:83da47b7ed26 49 // read the Ta register
Hotboards 0:83da47b7ed26 50 val = readReg( REG_TEMPERATURE );
Hotboards 0:83da47b7ed26 51
Hotboards 0:83da47b7ed26 52 // small algorithm to calculate tmeperature in Celcius
Hotboards 0:83da47b7ed26 53 // borrowed from https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
Hotboards 0:83da47b7ed26 54 temp = val & 0x0FFF;
Hotboards 1:f850ee1083ba 55
Hotboards 0:83da47b7ed26 56 temp /= (float)16.0;
Hotboards 1:f850ee1083ba 57 //check if a negative temperature
Hotboards 0:83da47b7ed26 58 if( val & 0x1000 ) temp -= 256;
Hotboards 0:83da47b7ed26 59
Hotboards 0:83da47b7ed26 60 return temp;
Hotboards 0:83da47b7ed26 61 }
Hotboards 0:83da47b7ed26 62
Hotboards 0:83da47b7ed26 63 void Hotboards_temp::setAlarms( float lower, float upper )
Hotboards 0:83da47b7ed26 64 {
Hotboards 0:83da47b7ed26 65 // set alarm values
Hotboards 0:83da47b7ed26 66 writeAlarm( REG_ALERT_UPPER, upper );
Hotboards 0:83da47b7ed26 67 writeAlarm( REG_CRITICAL_TEMP, upper );
Hotboards 0:83da47b7ed26 68 writeAlarm( REG_ALERT_LOWER, lower );
Hotboards 0:83da47b7ed26 69
Hotboards 0:83da47b7ed26 70 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 71 // set alarm only in comparator mode with LAERT pin set to LOW
Hotboards 0:83da47b7ed26 72 writeReg( REG_CONFIG, val | 0x0008 );
Hotboards 0:83da47b7ed26 73 }
Hotboards 0:83da47b7ed26 74
Hotboards 0:83da47b7ed26 75 void Hotboards_temp::disableAlarms( void )
Hotboards 0:83da47b7ed26 76 {
Hotboards 0:83da47b7ed26 77 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 78 // just clear the Alert Output Control bit
Hotboards 0:83da47b7ed26 79 writeReg( REG_CONFIG, val ^ 0x0008 );
Hotboards 0:83da47b7ed26 80 }
Hotboards 0:83da47b7ed26 81
Hotboards 0:83da47b7ed26 82 void Hotboards_temp::shutdown( bool state )
Hotboards 0:83da47b7ed26 83 {
Hotboards 0:83da47b7ed26 84 uint16_t val = readReg( REG_CONFIG );
Hotboards 0:83da47b7ed26 85
Hotboards 0:83da47b7ed26 86 if( state == HT_SENSOR_OFF )
Hotboards 0:83da47b7ed26 87 {// shutdown, curretn under 1uA, and disable convertions
Hotboards 0:83da47b7ed26 88 writeReg( REG_CONFIG, val | 0x0100 );
Hotboards 0:83da47b7ed26 89 }
Hotboards 0:83da47b7ed26 90 else
Hotboards 0:83da47b7ed26 91 {// power on
Hotboards 0:83da47b7ed26 92 writeReg( REG_CONFIG, val ^ 0x0100 );
Hotboards 0:83da47b7ed26 93 }
Hotboards 0:83da47b7ed26 94 }
Hotboards 0:83da47b7ed26 95
Hotboards 0:83da47b7ed26 96 void Hotboards_temp::setResolution( uint8_t resolution )
Hotboards 0:83da47b7ed26 97 {
Hotboards 0:83da47b7ed26 98 resolution &= 0x03;
Hotboards 0:83da47b7ed26 99 writeReg( REG_RESOLUTION, resolution << 8 );
Hotboards 0:83da47b7ed26 100 }
Hotboards 0:83da47b7ed26 101
Hotboards 0:83da47b7ed26 102 float Hotboards_temp::CelsiusToFarenheit( float celsius )
Hotboards 0:83da47b7ed26 103 {
Hotboards 0:83da47b7ed26 104 return celsius * (float)9.0 / (float)5.0 + 32;
Hotboards 0:83da47b7ed26 105 }
Hotboards 0:83da47b7ed26 106
Hotboards 0:83da47b7ed26 107 float Hotboards_temp::FarenheitToCelsius( float farenheit )
Hotboards 0:83da47b7ed26 108 {
Hotboards 0:83da47b7ed26 109 return ( farenheit - 32 ) * (float)5.0 / (float)9.0;
Hotboards 0:83da47b7ed26 110 }
Hotboards 0:83da47b7ed26 111
Hotboards 0:83da47b7ed26 112 uint16_t Hotboards_temp::readReg( uint8_t reg )
Hotboards 0:83da47b7ed26 113 {
Hotboards 1:f850ee1083ba 114 uint16_t val;
Hotboards 1:f850ee1083ba 115 char buffer[2];
Hotboards 1:f850ee1083ba 116
Hotboards 1:f850ee1083ba 117 _address &= 0x3E;
Hotboards 0:83da47b7ed26 118 buffer[0] = reg;
Hotboards 0:83da47b7ed26 119 _i2c.write( _address, buffer, 1, true );
Hotboards 1:f850ee1083ba 120 _address |= 0x01;
Hotboards 0:83da47b7ed26 121 _i2c.read( _address, buffer, 2, false );
Hotboards 0:83da47b7ed26 122
Hotboards 0:83da47b7ed26 123 val = buffer[0] << 8;
Hotboards 0:83da47b7ed26 124 val |= buffer[1];
Hotboards 0:83da47b7ed26 125
Hotboards 0:83da47b7ed26 126 return val;
Hotboards 0:83da47b7ed26 127 }
Hotboards 0:83da47b7ed26 128
Hotboards 0:83da47b7ed26 129 void Hotboards_temp::writeReg( uint8_t reg, uint16_t val )
Hotboards 0:83da47b7ed26 130 {
Hotboards 0:83da47b7ed26 131 char buffer[3] = { reg, val >> 8, val & 0x00FF };
Hotboards 1:f850ee1083ba 132 _address &= 0x3E;
Hotboards 0:83da47b7ed26 133 _i2c.write( _address, buffer, 3 );
Hotboards 0:83da47b7ed26 134 }
Hotboards 0:83da47b7ed26 135
Hotboards 0:83da47b7ed26 136 void Hotboards_temp::writeAlarm( uint16_t reg, float temp )
Hotboards 0:83da47b7ed26 137 {
Hotboards 0:83da47b7ed26 138 uint16_t val = 0x0000;
Hotboards 0:83da47b7ed26 139 // check if negative temp
Hotboards 0:83da47b7ed26 140 if( temp < 0 )
Hotboards 0:83da47b7ed26 141 {
Hotboards 0:83da47b7ed26 142 temp += (float)256.0;
Hotboards 0:83da47b7ed26 143 // set sign bit
Hotboards 0:83da47b7ed26 144 val = 0x1000;
Hotboards 0:83da47b7ed26 145 }
Hotboards 0:83da47b7ed26 146 // convert to binary
Hotboards 0:83da47b7ed26 147 val |= (uint16_t)( temp *= (float)16.0 );
Hotboards 0:83da47b7ed26 148 writeReg( reg, val );
Hotboards 0:83da47b7ed26 149 }