TMP42x HAL Based Library 1.0.2
This library is a software library that works with the TMP42x remote and local temperature sensor with N-Factor and series-R correction. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this power meter into their systems.
Loading...
Searching...
No Matches
README

Doxygen Action pages-build-deployment

TMP42x HAL Based Library

The TMP421, TMP422, and TMP423 are remote temperature sensor monitors with a built-in local temperature sensor. The remote temperature sensor diode-connected transistors are typically low-cost, NPN- or PNP-type transistors or diodes that are an integral part of microcontrollers, microprocessors, or FPGAs. (Click for more info)

This library is a software library that works with the TMP42x remote and local temperature sensor. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this sensor into their systems.

The library is designed to be easy to use and provides a simple, intuitive API for accessing the I2C interfaces of the TMP42x. It includes a range of functions for performing common I2C operations, such as sending and receiving data, querying the status of the chip, reading the measured parameters, and configuring the TMP42x settings.

With this library, developers can quickly and easily integrate the TMP42x into their systems, enabling them to take full advantage of the chip's capabilities.

Key Features

  • Easy-to-use API for accessing the I2C interfaces of the TMP42x
  • Support for common I2C operations, such as sending and receiving data, querying the status of the chip, reading the measured parameters, and configuring the TMP42x settings
  • Full feature library

Documentations

The full documents are available here

Schematic Symbol and Footprint

Footprint and schematic symbols are available in my Altium library.

Donate

Is it helpfull?

Buy me a Coffee

Getting Started

Quick Start

  1. Download the library source from the latest release
  2. Copy TMP42x.c and TMP42x.h file to your project directory and add them to your IDE if necessary.
  3. Inclued the library into your project:
    #include "TMP42x.h"
  4. Create an object (instanse) from TMP42x struct with desired name. I named it tmp422 as an example:
    TMP42x tmp422;
    struct tmp42x TMP42x
    Class (struct) that stores variables for interacting with TMP42x.
  5. Initialize the chip:
    TMP42x_init(&tmp422, TMP422, &hi2c1, 0x4C, TMP42x_Run, TMP42x_Normal, TMP42x_0_5, 1.008, 1.008, 0);
    TMP42x_Status TMP42x_init(TMP42x *self, TMP42x_Type type, I2C_HandleTypeDef *hi2c, uint8_t I2C_ADDR, TMP42x_Shutdown shutdown, TMP42x_Range range, TMP42x_ConversionRate conversion_rate, float n_eff1, float n_eff2, float n_eff3)
    Initialize the TMP42x with the given config.
    Definition TMP42x.c:138
    @ TMP422
    Definition TMP42x.h:59
    @ TMP42x_0_5
    Definition TMP42x.h:63
    @ TMP42x_Normal
    Definition TMP42x.h:61
    @ TMP42x_Run
    Definition TMP42x.h:62
    Each argument is described on the doc page.
  6. Now you can call TMP42x_getRemoteTemp function to read the meassured data:
    double remote_temp1 = TMP42x_getRemoteTemp(&tmp422, 1);
    double remote_temp2 = TMP42x_getRemoteTemp(&tmp422, 2);
    double local_temp = TMP42x_getLocalTemp(&tmp422);
    double TMP42x_getLocalTemp(TMP42x *self)
    Reads the full (floating-point) local temperature from a specified channel.
    Definition TMP42x.c:262
    double TMP42x_getRemoteTemp(TMP42x *self, uint8_t channel_number)
    Reads the full (floating-point) remote temperature from a specified channel.
    Definition TMP42x.c:217

Here is the whole code:

#include "TMP42x.h"
TMP42x tmp422;
double remote_temp1, remote_temp2, local_temp;
if(TMP42x_OK == TMP42x_init(&tmp422, TMP422, &hi2c1, 0x4C, TMP42x_Run, TMP42x_Normal, TMP42x_0_5, 1.008, 1.008, 0)){
remote_temp1 = TMP42x_getRemoteTemp(&tmp422, 1);
remote_temp2 = TMP42x_getRemoteTemp(&tmp422, 2);
local_temp = TMP42x_getLocalTemp(&tmp421);
}
@ TMP42x_OK
Definition TMP42x.h:60

If you want to use UART or virtual USB COM port on youe microcontroller, it is recommended to use this print function:

// Print setting -------------------
#define DEBUG_ENABLE 1
#define USB_DEBUG 0
#define DEBUG_UART (&huart1)
// ---------------------------------
#if DEBUG_ENABLE
#include "stdarg.h"
#include "string.h"
#include "stdlib.h"
#if USB_DEBUG
#include "usbd_cdc_if.h"
#endif
#endif
void DEBUG(const char* _str, ...){
#if DEBUG_ENABLE
va_list args;
va_start(args, _str);
char buffer[150];
memset(buffer, 0, 150);
int buffer_size = vsprintf(buffer, _str, args);
#if USB_DEBUG
CDC_Transmit_FS((uint8_t*) buffer, buffer_size);
#else
HAL_UART_Transmit(DEBUG_UART, (uint8_t*)buffer, buffer_size, 5000);
#endif
#endif
}

By applying the above trick, you can simply use this one to see the variables on the serial terminal:

#include "TMP42x.h"
TMP42x tmp422;
double remote_temp1, remote_temp2, local_temp;
if(TMP42x_OK == TMP42x_init(&tmp422, TMP422, &hi2c1, 0x4C, TMP42x_Run, TMP42x_Normal, TMP42x_0_5, 1.008, 1.008, 0)){
DEBUG("Local Temp: %.2f - Remote Temp 1: %.2f - Remote Temp 2: %.2f", TMP42x_getLocalTemp(&tmp422), TMP42x_getRemoteTemp(&tmp422, 1), TMP42x_getRemoteTemp(&tmp422, 2));
}
else{
DEBUG("----- TMP422 init failed -----\r\n");
}

Advanced Options

Getting the Integer Part of the Temperature

You can read the integer part of the temperature value by calling TMP42x_getRemoteTemp_Int and TMP42x_getLocalTemp_Int

int8_t remote_temp1 = TMP42x_getRemoteTemp_Int(&tmp422, 1);
int8_t remote_temp2 = TMP42x_getRemoteTemp_Int(&tmp422, 2);
int8_t local_temp = TMP42x_getLocalTemp_Int(&tmp422);
int8_t TMP42x_getRemoteTemp_Int(TMP42x *self, uint8_t channel_number)
Reads the integer part of the remote temperature from a specified channel.
Definition TMP42x.c:244
int8_t TMP42x_getLocalTemp_Int(TMP42x *self)
Reads the integer part of the local temperature.
Definition TMP42x.c:279

Each function is described on the doc page.

One-Shot Trigger

Instead of continous conversion (run mode), you can read start a single conversion when the device is in shutdown mode by calling the TMP42x_oneShotStart function (see more)

double remote_temp1 = TMP42x_getRemoteTemp(&tmp422, 1);
TMP42x_Status TMP42x_oneShotStart(TMP42x *self)
Starts a single conversion if the device is in shutdown mode.
Definition TMP42x.c:294

Soft Reset

You can send a reset command to TMP42x chip by calling TMP42x_softwareReset function. (see more)

Getting Manufacturer and Device ID

If you want to get the manufacturer or device ID, you can use these functions:

For example:

printf("Manufacturer ID is 0x%2X \r\n", TMP42x_getManID(&tmp422));
printf(" Device ID is 0x%2X \r\n", TMP42x_getDevID(&tmp422));
uint8_t TMP42x_getManID(TMP42x *self)
Get the manufacturer ID.
Definition TMP42x.c:320
uint8_t TMP42x_getDevID(TMP42x *self)
Get the device ID.
Definition TMP42x.c:331