Driver for the NXP PCT2075 digital temperature sensor and thermal watchdog
NXP PCT2075 temperature sensor driver for mbed
This library is a driver for the [NXP PCT2075](http://www.nxp.com/products/sensors/i2c-temperature-voltage-monitors/ic-bus-fm-plus-1-degree-c-accuracy-digital-temperature-sensor-and-thermal-watchdog:PCT2075). It only handles the I2C communication with the sensor, it does not handle the OS interrupt line. The [mbed InterruptIn](https://developer.mbed.org/handbook/InterruptIn) library can be used for this.
example usage
#include <mbed.h> #include <PCT2075.h> Serial s(USBTX, USBRX); // tx, rx InterruptIn TempPinInt(PC4); LowPowerTicker ticker; static volatile bool temp_int = false; static volatile bool tick_int = false; int main( void ) { TempPinInt.mode(PullUp); PCT2075::Configuration config = { PCT2075::OS_FAULT_QUE_1, PCT2075::OS_ACTIVE_LOW, PCT2075::OS_MODE_INTERRUPT, PCT2075::DEVICE_MODE_SHUTDOWN }; temp_sensor.set_configuration(config); temp_sensor.set_idle_time(PCT2075::TIDLE_MAX); temp_sensor.set_hyst_temperature(2000); temp_sensor.set_os_temperature(2500); TempPinInt.fall(&on_fall); ticker.attach(&tick, 2.0); while(true) { sleep(); // wait to be woken up by ticker or temp sensor if (tick_int) { s.printf("Tick\r\n"); config.device_mode = PCT2075::DEVICE_MODE_NORMAL; temp_sensor.set_configuration(config); tick_int = false; } if (temp_int) { config.device_mode = PCT2075::DEVICE_MODE_SHUTDOWN; temp_sensor.set_configuration(config); uint16_t t = temp_sensor.read_temperature(); s.printf("Temperature: %d\r\n", t); temp_int = false; } } }
PCT2075.h@1:a2b7889eb4e9, 2016-09-23 (annotated)
- Committer:
- Seppe Stas
- Date:
- Fri Sep 23 15:38:34 2016 +0200
- Revision:
- 1:a2b7889eb4e9
Added library files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Seppe Stas |
1:a2b7889eb4e9 | 1 | #ifndef _pct2075_h_ |
Seppe Stas |
1:a2b7889eb4e9 | 2 | #define _pct2075_h_ |
Seppe Stas |
1:a2b7889eb4e9 | 3 | |
Seppe Stas |
1:a2b7889eb4e9 | 4 | #include "mbed.h" |
Seppe Stas |
1:a2b7889eb4e9 | 5 | |
Seppe Stas |
1:a2b7889eb4e9 | 6 | class PCT2075 { |
Seppe Stas |
1:a2b7889eb4e9 | 7 | private: |
Seppe Stas |
1:a2b7889eb4e9 | 8 | I2C &i2c; |
Seppe Stas |
1:a2b7889eb4e9 | 9 | uint8_t address_write; |
Seppe Stas |
1:a2b7889eb4e9 | 10 | uint8_t address_read; |
Seppe Stas |
1:a2b7889eb4e9 | 11 | |
Seppe Stas |
1:a2b7889eb4e9 | 12 | public: |
Seppe Stas |
1:a2b7889eb4e9 | 13 | static const int16_t TEMP_MAX = 12500; // centi-°C |
Seppe Stas |
1:a2b7889eb4e9 | 14 | static const int16_t TEMP_MIN = -5500; // centi-°C |
Seppe Stas |
1:a2b7889eb4e9 | 15 | static const uint16_t TIDLE_MAX = 3100; // ms |
Seppe Stas |
1:a2b7889eb4e9 | 16 | static const uint16_t TIDLE_MIN = 100; // ms |
Seppe Stas |
1:a2b7889eb4e9 | 17 | |
Seppe Stas |
1:a2b7889eb4e9 | 18 | // Fault queue is defined as the number of faults that must occur |
Seppe Stas |
1:a2b7889eb4e9 | 19 | // consecutively to activate the OS output. It is provided to avoid false |
Seppe Stas |
1:a2b7889eb4e9 | 20 | // tripping due to noise. Because faults are determined at the end of data |
Seppe Stas |
1:a2b7889eb4e9 | 21 | // conversions, fault queue is also defined as the number of consecutive |
Seppe Stas |
1:a2b7889eb4e9 | 22 | // conversions returning a temperature trip. |
Seppe Stas |
1:a2b7889eb4e9 | 23 | enum OSFaultQue { |
Seppe Stas |
1:a2b7889eb4e9 | 24 | OS_FAULT_QUE_1 = 0, |
Seppe Stas |
1:a2b7889eb4e9 | 25 | OS_FAULT_QUE_2 = 1, |
Seppe Stas |
1:a2b7889eb4e9 | 26 | OS_FAULT_QUE_4 = 2, |
Seppe Stas |
1:a2b7889eb4e9 | 27 | OS_FAULT_QUE_6 = 3 |
Seppe Stas |
1:a2b7889eb4e9 | 28 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 29 | |
Seppe Stas |
1:a2b7889eb4e9 | 30 | enum OSPolarity { |
Seppe Stas |
1:a2b7889eb4e9 | 31 | OS_ACTIVE_LOW = 0, |
Seppe Stas |
1:a2b7889eb4e9 | 32 | OS_ACTIVE_HIGH = 1 |
Seppe Stas |
1:a2b7889eb4e9 | 33 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 34 | |
Seppe Stas |
1:a2b7889eb4e9 | 35 | enum OSMode { |
Seppe Stas |
1:a2b7889eb4e9 | 36 | OS_MODE_COMP = 0, |
Seppe Stas |
1:a2b7889eb4e9 | 37 | OS_MODE_INTERRUPT = 1 |
Seppe Stas |
1:a2b7889eb4e9 | 38 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 39 | |
Seppe Stas |
1:a2b7889eb4e9 | 40 | // In shutdown mode, the PCT2075 draws a small current of <1.0 µA and the |
Seppe Stas |
1:a2b7889eb4e9 | 41 | // power dissipation is minimized; the temperature conversion stops, but the |
Seppe Stas |
1:a2b7889eb4e9 | 42 | // I2C-bus interface remains active and register write/read operation can be |
Seppe Stas |
1:a2b7889eb4e9 | 43 | // performed. When the shutdown is set, the OS output will be unchanged in |
Seppe Stas |
1:a2b7889eb4e9 | 44 | // comparator mode and reset in interrupt mode. |
Seppe Stas |
1:a2b7889eb4e9 | 45 | enum DeviceMode { |
Seppe Stas |
1:a2b7889eb4e9 | 46 | DEVICE_MODE_NORMAL = 0, |
Seppe Stas |
1:a2b7889eb4e9 | 47 | DEVICE_MODE_SHUTDOWN = 1 |
Seppe Stas |
1:a2b7889eb4e9 | 48 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 49 | |
Seppe Stas |
1:a2b7889eb4e9 | 50 | struct Configuration { |
Seppe Stas |
1:a2b7889eb4e9 | 51 | OSFaultQue os_fault_que; |
Seppe Stas |
1:a2b7889eb4e9 | 52 | OSPolarity os_polarity; |
Seppe Stas |
1:a2b7889eb4e9 | 53 | OSMode os_mode; |
Seppe Stas |
1:a2b7889eb4e9 | 54 | DeviceMode device_mode; |
Seppe Stas |
1:a2b7889eb4e9 | 55 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 56 | |
Seppe Stas |
1:a2b7889eb4e9 | 57 | PCT2075 (I2C &i2c_obj, uint8_t address = 0x48); |
Seppe Stas |
1:a2b7889eb4e9 | 58 | |
Seppe Stas |
1:a2b7889eb4e9 | 59 | ~PCT2075(); |
Seppe Stas |
1:a2b7889eb4e9 | 60 | int16_t PCT2075_os_temp; |
Seppe Stas |
1:a2b7889eb4e9 | 61 | int16_t PCT2075_hyst_temp; |
Seppe Stas |
1:a2b7889eb4e9 | 62 | uint8_t PCT2075_temp_idle_cycle; |
Seppe Stas |
1:a2b7889eb4e9 | 63 | |
Seppe Stas |
1:a2b7889eb4e9 | 64 | Configuration get_configuration(); |
Seppe Stas |
1:a2b7889eb4e9 | 65 | void set_configuration(Configuration& config); |
Seppe Stas |
1:a2b7889eb4e9 | 66 | |
Seppe Stas |
1:a2b7889eb4e9 | 67 | // Shorthand functions for changing one parameter of the configuration |
Seppe Stas |
1:a2b7889eb4e9 | 68 | // without changing the other parameters. |
Seppe Stas |
1:a2b7889eb4e9 | 69 | // Note: When changing multiple parameters it is more efficient to batch |
Seppe Stas |
1:a2b7889eb4e9 | 70 | // them using set_configuration. |
Seppe Stas |
1:a2b7889eb4e9 | 71 | void set_os_fault_queue(OSFaultQue fault_que); |
Seppe Stas |
1:a2b7889eb4e9 | 72 | void set_os_polarity(OSPolarity polarity); |
Seppe Stas |
1:a2b7889eb4e9 | 73 | void set_os_mode(OSMode mode); |
Seppe Stas |
1:a2b7889eb4e9 | 74 | void set_device_mode(DeviceMode mode); |
Seppe Stas |
1:a2b7889eb4e9 | 75 | void shutdown_mode(); |
Seppe Stas |
1:a2b7889eb4e9 | 76 | void normal_mode(); |
Seppe Stas |
1:a2b7889eb4e9 | 77 | |
Seppe Stas |
1:a2b7889eb4e9 | 78 | // Returns the temperature in centi-degrees C, |
Seppe Stas |
1:a2b7889eb4e9 | 79 | // resolution of .125 degrees, rounded down to .1 degrees |
Seppe Stas |
1:a2b7889eb4e9 | 80 | int16_t read_temperature(); |
Seppe Stas |
1:a2b7889eb4e9 | 81 | |
Seppe Stas |
1:a2b7889eb4e9 | 82 | // Get the OS temperature in centi-degrees C, |
Seppe Stas |
1:a2b7889eb4e9 | 83 | // rounded to .5 degrees C |
Seppe Stas |
1:a2b7889eb4e9 | 84 | int16_t get_os_temperature(); |
Seppe Stas |
1:a2b7889eb4e9 | 85 | // Set the OS temperature in centi-degrees C, |
Seppe Stas |
1:a2b7889eb4e9 | 86 | // rounded to .5 degrees C |
Seppe Stas |
1:a2b7889eb4e9 | 87 | void set_os_temperature(int16_t temperature); |
Seppe Stas |
1:a2b7889eb4e9 | 88 | |
Seppe Stas |
1:a2b7889eb4e9 | 89 | // Get the hysteresis temperature in centi-degrees C, |
Seppe Stas |
1:a2b7889eb4e9 | 90 | // rounded to .5 degrees C |
Seppe Stas |
1:a2b7889eb4e9 | 91 | int16_t get_hyst_temperature(); |
Seppe Stas |
1:a2b7889eb4e9 | 92 | // Set the hysteresis temperature in centi-degrees C, |
Seppe Stas |
1:a2b7889eb4e9 | 93 | // rounded to .5 degrees C |
Seppe Stas |
1:a2b7889eb4e9 | 94 | void set_hyst_temperature(int16_t temperature); |
Seppe Stas |
1:a2b7889eb4e9 | 95 | |
Seppe Stas |
1:a2b7889eb4e9 | 96 | // Get idle time in ms, resolution of 100ms (deci-seconds) |
Seppe Stas |
1:a2b7889eb4e9 | 97 | uint16_t get_idle_time(); |
Seppe Stas |
1:a2b7889eb4e9 | 98 | // Set idle time in ms, rounded down to 100ms (deci-seconds) |
Seppe Stas |
1:a2b7889eb4e9 | 99 | // In normal operation mode, the temp-to-digital conversion is executed |
Seppe Stas |
1:a2b7889eb4e9 | 100 | // every 100 ms or other programmed value and the Temp register is updated |
Seppe Stas |
1:a2b7889eb4e9 | 101 | // at the end of each conversion. During each ‘conversion period’ (Tconv) of |
Seppe Stas |
1:a2b7889eb4e9 | 102 | // about 100 ms, the device takes only about 28 ms, called ‘temperature |
Seppe Stas |
1:a2b7889eb4e9 | 103 | // conversion time’ (tconv(T)), to complete a temperature-to-data conversion |
Seppe Stas |
1:a2b7889eb4e9 | 104 | // and then becomes idle for the time remaining in the period. This feature |
Seppe Stas |
1:a2b7889eb4e9 | 105 | // is implemented to significantly reduce the device power dissipation. |
Seppe Stas |
1:a2b7889eb4e9 | 106 | void set_idle_time(uint16_t time); |
Seppe Stas |
1:a2b7889eb4e9 | 107 | }; |
Seppe Stas |
1:a2b7889eb4e9 | 108 | |
Seppe Stas |
1:a2b7889eb4e9 | 109 | #endif // _PCT2075_H_ |