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.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • addr: Device I2C address

  • port: I2C port

  • sda_gpio: SDA GPIO

  • scl_gpio: SCL GPIO

esp_err_t mcp9808_free_desc(i2c_dev_t *dev)

Free device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t mcp9808_init(i2c_dev_t *dev)

Init device.

Set device configuration to default, clear lock bits

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t mcp9808_set_mode(i2c_dev_t *dev, mcp9808_mode_t mode)

Set device mode.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • mode: Power mode

esp_err_t mcp9808_get_mode(i2c_dev_t *dev, mcp9808_mode_t *mode)

Get device mode.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] mode: Current power mode

esp_err_t mcp9808_set_resolution(i2c_dev_t *dev, mcp9808_resolution_t res)

Set temperature resolution.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • res: Resolution

esp_err_t mcp9808_get_resolution(i2c_dev_t *dev, mcp9808_resolution_t *res)

Get temperature resolution.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] res: Resolution

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.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • mode: Alert mode

  • sel: Alert window (see datasheet)

  • polarity: Alert output polarity

  • hyst: Alert limits hysteresis

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.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] mode: Alert mode

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

  • [out] polarity: Alert output polarity

  • [out] hyst: Alert limits hysteresis

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

Set alert temperature limits.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • t_upper: Upper temperature

  • t_lower: Lower temperature

  • t_crit: Critical temperature

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

Get alert temperature limits.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] t_upper: Upper temperature

  • [out] t_lower: Lower temperature

  • [out] t_crit: Critical temperature

esp_err_t mcp9808_set_alert_status(i2c_dev_t *dev, bool alert)

Set alert status.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • alert: True for alert

esp_err_t mcp9808_get_alert_status(i2c_dev_t *dev, bool *alert)

Get current alert status.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] alert: Alert status

esp_err_t mcp9808_clear_interrupt(i2c_dev_t *dev)

Clear interrupt bit.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

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

Read temperature.

Return

ESP_OK on success

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