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 1:f850ee1083ba, committed 2016-03-22
- Comitter:
- Hotboards
- Date:
- Tue Mar 22 20:39:49 2016 +0000
- Parent:
- 0:83da47b7ed26
- Commit message:
- solved some issues related to read/write bit
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 |
diff -r 83da47b7ed26 -r f850ee1083ba Hotboards_temp.cpp --- a/Hotboards_temp.cpp Thu Mar 17 04:57:00 2016 +0000 +++ b/Hotboards_temp.cpp Tue Mar 22 20:39:49 2016 +0000 @@ -21,7 +21,7 @@ Hotboards_temp::Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution ) : _i2c(i2c) { - _address = address | 0x18; + _address = (address<<1)|0x30; _resolution = resolution; } @@ -32,7 +32,7 @@ if( val == 0x0054 ) { // device is presence, default set resolution - writeReg( REG_RESOLUTION, _resolution ); + writeReg( REG_RESOLUTION, _resolution); // clear configuration register (alarms included) writeReg( REG_CONFIG, 0x00 ); flag = 1; @@ -40,6 +40,7 @@ return flag; } + float Hotboards_temp::read( void ) { uint16_t val; @@ -51,8 +52,9 @@ // 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 + //check if a negative temperature if( val & 0x1000 ) temp -= 256; return temp; @@ -109,11 +111,13 @@ uint16_t Hotboards_temp::readReg( uint8_t reg ) { - int val; - char buffer[3]; - + uint16_t val; + char buffer[2]; + + _address &= 0x3E; buffer[0] = reg; _i2c.write( _address, buffer, 1, true ); + _address |= 0x01; _i2c.read( _address, buffer, 2, false ); val = buffer[0] << 8; @@ -125,7 +129,7 @@ void Hotboards_temp::writeReg( uint8_t reg, uint16_t val ) { char buffer[3] = { reg, val >> 8, val & 0x00FF }; - + _address &= 0x3E; _i2c.write( _address, buffer, 3 ); }
diff -r 83da47b7ed26 -r f850ee1083ba Hotboards_temp.h --- a/Hotboards_temp.h Thu Mar 17 04:57:00 2016 +0000 +++ b/Hotboards_temp.h Tue Mar 22 20:39:49 2016 +0000 @@ -58,6 +58,7 @@ 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) @@ -96,7 +97,6 @@ * @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