MCP9808 Digital temperature sensor
MCP9808.cpp@2:4f8e1fb852d3, 2017-01-20 (annotated)
- Committer:
- Jmfox24
- Date:
- Fri Jan 20 19:48:45 2017 +0000
- Revision:
- 2:4f8e1fb852d3
- Parent:
- 0:f06580426072
add sensor detection function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jmfox24 | 0:f06580426072 | 1 | #include <stdint.h> |
Jmfox24 | 0:f06580426072 | 2 | #include "mbed.h" |
Jmfox24 | 0:f06580426072 | 3 | #include "MCP9808.hpp" |
Jmfox24 | 0:f06580426072 | 4 | |
Jmfox24 | 0:f06580426072 | 5 | MCP9808::MCP9808(PinName sda, PinName scl, int addr) : |
Jmfox24 | 0:f06580426072 | 6 | _i2c(sda, scl), _addr(addr) {} |
Jmfox24 | 0:f06580426072 | 7 | |
Jmfox24 | 0:f06580426072 | 8 | MCP9808:: ~MCP9808() {} |
Jmfox24 | 0:f06580426072 | 9 | |
Jmfox24 | 2:4f8e1fb852d3 | 10 | bool MCP9808::is_detected() |
Jmfox24 | 2:4f8e1fb852d3 | 11 | { |
Jmfox24 | 2:4f8e1fb852d3 | 12 | uint16_t v = 0; |
Jmfox24 | 2:4f8e1fb852d3 | 13 | |
Jmfox24 | 2:4f8e1fb852d3 | 14 | /* check MFG ID */ |
Jmfox24 | 2:4f8e1fb852d3 | 15 | if (reg_read(MCP9808_REG_MFG_ID, &v)) { |
Jmfox24 | 2:4f8e1fb852d3 | 16 | return false; |
Jmfox24 | 2:4f8e1fb852d3 | 17 | } |
Jmfox24 | 2:4f8e1fb852d3 | 18 | if (v != MCP9808_MFG_ID) { |
Jmfox24 | 2:4f8e1fb852d3 | 19 | return false; |
Jmfox24 | 2:4f8e1fb852d3 | 20 | } |
Jmfox24 | 2:4f8e1fb852d3 | 21 | |
Jmfox24 | 2:4f8e1fb852d3 | 22 | /* check DEV ID */ |
Jmfox24 | 2:4f8e1fb852d3 | 23 | if (reg_read(MCP9808_REG_DEV_ID, &v)) { |
Jmfox24 | 2:4f8e1fb852d3 | 24 | return false; |
Jmfox24 | 2:4f8e1fb852d3 | 25 | } |
Jmfox24 | 2:4f8e1fb852d3 | 26 | if (v != MCP9808_DEV_ID) { |
Jmfox24 | 2:4f8e1fb852d3 | 27 | return false; |
Jmfox24 | 2:4f8e1fb852d3 | 28 | } |
Jmfox24 | 2:4f8e1fb852d3 | 29 | |
Jmfox24 | 2:4f8e1fb852d3 | 30 | return true; |
Jmfox24 | 2:4f8e1fb852d3 | 31 | } |
Jmfox24 | 2:4f8e1fb852d3 | 32 | |
Jmfox24 | 0:f06580426072 | 33 | int MCP9808::sleep() |
Jmfox24 | 0:f06580426072 | 34 | { |
Jmfox24 | 0:f06580426072 | 35 | return set_cfg_flag(MCP9808_CFG_FLAG_SHDN_ENABLED, true); |
Jmfox24 | 0:f06580426072 | 36 | } |
Jmfox24 | 0:f06580426072 | 37 | |
Jmfox24 | 0:f06580426072 | 38 | int MCP9808::wake() |
Jmfox24 | 0:f06580426072 | 39 | { |
Jmfox24 | 0:f06580426072 | 40 | return set_cfg_flag(MCP9808_CFG_FLAG_SHDN_ENABLED, false); |
Jmfox24 | 0:f06580426072 | 41 | } |
Jmfox24 | 0:f06580426072 | 42 | |
Jmfox24 | 0:f06580426072 | 43 | float MCP9808::get_temperature() |
Jmfox24 | 0:f06580426072 | 44 | { |
Jmfox24 | 0:f06580426072 | 45 | uint16_t v = 0; |
Jmfox24 | 0:f06580426072 | 46 | float t = 0.0f; |
Jmfox24 | 0:f06580426072 | 47 | |
Jmfox24 | 0:f06580426072 | 48 | reg_read(MCP9808_REG_TEMP, &v); |
Jmfox24 | 0:f06580426072 | 49 | t = v & 0x0FFF; |
Jmfox24 | 0:f06580426072 | 50 | t /= 16.0f; |
Jmfox24 | 0:f06580426072 | 51 | if (v & 0x1000) { |
Jmfox24 | 0:f06580426072 | 52 | t -= 256.0f; |
Jmfox24 | 0:f06580426072 | 53 | } |
Jmfox24 | 0:f06580426072 | 54 | return t; |
Jmfox24 | 0:f06580426072 | 55 | } |
Jmfox24 | 0:f06580426072 | 56 | |
Jmfox24 | 0:f06580426072 | 57 | int MCP9808::reg_read(uint8_t reg, uint16_t *value) |
Jmfox24 | 0:f06580426072 | 58 | { |
Jmfox24 | 0:f06580426072 | 59 | uint8_t buffer[sizeof(*value)] = { reg, 0x00 }; |
Jmfox24 | 0:f06580426072 | 60 | _i2c.write(_addr, (const char *)buffer, 1, true); |
Jmfox24 | 0:f06580426072 | 61 | _i2c.read(_addr, (char *)buffer, sizeof(buffer), false); |
Jmfox24 | 0:f06580426072 | 62 | *value = (buffer[0] << 8) | buffer[1]; |
Jmfox24 | 0:f06580426072 | 63 | return 0; |
Jmfox24 | 0:f06580426072 | 64 | } |
Jmfox24 | 0:f06580426072 | 65 | |
Jmfox24 | 0:f06580426072 | 66 | int MCP9808::reg_write(uint8_t reg, uint16_t value) |
Jmfox24 | 0:f06580426072 | 67 | { |
Jmfox24 | 0:f06580426072 | 68 | uint8_t buffer[] = { reg, (value >> 8), (value & 0xFF) }; |
Jmfox24 | 0:f06580426072 | 69 | _i2c.write(_addr, (const char *)buffer, sizeof(buffer), false); |
Jmfox24 | 0:f06580426072 | 70 | return 0; |
Jmfox24 | 0:f06580426072 | 71 | } |
Jmfox24 | 0:f06580426072 | 72 | |
Jmfox24 | 0:f06580426072 | 73 | int MCP9808::set_cfg_flag(uint16_t flag, bool v) |
Jmfox24 | 0:f06580426072 | 74 | { |
Jmfox24 | 0:f06580426072 | 75 | uint16_t value = 0x00; |
Jmfox24 | 0:f06580426072 | 76 | reg_read(MCP9808_REG_CFG, &value); |
Jmfox24 | 0:f06580426072 | 77 | if (v) { |
Jmfox24 | 0:f06580426072 | 78 | value |= flag; |
Jmfox24 | 0:f06580426072 | 79 | } else { |
Jmfox24 | 0:f06580426072 | 80 | value &= ~flag; |
Jmfox24 | 0:f06580426072 | 81 | } |
Jmfox24 | 0:f06580426072 | 82 | return reg_write(MCP9808_REG_CFG, value); |
Jmfox24 | 0:f06580426072 | 83 | } |