Simple library for getting temperature from MCP9808
Dependents: Projet_MBED_TEMPERATURE Nucleo_i2c_master
Revision 0:f8e8cccfd476, committed 2015-10-11
- Comitter:
- stumpi
- Date:
- Sun Oct 11 18:40:27 2015 +0000
- Commit message:
- First init
Changed in this revision
MCP9808.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP9808.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r f8e8cccfd476 MCP9808.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP9808.cpp Sun Oct 11 18:40:27 2015 +0000 @@ -0,0 +1,41 @@ +#include "MCP9808.h" + +MCP9808::MCP9808(PinName sda, PinName scl) : i2c(sda, scl) + { + } + +// read temperature from MCP9808 +float MCP9808::readTemp() + { + data_write[0] = MCP9808_REG_TEMP; + i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop + i2c.read(MCP9808_ADDR, data_read, 2, 0); + + if(data_read[0] & 0xE0) { + data_read[0] = data_read[0] & 0x1F; // clear flag bits + } + if((data_read[0] & 0x10) == 0x10) { // < 0 C + data_read[0] = data_read[0] & 0x0F; + tempval = 256 - (data_read[0] * 16) + (data_read[1] / 16.0); + tempval = tempval * -1; + } else { // > 0 C + tempval = (data_read[0] * 16) + (data_read[1] / 16.0); + } + return tempval; + } + +void MCP9808::goSleep() +{ + data_write[0] = MCP9808_REG_CONF; + data_write[1] = 0x01; // config msb + data_write[2] = 0x00; // config lsb + int status = i2c.write(MCP9808_ADDR, data_write, 3, 0); + } + +void MCP9808::wakeUp() +{ + data_write[0] = MCP9808_REG_CONF; + data_write[1] = 0x00; // config msb + data_write[2] = 0x00; // config lsb + int status = i2c.write(MCP9808_ADDR, data_write, 3, 0); + } \ No newline at end of file
diff -r 000000000000 -r f8e8cccfd476 MCP9808.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP9808.h Sun Oct 11 18:40:27 2015 +0000 @@ -0,0 +1,51 @@ +/** mbded library for driving the MCP9808 Temperature sensor +* datasheet link : http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf +* breakout : Adafruit MCP9808 Breakout board +* Christoph Suter (code based on Richard Kadrmas Nucleo-MCP9808 sample) +*/ + + +#include "mbed.h" + + +#ifndef MBED_DS3231_H +#define MBED_DS3231_H +//MCP9808 8 bit adress +#define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1 + +#define MCP9808_REG_TEMP (0x05) // Temperature Register +#define MCP9808_REG_CONF (0x01) // Configuration Register + +/* Interface to MAXIM DS3231 RTC */ +class MCP9808 + {public : + /** Create an instance of the MCP9808 connected to specfied I2C pins + * + * @param sda The I2C data pin + * @param scl The I2C clock pin + */ + MCP9808(PinName sda, PinName scl); + + /** Read the temperature + * + * @return The temperature + */ + float readTemp(); + + /** Go to sleep + */ + void goSleep(); + + /** Wake up + */ + void wakeUp(); + + + private : + I2C i2c; + char data_write[3]; + char data_read[2]; + float tempval; + }; + +#endif \ No newline at end of file