mcp9808 - Driver for MCP9808 digital temperature sensor

group mcp9808

ESP-IDF driver for MCP9808 Digital Temperature Sensor.

Copyright (C) 2020 Ruslan V. Uss unclerus@gmail.com

BSD Licensed as described in the file LICENSE

Defines

MCP9808_I2C_ADDR_000

I2C address, pins: A0=0, A1=0, A2=0.

MCP9808_I2C_ADDR_001

I2C address, pins: A0=1, A1=0, A2=0.

MCP9808_I2C_ADDR_010

I2C address, pins: A0=0, A1=1, A2=0.

MCP9808_I2C_ADDR_011

I2C address, pins: A0=1, A1=1, A2=0.

MCP9808_I2C_ADDR_100

I2C address, pins: A0=0, A1=0, A2=1.

MCP9808_I2C_ADDR_101

I2C address, pins: A0=1, A1=0, A2=1.

MCP9808_I2C_ADDR_110

I2C address, pins: A0=0, A1=1, A2=1.

MCP9808_I2C_ADDR_111

I2C address, pins: A0=1, A1=1, A2=1.

Enums

enum mcp9808_mode_t

Device mode.

Values:

enumerator MCP9808_CONTINUOUS

Continuous measurement mode, default.

enumerator MCP9808_SHUTDOWN

Shutdown mode.

enum mcp9808_hysteresis_t

T upper and T lower hysteresis.

Values:

enumerator MCP9808_HYST_0

0.0 deg.C, default

enumerator MCP9808_HYST_1_5

1.5 deg.C

enumerator MCP9808_HYST_3

3.0 deg.C

enumerator MCP9808_HYST_6

6.0 deg.C

enum mcp9808_alert_mode_t

Alert output mode.

Values:

enumerator MCP9808_ALERT_DISABLED

Alert output disabled, default.

enumerator MCP9808_ALERT_COMPARATOR

Alert output in comparator mode.

enumerator MCP9808_ALERT_INTERRUPT

Alert output in interrupt mode.

enum mcp9808_alert_select_t

Alert output select.

Values:

enumerator MCP9808_ALERT_UP_LOW_CRIT

Alert when T > T upper or T < T lower or T > T crit, default.

enumerator MCP9808_ALERT_CRIT

Alert when T > T crit.

enum mcp9808_alert_polarity_t

Alert output polarity.

Values:

enumerator MCP9808_ALERT_LOW

Active-low, pull-up resistor required, default.

enumerator MCP9808_ALERT_HIGH

Active-high.

enum mcp9808_resolution_t

Temperature resolution.

Values:

enumerator MCP9808_RES_0_5

Resolution = +0.5 deg.C, conversion time = 30 ms, typical sample rate = 33 Hz.

enumerator MCP9808_RES_0_25

Resolution = +0.25 deg.C, conversion time = 65 ms, typical sample rate = 15 Hz.

enumerator MCP9808_RES_0_125

Resolution = +0.125 deg.C, conversion time = 130 ms, typical sample rate = 7 Hz.

enumerator MCP9808_RES_0_0625

Resolution = +0.0625 deg.C, conversion time = 250 ms, typical sample rate = 4 Hz, default.

Functions

esp_err_t mcp9808_init_desc(i2c_dev_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Parameters
  • dev – Device descriptor

  • addr – Device I2C address

  • port – I2C port

  • sda_gpio – SDA GPIO

  • scl_gpio – SCL GPIO

Returns

ESP_OK on success

esp_err_t mcp9808_free_desc(i2c_dev_t *dev)

Free device descriptor.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t mcp9808_init(i2c_dev_t *dev)

Init device.

Set device configuration to default, clear lock bits

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t mcp9808_set_mode(i2c_dev_t *dev, mcp9808_mode_t mode)

Set device mode.

Parameters
  • dev – Device descriptor

  • mode – Power mode

Returns

ESP_OK on success

esp_err_t mcp9808_get_mode(i2c_dev_t *dev, mcp9808_mode_t *mode)

Get device mode.

Parameters
  • dev – Device descriptor

  • [out] mode – Current power mode

Returns

ESP_OK on success

esp_err_t mcp9808_set_resolution(i2c_dev_t *dev, mcp9808_resolution_t res)

Set temperature resolution.

Parameters
  • dev – Device descriptor

  • res – Resolution

Returns

ESP_OK on success

esp_err_t mcp9808_get_resolution(i2c_dev_t *dev, mcp9808_resolution_t *res)

Get temperature resolution.

Parameters
  • dev – Device descriptor

  • [out] res – Resolution

Returns

ESP_OK on success

esp_err_t mcp9808_set_alert_config(i2c_dev_t *dev, mcp9808_alert_mode_t mode, mcp9808_alert_select_t sel, mcp9808_alert_polarity_t polarity, mcp9808_hysteresis_t hyst)

Configure alert parameters.

Parameters
  • dev – Device descriptor

  • mode – Alert mode

  • sel – Alert window (see datasheet)

  • polarity – Alert output polarity

  • hyst – Alert limits hysteresis

Returns

ESP_OK on success

esp_err_t mcp9808_get_alert_config(i2c_dev_t *dev, mcp9808_alert_mode_t *mode, mcp9808_alert_select_t *sel, mcp9808_alert_polarity_t *polarity, mcp9808_hysteresis_t *hyst)

Get alert configuration.

Parameters
  • dev – Device descriptor

  • [out] mode – Alert mode

  • [out] sel – Alert window (see datasheet)

  • [out] polarity – Alert output polarity

  • [out] hyst – Alert limits hysteresis

Returns

ESP_OK on success

esp_err_t mcp9808_set_limits(i2c_dev_t *dev, float t_upper, float t_lower, float t_crit)

Set alert temperature limits.

Parameters
  • dev – Device descriptor

  • t_upper – Upper temperature

  • t_lower – Lower temperature

  • t_crit – Critical temperature

Returns

ESP_OK on success

esp_err_t mcp9808_get_limits(i2c_dev_t *dev, float *t_upper, float *t_lower, float *t_crit)

Get alert temperature limits.

Parameters
  • dev – Device descriptor

  • [out] t_upper – Upper temperature

  • [out] t_lower – Lower temperature

  • [out] t_crit – Critical temperature

Returns

ESP_OK on success

esp_err_t mcp9808_set_alert_status(i2c_dev_t *dev, bool alert)

Set alert status.

Parameters
  • dev – Device descriptor

  • alert – True for alert

Returns

ESP_OK on success

esp_err_t mcp9808_get_alert_status(i2c_dev_t *dev, bool *alert)

Get current alert status.

Parameters
  • dev – Device descriptor

  • [out] alert – Alert status

Returns

ESP_OK on success

esp_err_t mcp9808_clear_interrupt(i2c_dev_t *dev)

Clear interrupt bit.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t mcp9808_get_temperature(i2c_dev_t *dev, float *t, bool *lower, bool *upper, bool *crit)

Read temperature.

Parameters
  • dev – Device descriptor

  • [out] t – Ambient temperature

  • [out] lower – True if T a < T lower, can be NULL

  • [out] upper – True if T a > T upper, can be NULL

  • [out] crit – True if T a >= T critical, can be NULL

Returns

ESP_OK on success