MCP9808 Digital temperature sensor

Committer:
Jmfox24
Date:
Fri Jan 20 19:48:45 2017 +0000
Revision:
2:4f8e1fb852d3
Parent:
1:7e98ef2bd303
add sensor detection function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jmfox24 0:f06580426072 1 #ifndef MCP9808_HPP
Jmfox24 0:f06580426072 2 #define MCP9808_HPP
Jmfox24 0:f06580426072 3
Jmfox24 0:f06580426072 4 #include <stdint.h>
Jmfox24 0:f06580426072 5 #include "mbed.h"
Jmfox24 0:f06580426072 6
Jmfox24 0:f06580426072 7 /** A driver interface for the Microchip MCP9808 digital temperature sensor
Jmfox24 0:f06580426072 8 *
Jmfox24 0:f06580426072 9 * Example:
Jmfox24 0:f06580426072 10 * @code
Jmfox24 0:f06580426072 11 *
Jmfox24 1:7e98ef2bd303 12 * #include "mbed.h"
Jmfox24 0:f06580426072 13 *
Jmfox24 0:f06580426072 14 * MCP9808 sensor(I2C_SDA, I2C_SCL, 0x30);
Jmfox24 0:f06580426072 15 *
Jmfox24 0:f06580426072 16 * int main() {
Jmfox24 0:f06580426072 17 * while (true) {
Jmfox24 0:f06580426072 18 * sensor.wake();
Jmfox24 0:f06580426072 19 * osDelay(250);
Jmfox24 0:f06580426072 20 * printf("Current temperature: %f °C\r\n", sensor.get_temperature());
Jmfox24 0:f06580426072 21 * sensor.sleep();
Jmfox24 0:f06580426072 22 * osDelay(1000);
Jmfox24 0:f06580426072 23 * }
Jmfox24 0:f06580426072 24 * }
Jmfox24 0:f06580426072 25 * @endcode
Jmfox24 0:f06580426072 26 */
Jmfox24 0:f06580426072 27 class MCP9808
Jmfox24 0:f06580426072 28 {
Jmfox24 0:f06580426072 29 public:
Jmfox24 0:f06580426072 30
Jmfox24 0:f06580426072 31 /** Register definitions
Jmfox24 0:f06580426072 32 */
Jmfox24 0:f06580426072 33 enum Register {
Jmfox24 0:f06580426072 34 MCP9808_REG_RFU = 0x00,
Jmfox24 0:f06580426072 35 MCP9808_REG_CFG = 0x01,
Jmfox24 0:f06580426072 36 MCP9808_REG_ALERT_TEMP_UPPER = 0x02,
Jmfox24 0:f06580426072 37 MCP9808_REG_ALERT_TEMP_LOWER = 0x03,
Jmfox24 0:f06580426072 38 MCP9808_REG_CRIT_TEMP = 0x04,
Jmfox24 0:f06580426072 39 MCP9808_REG_TEMP = 0x05,
Jmfox24 0:f06580426072 40 MCP9808_REG_MFG_ID = 0x06,
Jmfox24 0:f06580426072 41 MCP9808_REG_DEV_ID = 0x07,
Jmfox24 0:f06580426072 42 MCP9808_REG_RES = 0x08,
Jmfox24 0:f06580426072 43 };
Jmfox24 0:f06580426072 44
Jmfox24 0:f06580426072 45 /** Config register flags
Jmfox24 0:f06580426072 46 */
Jmfox24 0:f06580426072 47 enum ConfigFlag {
Jmfox24 0:f06580426072 48 MCP9808_CFG_FLAG_ALERT_MODE_INT = (1 << 0),
Jmfox24 0:f06580426072 49 MCP9808_CFG_FLAG_ALERT_POL_HIGH = (1 << 1),
Jmfox24 0:f06580426072 50 MCP9808_CFG_FLAG_ALERT_CRIT_ONLY = (1 << 2),
Jmfox24 0:f06580426072 51 MCP9808_CFG_FLAG_ALERT_ENABLE = (1 << 3),
Jmfox24 0:f06580426072 52 MCP9808_CFG_FLAG_ALERT_ASSERT_ENABLE = (1 << 4),
Jmfox24 0:f06580426072 53 MCP9808_CFG_FLAG_INT_CLR = (1 << 5),
Jmfox24 0:f06580426072 54 MCP9808_CFG_FLAG_WIN_LOCKED = (1 << 6),
Jmfox24 0:f06580426072 55 MCP9808_CFG_FLAG_CRIT_LOCKED = (1 << 7),
Jmfox24 0:f06580426072 56 MCP9808_CFG_FLAG_SHDN_ENABLED = (1 << 8),
Jmfox24 0:f06580426072 57 };
Jmfox24 0:f06580426072 58
Jmfox24 2:4f8e1fb852d3 59 /** Manufacturer ID from MCP9808_REG_MFG_ID register
Jmfox24 2:4f8e1fb852d3 60 */
Jmfox24 2:4f8e1fb852d3 61 static const uint16_t MCP9808_MFG_ID = 0x0054;
Jmfox24 2:4f8e1fb852d3 62
Jmfox24 2:4f8e1fb852d3 63 /** Device ID from MCP9808_REG_DEV_ID register
Jmfox24 2:4f8e1fb852d3 64 */
Jmfox24 2:4f8e1fb852d3 65 static const uint16_t MCP9808_DEV_ID = 0x0400;
Jmfox24 2:4f8e1fb852d3 66
Jmfox24 0:f06580426072 67 /** Create an MCP9808 using the specified pins for I2C and specified I2C
Jmfox24 0:f06580426072 68 * address
Jmfox24 0:f06580426072 69 *
Jmfox24 0:f06580426072 70 * @param sda I2C SDA pin to connect to
Jmfox24 0:f06580426072 71 * @param scl I2C SCL pin to connect to
Jmfox24 0:f06580426072 72 * @param addr I2C address for the sensor
Jmfox24 0:f06580426072 73 */
Jmfox24 0:f06580426072 74 MCP9808(PinName sda, PinName scl, int addr);
Jmfox24 0:f06580426072 75
Jmfox24 0:f06580426072 76 virtual ~MCP9808();
Jmfox24 0:f06580426072 77
Jmfox24 2:4f8e1fb852d3 78 /** Detects the sensor's presence
Jmfox24 2:4f8e1fb852d3 79 *
Jmfox24 2:4f8e1fb852d3 80 * @returns true if sensor is detected
Jmfox24 2:4f8e1fb852d3 81 */
Jmfox24 2:4f8e1fb852d3 82 bool is_detected();
Jmfox24 2:4f8e1fb852d3 83
Jmfox24 0:f06580426072 84 /** Puts the sensor into (low-power) shutdown mode
Jmfox24 0:f06580426072 85 *
Jmfox24 0:f06580426072 86 * @returns 0 on success, negative on error
Jmfox24 0:f06580426072 87 */
Jmfox24 0:f06580426072 88 int sleep();
Jmfox24 0:f06580426072 89
Jmfox24 0:f06580426072 90 /** Puts the sensor into continuous conversion mode
Jmfox24 0:f06580426072 91 *
Jmfox24 0:f06580426072 92 * @returns 0 on success, negative on error
Jmfox24 0:f06580426072 93 */
Jmfox24 0:f06580426072 94 int wake();
Jmfox24 0:f06580426072 95
Jmfox24 0:f06580426072 96 /** Returns the current ambient temperature in degrees Celsius
Jmfox24 0:f06580426072 97 *
Jmfox24 0:f06580426072 98 * @returns A floating-point value representing the current ambient
Jmfox24 0:f06580426072 99 * temperature in degrees Celsius
Jmfox24 0:f06580426072 100 */
Jmfox24 0:f06580426072 101 float get_temperature();
Jmfox24 0:f06580426072 102
Jmfox24 0:f06580426072 103 private:
Jmfox24 0:f06580426072 104 I2C _i2c;
Jmfox24 0:f06580426072 105 int _addr;
Jmfox24 0:f06580426072 106
Jmfox24 0:f06580426072 107 /** Reads a 16-bit register from the sensor
Jmfox24 0:f06580426072 108 *
Jmfox24 0:f06580426072 109 * @param reg The register to read from
Jmfox24 0:f06580426072 110 * @param value A pointer to the value to read into
Jmfox24 0:f06580426072 111 * @returns 0 on success, negative on error
Jmfox24 0:f06580426072 112 */
Jmfox24 0:f06580426072 113 int reg_read(uint8_t reg, uint16_t *value);
Jmfox24 0:f06580426072 114
Jmfox24 0:f06580426072 115 /** Writes a 16-bit register into the sensor
Jmfox24 0:f06580426072 116 *
Jmfox24 0:f06580426072 117 * @param reg The register to read into
Jmfox24 0:f06580426072 118 * @param value The value to write
Jmfox24 0:f06580426072 119 * @returns 0 on success, negative on error
Jmfox24 0:f06580426072 120 */
Jmfox24 0:f06580426072 121 int reg_write(uint8_t reg, uint16_t value);
Jmfox24 0:f06580426072 122
Jmfox24 0:f06580426072 123 /** Sets or clears a flag from the config register
Jmfox24 0:f06580426072 124 *
Jmfox24 0:f06580426072 125 * @param flag The flag to modify
Jmfox24 0:f06580426072 126 * @param v The flag state (true = set, false = clear)
Jmfox24 0:f06580426072 127 * @returns 0 on success, negative on error
Jmfox24 0:f06580426072 128 */
Jmfox24 0:f06580426072 129 int set_cfg_flag(uint16_t flag, bool v);
Jmfox24 0:f06580426072 130 };
Jmfox24 0:f06580426072 131
Jmfox24 0:f06580426072 132 #endif /* MCP9808_HPP */