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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************
00032 */
00033 #include "mbed.h"
00034 #include "max32630fthr.h"
00035 #include "max31725.h"
00036 #include "max31725_cpp.h"
00037 #include "USBSerial.h"
00038 
00039     Serial pc(USBTX, USBRX);          // Use USB debug probe for serial link
00040     Serial uart(P2_1, P2_0);
00041 void wait_sec_prompt(uint8_t time)
00042 {
00043    // Ports and serial connections
00044     uint32_t i;
00045     for (i = 0; i < time; i++) {
00046         pc.printf(".");
00047         wait(1);
00048     }
00049     pc.printf("\r\n");
00050 }
00051 
00052 
00053 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); 
00054  
00055 DigitalOut rLED(LED1);
00056 DigitalOut gLED(LED2);
00057 DigitalOut bLED(LED3);
00058  
00059 I2C i2cBus(P3_4, P3_5);
00060 
00061 // main() runs in its own thread in the OS
00062 // (note the calls to Thread::wait below for delays)
00063 /**
00064 * @brief Sample main program for MAX31725
00065 * @version 1.0000.0002
00066 *
00067 * @details Sample main program for MAX31725
00068 * The prints are sent to the terminal window (9600, 8n1).
00069 * The program sets the GPIOs to 3.3V and the program
00070 * configures the chip and reads temperatures.
00071 * To run the program, drag and drop the .bin file into the 
00072 * DAPLINK folder. After it finishes flashing, cycle the power or 
00073 * reset the Pegasus (MAX32630FTHR) after flashing by pressing the button on
00074 * the Pegasus next to the battery connector or the button
00075 * on the MAXREFDES100HDK.
00076 */
00077 int main()
00078 {
00079     uint32_t i;
00080     float temperature;
00081     uint8_t cfg;
00082     DigitalOut rLED(LED1, LED_OFF);
00083     DigitalOut gLED(LED2, LED_OFF);
00084     DigitalOut bLED(LED3, LED_OFF);
00085     gLED = LED_ON;
00086     pc.baud(9600);                    // Baud rate = 115200
00087     pc.printf("MAX31725 Digital Thermometer and "
00088         "Thermostat example source code.\r\n");
00089     pc.printf("\r\n");
00090     uint8_t i2c_addr = MAX31725_I2C_SLAVE_ADR_21;
00091     MAX31725 temp_sensor(i2cBus, i2c_addr);
00092     i2cBus.frequency(400000);
00093     /* Configure for time out enabled, normal format, fault filter 6,
00094        active low polarity, comparator mode, continuous
00095      */
00096     temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
00097         MAX31725_CFG_NORMAL_FORMAT| MAX31725_CFG_FAULT_FILTER_6 |
00098         MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
00099         MAX31725_CFG_CONTINUOUS));
00100     for (i = 0; i < 10; i++) {
00101         wait(MAX31725_WAIT_CONV_TIME);
00102         temperature =
00103             temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
00104         pc.printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00105             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00106     }
00107     temp_sensor.read_cfg_reg(&cfg);
00108     pc.printf("Configuration Register = 0x%02Xh \r\n", cfg);
00109 #if 0
00110     temp_sensor.write_trip_low_thyst(-63.9375);
00111     temperature =
00112         temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
00113     pc.printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00114             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00115 
00116     temp_sensor.write_trip_high_tos(64.0625f);
00117     temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
00118     pc.printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00119             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00120     pc.printf("\r\n\r\n");
00121 #endif
00122 
00123     pc.printf("\r\n");
00124     temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
00125         MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
00126         MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
00127         MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN));
00128     for (i = 0; i < 8; i++) {
00129         wait_sec_prompt(10); /*  leave it in shutdown mode for a while */
00130         /* Configure for one shot, time out disabled, extended format, fault filter 4,
00131            active low polarity, comparator mode, shutdown
00132          */
00133         temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
00134             MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
00135             MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
00136             MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN));
00137         wait(MAX31725_WAIT_CONV_TIME);
00138         temperature =
00139             temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
00140         pc.printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00141             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00142     }
00143     temp_sensor.read_cfg_reg(&cfg);
00144     pc.printf("Configuration Register = 0x%02Xh \r\n", cfg);
00145 
00146     pc.printf("\r\n\r\n");
00147 
00148 #if 0
00149     temp_sensor.write_trip_low_thyst(-55.0f);
00150     temperature = 
00151         temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
00152     pc.printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00153             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00154 
00155     temp_sensor.write_trip_high_tos(125.0f);
00156     temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
00157     pc.printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00158             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00159     pc.printf("\r\n\r\n");
00160 #endif
00161 
00162     /***************************************************************************
00163      * Call the C code version of the driver
00164      ***************************************************************************
00165      */
00166 #include "max31725_c.h"
00167     pc.printf("C implementation of the code\r\n");
00168     max31725_init(i2c_addr);
00169     /* Configure for time out enabled, normal format, fault filter 6,
00170        active low polarity, comparator mode, continuous
00171      */
00172     max31725_write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
00173         MAX31725_CFG_NORMAL_FORMAT | MAX31725_CFG_FAULT_FILTER_6 |
00174         MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
00175         MAX31725_CFG_CONTINUOUS), i2cBus);
00176     for (i = 0; i < 10; i++) {
00177         wait(MAX31725_WAIT_CONV_TIME);
00178         temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
00179             i2cBus);
00180         pc.printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00181             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00182     }
00183 
00184     max31725_read_cfg_reg(&cfg, i2cBus);
00185     pc.printf("Configuration Register = 0x%02Xh \r\n", cfg);
00186 
00187 #if 0
00188     max31725_write_trip_low_thyst(-63.9375, i2cBus);
00189     temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
00190         i2cBus);
00191     pc.printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00192             temperature, max31725_celsius_to_fahrenheit(temperature));
00193 
00194     max31725_write_trip_high_tos(64.0625f, i2cBus);
00195     temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
00196         i2cBus);
00197     pc.printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00198             temperature, max31725_celsius_to_fahrenheit(temperature));
00199 #endif
00200 
00201     pc.printf("\r\n");
00202     max31725_write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
00203         MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
00204         MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
00205         MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN), i2cBus);
00206     for (i = 0; i < 8; i++) {
00207         wait_sec_prompt(10); /*  leave it in shutdown mode for a while */
00208         /* Configure for one shot, time out disabled, extended format, fault filter 4,
00209            active low polarity, comparator mode, shutdown
00210          */
00211         max31725_write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
00212             MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
00213             MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
00214             MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN), i2cBus);
00215         wait(MAX31725_WAIT_CONV_TIME);
00216         temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
00217             i2cBus);
00218         pc.printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00219             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00220     }
00221     max31725_read_cfg_reg(&cfg, i2cBus);
00222     pc.printf("Configuration Register = 0x%02Xh \r\n", cfg);
00223 
00224 #if 0
00225     max31725_write_trip_low_thyst(-55, i2cBus);
00226     temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
00227         i2cBus);
00228     pc.printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00229             temperature, max31725_celsius_to_fahrenheit(temperature));
00230 
00231     max31725_write_trip_high_tos(125.0f, i2cBus);
00232     temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
00233         i2cBus);
00234     pc.printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00235             temperature, max31725_celsius_to_fahrenheit(temperature));
00236 #endif
00237     pc.printf("\r\n\r\n\r\n");
00238 
00239 
00240     while (true) {  // Blink the green LED 
00241         gLED = !gLED;
00242         wait(0.5);
00243     }
00244 }
00245  
00246