Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MCP9808.hpp@1:7e98ef2bd303, 2017-01-09 (annotated)
- Committer:
- Jmfox24
- Date:
- Mon Jan 09 17:57:58 2017 +0000
- Revision:
- 1:7e98ef2bd303
- Parent:
- 0:f06580426072
- Child:
- 2:4f8e1fb852d3
fix missing include hash in example code
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:f06580426072 | 59 | /** Create an MCP9808 using the specified pins for I2C and specified I2C |
| Jmfox24 | 0:f06580426072 | 60 | * address |
| Jmfox24 | 0:f06580426072 | 61 | * |
| Jmfox24 | 0:f06580426072 | 62 | * @param sda I2C SDA pin to connect to |
| Jmfox24 | 0:f06580426072 | 63 | * @param scl I2C SCL pin to connect to |
| Jmfox24 | 0:f06580426072 | 64 | * @param addr I2C address for the sensor |
| Jmfox24 | 0:f06580426072 | 65 | */ |
| Jmfox24 | 0:f06580426072 | 66 | MCP9808(PinName sda, PinName scl, int addr); |
| Jmfox24 | 0:f06580426072 | 67 | |
| Jmfox24 | 0:f06580426072 | 68 | virtual ~MCP9808(); |
| Jmfox24 | 0:f06580426072 | 69 | |
| Jmfox24 | 0:f06580426072 | 70 | /** Puts the sensor into (low-power) shutdown mode |
| Jmfox24 | 0:f06580426072 | 71 | * |
| Jmfox24 | 0:f06580426072 | 72 | * @returns 0 on success, negative on error |
| Jmfox24 | 0:f06580426072 | 73 | */ |
| Jmfox24 | 0:f06580426072 | 74 | int sleep(); |
| Jmfox24 | 0:f06580426072 | 75 | |
| Jmfox24 | 0:f06580426072 | 76 | /** Puts the sensor into continuous conversion mode |
| Jmfox24 | 0:f06580426072 | 77 | * |
| Jmfox24 | 0:f06580426072 | 78 | * @returns 0 on success, negative on error |
| Jmfox24 | 0:f06580426072 | 79 | */ |
| Jmfox24 | 0:f06580426072 | 80 | int wake(); |
| Jmfox24 | 0:f06580426072 | 81 | |
| Jmfox24 | 0:f06580426072 | 82 | /** Returns the current ambient temperature in degrees Celsius |
| Jmfox24 | 0:f06580426072 | 83 | * |
| Jmfox24 | 0:f06580426072 | 84 | * @returns A floating-point value representing the current ambient |
| Jmfox24 | 0:f06580426072 | 85 | * temperature in degrees Celsius |
| Jmfox24 | 0:f06580426072 | 86 | */ |
| Jmfox24 | 0:f06580426072 | 87 | float get_temperature(); |
| Jmfox24 | 0:f06580426072 | 88 | |
| Jmfox24 | 0:f06580426072 | 89 | private: |
| Jmfox24 | 0:f06580426072 | 90 | I2C _i2c; |
| Jmfox24 | 0:f06580426072 | 91 | int _addr; |
| Jmfox24 | 0:f06580426072 | 92 | |
| Jmfox24 | 0:f06580426072 | 93 | /** Reads a 16-bit register from the sensor |
| Jmfox24 | 0:f06580426072 | 94 | * |
| Jmfox24 | 0:f06580426072 | 95 | * @param reg The register to read from |
| Jmfox24 | 0:f06580426072 | 96 | * @param value A pointer to the value to read into |
| Jmfox24 | 0:f06580426072 | 97 | * @returns 0 on success, negative on error |
| Jmfox24 | 0:f06580426072 | 98 | */ |
| Jmfox24 | 0:f06580426072 | 99 | int reg_read(uint8_t reg, uint16_t *value); |
| Jmfox24 | 0:f06580426072 | 100 | |
| Jmfox24 | 0:f06580426072 | 101 | /** Writes a 16-bit register into the sensor |
| Jmfox24 | 0:f06580426072 | 102 | * |
| Jmfox24 | 0:f06580426072 | 103 | * @param reg The register to read into |
| Jmfox24 | 0:f06580426072 | 104 | * @param value The value to write |
| Jmfox24 | 0:f06580426072 | 105 | * @returns 0 on success, negative on error |
| Jmfox24 | 0:f06580426072 | 106 | */ |
| Jmfox24 | 0:f06580426072 | 107 | int reg_write(uint8_t reg, uint16_t value); |
| Jmfox24 | 0:f06580426072 | 108 | |
| Jmfox24 | 0:f06580426072 | 109 | /** Sets or clears a flag from the config register |
| Jmfox24 | 0:f06580426072 | 110 | * |
| Jmfox24 | 0:f06580426072 | 111 | * @param flag The flag to modify |
| Jmfox24 | 0:f06580426072 | 112 | * @param v The flag state (true = set, false = clear) |
| Jmfox24 | 0:f06580426072 | 113 | * @returns 0 on success, negative on error |
| Jmfox24 | 0:f06580426072 | 114 | */ |
| Jmfox24 | 0:f06580426072 | 115 | int set_cfg_flag(uint16_t flag, bool v); |
| Jmfox24 | 0:f06580426072 | 116 | }; |
| Jmfox24 | 0:f06580426072 | 117 | |
| Jmfox24 | 0:f06580426072 | 118 | #endif /* MCP9808_HPP */ |