Sample main.c/cpp code for the Maxim Integrated MAX31725, MAX31726 high temperature sensor with accuracy of to +-0.5°C. Hosted on the MAX32630FTHR. Typical applications are for thermometers, thermostat and over-temperature monitoring.

Dependencies:   MAX31725_Accurate_Temperature_Sensor max32630fthr USBDevice

Committer:
phonemacro
Date:
Tue Apr 09 22:58:23 2019 +0000
Revision:
1:5d32aeb8387e
Parent:
0:08830daf46a0
Child:
2:1890f1449f7c
add max31725 i2c addresses

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:08830daf46a0 1 /*******************************************************************************
phonemacro 0:08830daf46a0 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:08830daf46a0 3 *
phonemacro 0:08830daf46a0 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:08830daf46a0 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:08830daf46a0 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:08830daf46a0 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:08830daf46a0 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:08830daf46a0 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:08830daf46a0 10 *
phonemacro 0:08830daf46a0 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:08830daf46a0 12 * in all copies or substantial portions of the Software.
phonemacro 0:08830daf46a0 13 *
phonemacro 0:08830daf46a0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:08830daf46a0 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:08830daf46a0 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:08830daf46a0 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:08830daf46a0 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:08830daf46a0 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:08830daf46a0 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:08830daf46a0 21 *
phonemacro 0:08830daf46a0 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:08830daf46a0 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:08830daf46a0 24 * Products, Inc. Branding Policy.
phonemacro 0:08830daf46a0 25 *
phonemacro 0:08830daf46a0 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:08830daf46a0 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:08830daf46a0 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:08830daf46a0 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:08830daf46a0 30 * ownership rights.
phonemacro 0:08830daf46a0 31 *******************************************************************************
phonemacro 0:08830daf46a0 32 */
phonemacro 0:08830daf46a0 33 #include "mbed.h"
phonemacro 0:08830daf46a0 34 #include "max32630fthr.h"
phonemacro 0:08830daf46a0 35 #include "max31725.h"
phonemacro 0:08830daf46a0 36 #include "max31725_cpp.h"
phonemacro 0:08830daf46a0 37 #include "USBSerial.h"
phonemacro 0:08830daf46a0 38
phonemacro 0:08830daf46a0 39 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
phonemacro 0:08830daf46a0 40
phonemacro 0:08830daf46a0 41 DigitalOut rLED(LED1);
phonemacro 0:08830daf46a0 42 DigitalOut gLED(LED2);
phonemacro 0:08830daf46a0 43 DigitalOut bLED(LED3);
phonemacro 0:08830daf46a0 44
phonemacro 0:08830daf46a0 45 I2C i2cBus(P3_4, P3_5);
phonemacro 0:08830daf46a0 46
phonemacro 0:08830daf46a0 47 // main() runs in its own thread in the OS
phonemacro 0:08830daf46a0 48 // (note the calls to Thread::wait below for delays)
phonemacro 0:08830daf46a0 49 /**
phonemacro 0:08830daf46a0 50 * @brief Sample main program for MAX31725
phonemacro 0:08830daf46a0 51 * @version 1.0000.0002
phonemacro 0:08830daf46a0 52 *
phonemacro 0:08830daf46a0 53 * @details Sample main program for MAX31725
phonemacro 0:08830daf46a0 54 * The prints are sent to the terminal window (9600, 8n1).
phonemacro 0:08830daf46a0 55 * The program sets the GPIOs to 3.3V and the program
phonemacro 0:08830daf46a0 56 * configures the chip and reads temperatures.
phonemacro 0:08830daf46a0 57 * To run the program, drag and drop the .bin file into the
phonemacro 0:08830daf46a0 58 * DAPLINK folder. After it finishes flashing, cycle the power or
phonemacro 0:08830daf46a0 59 * reset the Pegasus (MAX32630FTHR) after flashing by pressing the button on
phonemacro 0:08830daf46a0 60 * the Pegasus next to the battery connector or the button
phonemacro 0:08830daf46a0 61 * on the MAXREFDES100HDK.
phonemacro 0:08830daf46a0 62 */
phonemacro 0:08830daf46a0 63 int main()
phonemacro 0:08830daf46a0 64 {
phonemacro 0:08830daf46a0 65 uint32_t i;
phonemacro 0:08830daf46a0 66 float temperature;
phonemacro 0:08830daf46a0 67 uint8_t cfg;
phonemacro 0:08830daf46a0 68 DigitalOut rLED(LED1, LED_OFF);
phonemacro 0:08830daf46a0 69 DigitalOut gLED(LED2, LED_OFF);
phonemacro 0:08830daf46a0 70 DigitalOut bLED(LED3, LED_OFF);
phonemacro 0:08830daf46a0 71 gLED = LED_ON;
phonemacro 0:08830daf46a0 72 printf("MAX31725 Digital Thermometer and "
phonemacro 0:08830daf46a0 73 "Thermostat example source code.\r\n");
phonemacro 0:08830daf46a0 74 printf("\r\n");
phonemacro 1:5d32aeb8387e 75 uint8_t i2c_addr = MAX31725_I2C_SLAVE_ADR_21;
phonemacro 1:5d32aeb8387e 76 MAX31725 temp_sensor(i2cBus, i2c_addr);
phonemacro 0:08830daf46a0 77 i2cBus.frequency(400000);
phonemacro 0:08830daf46a0 78 /* Configure for time out enabled, normal format, fault filter 6,
phonemacro 0:08830daf46a0 79 active low polarity, comparator mode, continuous
phonemacro 0:08830daf46a0 80 */
phonemacro 0:08830daf46a0 81 temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
phonemacro 0:08830daf46a0 82 MAX31725_CFG_NORMAL_FORMAT| MAX31725_CFG_FAULT_FILTER_6 |
phonemacro 0:08830daf46a0 83 MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
phonemacro 0:08830daf46a0 84 MAX31725_CFG_CONTINUOUS));
phonemacro 0:08830daf46a0 85 for (i = 0; i < 10; i++) {
phonemacro 0:08830daf46a0 86 wait(MAX31725_WAIT_CONV_TIME);
phonemacro 0:08830daf46a0 87 temperature =
phonemacro 0:08830daf46a0 88 temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
phonemacro 0:08830daf46a0 89 printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 90 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 91 }
phonemacro 0:08830daf46a0 92 temp_sensor.read_cfg_reg(&cfg);
phonemacro 0:08830daf46a0 93 printf("Configuration Register = 0x%02Xh \r\n", cfg);
phonemacro 0:08830daf46a0 94 #if 0
phonemacro 0:08830daf46a0 95 temp_sensor.write_trip_low_thyst(-63.9375);
phonemacro 0:08830daf46a0 96 temperature =
phonemacro 0:08830daf46a0 97 temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
phonemacro 0:08830daf46a0 98 printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 99 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 100
phonemacro 0:08830daf46a0 101 temp_sensor.write_trip_high_tos(64.0625f);
phonemacro 0:08830daf46a0 102 temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
phonemacro 0:08830daf46a0 103 printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 104 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 105 printf("\r\n\r\n");
phonemacro 0:08830daf46a0 106 #endif
phonemacro 0:08830daf46a0 107
phonemacro 0:08830daf46a0 108 printf("\r\n");
phonemacro 0:08830daf46a0 109
phonemacro 0:08830daf46a0 110 for (i = 0; i < 8; i++) {
phonemacro 0:08830daf46a0 111 /* Configure for one shot, time out disabled, extended format, fault filter 4,
phonemacro 0:08830daf46a0 112 active low polarity, comparator mode, shutdown
phonemacro 0:08830daf46a0 113 */
phonemacro 0:08830daf46a0 114 temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
phonemacro 0:08830daf46a0 115 MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
phonemacro 0:08830daf46a0 116 MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
phonemacro 0:08830daf46a0 117 MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN));
phonemacro 0:08830daf46a0 118 wait(MAX31725_WAIT_CONV_TIME);
phonemacro 0:08830daf46a0 119 temperature =
phonemacro 0:08830daf46a0 120 temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
phonemacro 0:08830daf46a0 121 printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 122 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 123 wait(2); /* leave it in shutdown mode for a while */
phonemacro 0:08830daf46a0 124 }
phonemacro 0:08830daf46a0 125 temp_sensor.read_cfg_reg(&cfg);
phonemacro 0:08830daf46a0 126 printf("Configuration Register = 0x%02Xh \r\n", cfg);
phonemacro 0:08830daf46a0 127
phonemacro 0:08830daf46a0 128 printf("\r\n\r\n");
phonemacro 0:08830daf46a0 129
phonemacro 0:08830daf46a0 130 #if 0
phonemacro 0:08830daf46a0 131 temp_sensor.write_trip_low_thyst(-55.0f);
phonemacro 0:08830daf46a0 132 temperature =
phonemacro 0:08830daf46a0 133 temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
phonemacro 0:08830daf46a0 134 printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 135 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 136
phonemacro 0:08830daf46a0 137 temp_sensor.write_trip_high_tos(125.0f);
phonemacro 0:08830daf46a0 138 temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
phonemacro 0:08830daf46a0 139 printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 140 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 141 printf("\r\n\r\n");
phonemacro 0:08830daf46a0 142 #endif
phonemacro 0:08830daf46a0 143
phonemacro 0:08830daf46a0 144 /***************************************************************************
phonemacro 0:08830daf46a0 145 * Call the C code version of the driver
phonemacro 0:08830daf46a0 146 ***************************************************************************
phonemacro 0:08830daf46a0 147 */
phonemacro 0:08830daf46a0 148 #include "max31725_c.h"
phonemacro 0:08830daf46a0 149 printf("C implementation of the code\r\n");
phonemacro 1:5d32aeb8387e 150 max31725_init(i2c_addr);
phonemacro 0:08830daf46a0 151 /* Configure for time out enabled, normal format, fault filter 6,
phonemacro 0:08830daf46a0 152 active low polarity, comparator mode, continuous
phonemacro 0:08830daf46a0 153 */
phonemacro 0:08830daf46a0 154 max31725_write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
phonemacro 0:08830daf46a0 155 MAX31725_CFG_NORMAL_FORMAT | MAX31725_CFG_FAULT_FILTER_6 |
phonemacro 0:08830daf46a0 156 MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
phonemacro 0:08830daf46a0 157 MAX31725_CFG_CONTINUOUS), i2cBus);
phonemacro 0:08830daf46a0 158 /* Configure for 9 bit resolution, fault filter 1,
phonemacro 0:08830daf46a0 159 active low, comparator, continuous,
phonemacro 0:08830daf46a0 160 */
phonemacro 0:08830daf46a0 161 for (i = 0; i < 10; i++) {
phonemacro 0:08830daf46a0 162 wait(MAX31725_WAIT_CONV_TIME);
phonemacro 0:08830daf46a0 163 temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
phonemacro 0:08830daf46a0 164 i2cBus);
phonemacro 0:08830daf46a0 165 printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 166 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 167 }
phonemacro 0:08830daf46a0 168
phonemacro 0:08830daf46a0 169 max31725_read_cfg_reg(&cfg, i2cBus);
phonemacro 0:08830daf46a0 170 printf("Configuration Register = 0x%02Xh \r\n", cfg);
phonemacro 0:08830daf46a0 171
phonemacro 0:08830daf46a0 172 #if 0
phonemacro 0:08830daf46a0 173 max31725_write_trip_low_thyst(-63.9375, i2cBus);
phonemacro 0:08830daf46a0 174 temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
phonemacro 0:08830daf46a0 175 i2cBus);
phonemacro 0:08830daf46a0 176 printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 177 temperature, max31725_celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 178
phonemacro 0:08830daf46a0 179 max31725_write_trip_high_tos(64.0625f, i2cBus);
phonemacro 0:08830daf46a0 180 temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
phonemacro 0:08830daf46a0 181 i2cBus);
phonemacro 0:08830daf46a0 182 printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 183 temperature, max31725_celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 184 #endif
phonemacro 0:08830daf46a0 185
phonemacro 0:08830daf46a0 186 printf("\r\n");
phonemacro 0:08830daf46a0 187 for (i = 0; i < 8; i++) {
phonemacro 0:08830daf46a0 188 /* Configure for one shot, time out disabled, extended format, fault filter 4,
phonemacro 0:08830daf46a0 189 active low polarity, comparator mode, shutdown
phonemacro 0:08830daf46a0 190 */
phonemacro 0:08830daf46a0 191 max31725_write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
phonemacro 0:08830daf46a0 192 MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
phonemacro 0:08830daf46a0 193 MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
phonemacro 0:08830daf46a0 194 MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN), i2cBus);
phonemacro 0:08830daf46a0 195 wait(MAX31725_WAIT_CONV_TIME);
phonemacro 0:08830daf46a0 196 temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
phonemacro 0:08830daf46a0 197 i2cBus);
phonemacro 0:08830daf46a0 198 printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 199 temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 200 wait(2); /* leave it in shutdown mode for a while */
phonemacro 0:08830daf46a0 201 }
phonemacro 0:08830daf46a0 202 max31725_read_cfg_reg(&cfg, i2cBus);
phonemacro 0:08830daf46a0 203 printf("Configuration Register = 0x%02Xh \r\n", cfg);
phonemacro 0:08830daf46a0 204
phonemacro 0:08830daf46a0 205 #if 0
phonemacro 0:08830daf46a0 206 max31725_write_trip_low_thyst(-55, i2cBus);
phonemacro 0:08830daf46a0 207 temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
phonemacro 0:08830daf46a0 208 i2cBus);
phonemacro 0:08830daf46a0 209 printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 210 temperature, max31725_celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 211
phonemacro 0:08830daf46a0 212 max31725_write_trip_high_tos(125.0f, i2cBus);
phonemacro 0:08830daf46a0 213 temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
phonemacro 0:08830daf46a0 214 i2cBus);
phonemacro 0:08830daf46a0 215 printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:08830daf46a0 216 temperature, max31725_celsius_to_fahrenheit(temperature));
phonemacro 0:08830daf46a0 217 #endif
phonemacro 0:08830daf46a0 218 printf("\r\n\r\n\r\n");
phonemacro 0:08830daf46a0 219
phonemacro 0:08830daf46a0 220
phonemacro 0:08830daf46a0 221 while (true) { // Blink the green LED
phonemacro 0:08830daf46a0 222 gLED = !gLED;
phonemacro 0:08830daf46a0 223 wait(0.5);
phonemacro 0:08830daf46a0 224 }
phonemacro 0:08830daf46a0 225 }
phonemacro 0:08830daf46a0 226
phonemacro 0:08830daf46a0 227