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

main.cpp

Committer:
phonemacro
Date:
2019-04-09
Revision:
1:5d32aeb8387e
Parent:
0:08830daf46a0
Child:
2:1890f1449f7c

File content as of revision 1:5d32aeb8387e:

/*******************************************************************************
* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************
*/
#include "mbed.h"
#include "max32630fthr.h"
#include "max31725.h"
#include "max31725_cpp.h"
#include "USBSerial.h"
 
MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); 
 
DigitalOut rLED(LED1);
DigitalOut gLED(LED2);
DigitalOut bLED(LED3);
 
I2C i2cBus(P3_4, P3_5);

// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)
/**
* @brief Sample main program for MAX31725
* @version 1.0000.0002
*
* @details Sample main program for MAX31725
* The prints are sent to the terminal window (9600, 8n1).
* The program sets the GPIOs to 3.3V and the program
* configures the chip and reads temperatures.
* To run the program, drag and drop the .bin file into the 
* DAPLINK folder. After it finishes flashing, cycle the power or 
* reset the Pegasus (MAX32630FTHR) after flashing by pressing the button on
* the Pegasus next to the battery connector or the button
* on the MAXREFDES100HDK.
*/
int main()
{
    uint32_t i;
    float temperature;
    uint8_t cfg;
    DigitalOut rLED(LED1, LED_OFF);
    DigitalOut gLED(LED2, LED_OFF);
    DigitalOut bLED(LED3, LED_OFF);
    gLED = LED_ON;
    printf("MAX31725 Digital Thermometer and "
        "Thermostat example source code.\r\n");
    printf("\r\n");
    uint8_t i2c_addr = MAX31725_I2C_SLAVE_ADR_21;
    MAX31725 temp_sensor(i2cBus, i2c_addr);
    i2cBus.frequency(400000);
    /* Configure for time out enabled, normal format, fault filter 6,
       active low polarity, comparator mode, continuous
     */
    temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
        MAX31725_CFG_NORMAL_FORMAT| MAX31725_CFG_FAULT_FILTER_6 |
        MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
        MAX31725_CFG_CONTINUOUS));
    for (i = 0; i < 10; i++) {
        wait(MAX31725_WAIT_CONV_TIME);
        temperature =
            temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
        printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
    }
    temp_sensor.read_cfg_reg(&cfg);
    printf("Configuration Register = 0x%02Xh \r\n", cfg);
#if 0
    temp_sensor.write_trip_low_thyst(-63.9375);
    temperature =
        temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
    printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));

    temp_sensor.write_trip_high_tos(64.0625f);
    temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
    printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
    printf("\r\n\r\n");
#endif

    printf("\r\n");

    for (i = 0; i < 8; i++) {
        /* Configure for one shot, time out disabled, extended format, fault filter 4,
           active low polarity, comparator mode, shutdown
         */
        temp_sensor.write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
            MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
            MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
            MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN));
        wait(MAX31725_WAIT_CONV_TIME);
        temperature =
            temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE);
        printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
        wait(2); /*  leave it in shutdown mode for a while */
    }
    temp_sensor.read_cfg_reg(&cfg);
    printf("Configuration Register = 0x%02Xh \r\n", cfg);

    printf("\r\n\r\n");

#if 0
    temp_sensor.write_trip_low_thyst(-55.0f);
    temperature = 
        temp_sensor.read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP);
    printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));

    temp_sensor.write_trip_high_tos(125.0f);
    temperature = temp_sensor.read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP);
    printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
    printf("\r\n\r\n");
#endif

    /***************************************************************************
     * Call the C code version of the driver
     ***************************************************************************
     */
#include "max31725_c.h"
    printf("C implementation of the code\r\n");
    max31725_init(i2c_addr);
    /* Configure for time out enabled, normal format, fault filter 6,
       active low polarity, comparator mode, continuous
     */
    max31725_write_cfg_reg(uint8_t(MAX31725_CFG_TIMEOUT_ENABLE |
        MAX31725_CFG_NORMAL_FORMAT | MAX31725_CFG_FAULT_FILTER_6 |
        MAX31725_CFG_OS_POLARITY_ACT_LOW | MAX31725_CFG_COMPARATOR_MODE |
        MAX31725_CFG_CONTINUOUS), i2cBus);
    /* Configure for 9 bit resolution, fault filter 1, 
       active low, comparator, continuous,
     */
    for (i = 0; i < 10; i++) {
        wait(MAX31725_WAIT_CONV_TIME);
        temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
            i2cBus);
        printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
    }

    max31725_read_cfg_reg(&cfg, i2cBus);
    printf("Configuration Register = 0x%02Xh \r\n", cfg);

#if 0
    max31725_write_trip_low_thyst(-63.9375, i2cBus);
    temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
        i2cBus);
    printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, max31725_celsius_to_fahrenheit(temperature));

    max31725_write_trip_high_tos(64.0625f, i2cBus);
    temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
        i2cBus);
    printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, max31725_celsius_to_fahrenheit(temperature));
#endif

    printf("\r\n");
    for (i = 0; i < 8; i++) {
        /* Configure for one shot, time out disabled, extended format, fault filter 4,
           active low polarity, comparator mode, shutdown
         */
        max31725_write_cfg_reg(uint8_t(MAX31725_CFG_ONE_SHOT_START |
            MAX31725_CFG_TIMEOUT_DISABLE | MAX31725_CFG_EXTENDED_FORMAT |
            MAX31725_CFG_FAULT_FILTER_4 | MAX31725_CFG_OS_POLARITY_ACT_LOW |
            MAX31725_CFG_COMPARATOR_MODE | MAX31725_CFG_SHUTDOWN), i2cBus);
        wait(MAX31725_WAIT_CONV_TIME);
        temperature = max31725_read_reg_as_temperature(MAX31725_REG_TEMPERATURE,
            i2cBus);
        printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, temp_sensor.celsius_to_fahrenheit(temperature));
        wait(2); /*  leave it in shutdown mode for a while */
    }
    max31725_read_cfg_reg(&cfg, i2cBus);
    printf("Configuration Register = 0x%02Xh \r\n", cfg);

#if 0
    max31725_write_trip_low_thyst(-55, i2cBus);
    temperature = max31725_read_reg_as_temperature(MAX31725_REG_THYST_LOW_TRIP,
        i2cBus);
    printf("Thyst Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, max31725_celsius_to_fahrenheit(temperature));

    max31725_write_trip_high_tos(125.0f, i2cBus);
    temperature = max31725_read_reg_as_temperature(MAX31725_REG_TOS_HIGH_TRIP,
        i2cBus);
    printf("TOS Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
            temperature, max31725_celsius_to_fahrenheit(temperature));
#endif
    printf("\r\n\r\n\r\n");


    while (true) {  // Blink the green LED 
        gLED = !gLED;
        wait(0.5);
    }
}