Library for TMPx75 Temperature Sensor
TMPx75.h@1:ba7de6d38992, 2018-12-12 (annotated)
- Committer:
- jake123jake123
- Date:
- Wed Dec 12 21:53:42 2018 +0000
- Revision:
- 1:ba7de6d38992
- Parent:
- 0:ba71558a5d0a
example code in .h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jake123jake123 | 0:ba71558a5d0a | 1 | /* |
jake123jake123 | 0:ba71558a5d0a | 2 | Copyright (c) 2018 Jacobus L de Jager |
jake123jake123 | 0:ba71558a5d0a | 3 | |
jake123jake123 | 0:ba71558a5d0a | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
jake123jake123 | 0:ba71558a5d0a | 5 | of this software and associated documentation files (the "Software"), to deal |
jake123jake123 | 0:ba71558a5d0a | 6 | in the Software without restriction, including without limitation the rights |
jake123jake123 | 0:ba71558a5d0a | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jake123jake123 | 0:ba71558a5d0a | 8 | copies of the Software, and to permit persons to whom the Software is |
jake123jake123 | 0:ba71558a5d0a | 9 | furnished to do so, subject to the following conditions: |
jake123jake123 | 0:ba71558a5d0a | 10 | |
jake123jake123 | 0:ba71558a5d0a | 11 | The above copyright notice and this permission notice shall be included in all |
jake123jake123 | 0:ba71558a5d0a | 12 | copies or substantial portions of the Software. |
jake123jake123 | 0:ba71558a5d0a | 13 | |
jake123jake123 | 0:ba71558a5d0a | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jake123jake123 | 0:ba71558a5d0a | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jake123jake123 | 0:ba71558a5d0a | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jake123jake123 | 0:ba71558a5d0a | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jake123jake123 | 0:ba71558a5d0a | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jake123jake123 | 0:ba71558a5d0a | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
jake123jake123 | 0:ba71558a5d0a | 20 | SOFTWARE. |
jake123jake123 | 0:ba71558a5d0a | 21 | */ |
jake123jake123 | 0:ba71558a5d0a | 22 | |
jake123jake123 | 0:ba71558a5d0a | 23 | #ifndef TMPx75_h |
jake123jake123 | 0:ba71558a5d0a | 24 | #define TMPx75_h |
jake123jake123 | 0:ba71558a5d0a | 25 | |
jake123jake123 | 0:ba71558a5d0a | 26 | #include "mbed.h" |
jake123jake123 | 0:ba71558a5d0a | 27 | |
jake123jake123 | 0:ba71558a5d0a | 28 | /* |
jake123jake123 | 0:ba71558a5d0a | 29 | Library for TMP175 and TMP75 Temperature Sensors |
jake123jake123 | 0:ba71558a5d0a | 30 | For reference, read the datasheet: http://www.ti.com/lit/ds/symlink/tmp75.pdf |
jake123jake123 | 0:ba71558a5d0a | 31 | |
jake123jake123 | 0:ba71558a5d0a | 32 | I2C ADDR (TMP75): 1 0 0 1 A2 A1 A0 (1001000 = 0x48) |
jake123jake123 | 0:ba71558a5d0a | 33 | */ |
jake123jake123 | 0:ba71558a5d0a | 34 | |
jake123jake123 | 0:ba71558a5d0a | 35 | #define TEMPERATURE_REGISTER 0x00 |
jake123jake123 | 0:ba71558a5d0a | 36 | #define CONFIGURATION_REGISTER 0x01 |
jake123jake123 | 0:ba71558a5d0a | 37 | #define T_LOW_REGISTER 0x02 |
jake123jake123 | 0:ba71558a5d0a | 38 | #define T_HIGH_REGISTER 0x03 |
jake123jake123 | 0:ba71558a5d0a | 39 | |
jake123jake123 | 0:ba71558a5d0a | 40 | //COnfiguration Register |
jake123jake123 | 0:ba71558a5d0a | 41 | //Register Byte: OS R1 R0 F1 F0 POL TM SD |
jake123jake123 | 0:ba71558a5d0a | 42 | |
jake123jake123 | 0:ba71558a5d0a | 43 | //Default: continuous, comparator, high to low, 1 fault, 9 bits |
jake123jake123 | 0:ba71558a5d0a | 44 | #define DEFAULT_CONFIG 0x00 |
jake123jake123 | 0:ba71558a5d0a | 45 | |
jake123jake123 | 0:ba71558a5d0a | 46 | //Shutdown Mode (SD) |
jake123jake123 | 0:ba71558a5d0a | 47 | #define CONTINUOUS_MODE 0x00 |
jake123jake123 | 0:ba71558a5d0a | 48 | #define SHUTDOWN_MODE 0x01 |
jake123jake123 | 0:ba71558a5d0a | 49 | |
jake123jake123 | 0:ba71558a5d0a | 50 | //Thermostat Mode (TM) |
jake123jake123 | 0:ba71558a5d0a | 51 | #define COMPARATOR_MODE 0x00 |
jake123jake123 | 0:ba71558a5d0a | 52 | #define INTERRUPT_MODE 0x02 |
jake123jake123 | 0:ba71558a5d0a | 53 | |
jake123jake123 | 0:ba71558a5d0a | 54 | //Polarity (POL) |
jake123jake123 | 0:ba71558a5d0a | 55 | #define HIGH_TO_LOW_POL 0x00 |
jake123jake123 | 0:ba71558a5d0a | 56 | #define LOW_TO_HIGH_POL 0x04 |
jake123jake123 | 0:ba71558a5d0a | 57 | |
jake123jake123 | 0:ba71558a5d0a | 58 | //Fault Queue (F1/F0) |
jake123jake123 | 0:ba71558a5d0a | 59 | #define CONSECUTIVE_FAULTS_1 0x00 |
jake123jake123 | 0:ba71558a5d0a | 60 | #define CONSECUTIVE_FAULTS_2 0x08 |
jake123jake123 | 0:ba71558a5d0a | 61 | #define CONSECUTIVE_FAULTS_4 0x10 |
jake123jake123 | 0:ba71558a5d0a | 62 | #define CONSECUTIVE_FAULTS_6 0x18 |
jake123jake123 | 0:ba71558a5d0a | 63 | |
jake123jake123 | 0:ba71558a5d0a | 64 | //Converter Resolution (R1/R0) |
jake123jake123 | 0:ba71558a5d0a | 65 | #define RESOLUTION_9_BITS 0x00 // (0.5°C) 27.5 ms |
jake123jake123 | 0:ba71558a5d0a | 66 | #define RESOLUTION_10_BITS 0x20 // (0.25°C) 55 ms |
jake123jake123 | 0:ba71558a5d0a | 67 | #define RESOLUTION_11_BITS 0x40 // (0.125°C) 110 ms |
jake123jake123 | 0:ba71558a5d0a | 68 | #define RESOLUTION_12_BITS 0x60 // (0.0625°C) 220 ms |
jake123jake123 | 0:ba71558a5d0a | 69 | |
jake123jake123 | 0:ba71558a5d0a | 70 | //One-Shot (OS) |
jake123jake123 | 0:ba71558a5d0a | 71 | #define ONE_SHOT_MODE 0x80 |
jake123jake123 | 0:ba71558a5d0a | 72 | |
jake123jake123 | 0:ba71558a5d0a | 73 | |
jake123jake123 | 0:ba71558a5d0a | 74 | class TMPx75 |
jake123jake123 | 0:ba71558a5d0a | 75 | { |
jake123jake123 | 0:ba71558a5d0a | 76 | public: |
jake123jake123 | 0:ba71558a5d0a | 77 | TMPx75(I2C *i2c, uint8_t i2c_addr); |
jake123jake123 | 0:ba71558a5d0a | 78 | virtual ~TMPx75(); |
jake123jake123 | 0:ba71558a5d0a | 79 | Serial pc; |
jake123jake123 | 0:ba71558a5d0a | 80 | |
jake123jake123 | 0:ba71558a5d0a | 81 | float read_temperature(); |
jake123jake123 | 0:ba71558a5d0a | 82 | int read_configuration(); |
jake123jake123 | 0:ba71558a5d0a | 83 | void write_configuration(uint8_t config_byte); |
jake123jake123 | 0:ba71558a5d0a | 84 | float read_T_LOW(); |
jake123jake123 | 0:ba71558a5d0a | 85 | void write_T_LOW(float temp); |
jake123jake123 | 0:ba71558a5d0a | 86 | float read_T_HIGH(); |
jake123jake123 | 0:ba71558a5d0a | 87 | void write_T_HIGH(float temp); |
jake123jake123 | 0:ba71558a5d0a | 88 | |
jake123jake123 | 0:ba71558a5d0a | 89 | private: |
jake123jake123 | 0:ba71558a5d0a | 90 | I2C *_i2c; |
jake123jake123 | 0:ba71558a5d0a | 91 | uint8_t _i2c_addr; |
jake123jake123 | 0:ba71558a5d0a | 92 | }; |
jake123jake123 | 0:ba71558a5d0a | 93 | |
jake123jake123 | 0:ba71558a5d0a | 94 | #endif |
jake123jake123 | 0:ba71558a5d0a | 95 | |
jake123jake123 | 1:ba7de6d38992 | 96 | /* |
jake123jake123 | 1:ba7de6d38992 | 97 | // Example program for reading and writing to the TMPx75 temperature sensor |
jake123jake123 | 1:ba7de6d38992 | 98 | // Jacobus L de Jager 2018 |
jake123jake123 | 1:ba7de6d38992 | 99 | |
jake123jake123 | 1:ba7de6d38992 | 100 | #include "mbed.h" |
jake123jake123 | 1:ba7de6d38992 | 101 | #include "TMPx75.h" |
jake123jake123 | 1:ba7de6d38992 | 102 | |
jake123jake123 | 1:ba7de6d38992 | 103 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); |
jake123jake123 | 1:ba7de6d38992 | 104 | I2C i2c(I2C_SDA,I2C_SCL); |
jake123jake123 | 1:ba7de6d38992 | 105 | TMPx75 tmp75(&i2c, 0x48); |
jake123jake123 | 1:ba7de6d38992 | 106 | |
jake123jake123 | 1:ba7de6d38992 | 107 | int main() |
jake123jake123 | 1:ba7de6d38992 | 108 | { |
jake123jake123 | 1:ba7de6d38992 | 109 | printf("\nTMPx75 example\n"); |
jake123jake123 | 1:ba7de6d38992 | 110 | |
jake123jake123 | 1:ba7de6d38992 | 111 | while(1) |
jake123jake123 | 1:ba7de6d38992 | 112 | { |
jake123jake123 | 1:ba7de6d38992 | 113 | pc.printf("\nreading temperature\n"); |
jake123jake123 | 1:ba7de6d38992 | 114 | float temp = tmp75.read_temperature(); |
jake123jake123 | 1:ba7de6d38992 | 115 | printf("temp = %f\n", temp); |
jake123jake123 | 1:ba7de6d38992 | 116 | |
jake123jake123 | 1:ba7de6d38992 | 117 | pc.printf("writing config\n"); |
jake123jake123 | 1:ba7de6d38992 | 118 | tmp75.write_configuration(RESOLUTION_12_BITS); |
jake123jake123 | 1:ba7de6d38992 | 119 | |
jake123jake123 | 1:ba7de6d38992 | 120 | pc.printf("reading config\n"); |
jake123jake123 | 1:ba7de6d38992 | 121 | int config = tmp75.read_configuration(); |
jake123jake123 | 1:ba7de6d38992 | 122 | printf("config register = 0x%x\n", config); |
jake123jake123 | 1:ba7de6d38992 | 123 | |
jake123jake123 | 1:ba7de6d38992 | 124 | pc.printf("reading T LOW\n"); |
jake123jake123 | 1:ba7de6d38992 | 125 | float T_LOW = tmp75.read_T_LOW(); |
jake123jake123 | 1:ba7de6d38992 | 126 | printf("T LOW = %f\n", temp); |
jake123jake123 | 1:ba7de6d38992 | 127 | |
jake123jake123 | 1:ba7de6d38992 | 128 | pc.printf("reading T HIGH\n"); |
jake123jake123 | 1:ba7de6d38992 | 129 | float T_HIGH = tmp75.read_T_HIGH(); |
jake123jake123 | 1:ba7de6d38992 | 130 | printf("T HIGH = %f\n", temp); |
jake123jake123 | 1:ba7de6d38992 | 131 | |
jake123jake123 | 1:ba7de6d38992 | 132 | pc.printf("writing T LOW\n"); |
jake123jake123 | 1:ba7de6d38992 | 133 | tmp75.write_T_LOW(25); |
jake123jake123 | 1:ba7de6d38992 | 134 | |
jake123jake123 | 1:ba7de6d38992 | 135 | pc.printf("writing T HIGH\n"); |
jake123jake123 | 1:ba7de6d38992 | 136 | tmp75.write_T_HIGH(50); |
jake123jake123 | 1:ba7de6d38992 | 137 | |
jake123jake123 | 1:ba7de6d38992 | 138 | printf("done!\n"); |
jake123jake123 | 1:ba7de6d38992 | 139 | |
jake123jake123 | 1:ba7de6d38992 | 140 | wait(0.5); |
jake123jake123 | 1:ba7de6d38992 | 141 | } |
jake123jake123 | 1:ba7de6d38992 | 142 | } |
jake123jake123 | 1:ba7de6d38992 | 143 | |
jake123jake123 | 1:ba7de6d38992 | 144 | */ |