Example main.c code for the Maxim Integrated DS7505 low voltage high accurate temperature sensor. Hosted on the MAX32630FTHR. C and C++ sample source code provided.

Dependencies:   DS7505_Low_Voltage_Temperature_Sensor max32630fthr USBDevice

Committer:
phonemacro
Date:
Sat Apr 27 09:51:03 2019 +0000
Revision:
0:3037141e268d
Child:
2:2236f57e3547
Initial Commit

Who changed what in which revision?

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