Module Brief Description
The LTC2991 is used to monitor system temperature, voltage and current. Through the I2C serial interface, 8 displays can be individually measured to provide voltage and can be paired for differential measurement of current sense resistors or temperature sense transistors. Other measurements include internal temperature and internal VCC. The internal 10ppm reference is the minimum number of supporting components and required area. Selectable addresses and configurable functions provide the LTC2991 flexibility for a variety of systems requiring temperature, voltage or current data. The LTC2991 is suitable for systems requiring submillivolt voltage resolution, 1% current measurement and 1°C temperature accuracy, or any combination of the three.
ltc2991.c
#include <stdio.h>
#include <stdlib.h>
#include "ltc2991.h"
#include "i2c.h"
#define LTC2991_SINGLE_ENDED_RATIO (305.18) //单点采集的分辨率 305.18uV = 2.5/(2^13)
#define LTC2991_DIFFERENTIAL_RATIO (19.075) //单点采集的分辨率 19.075uV = 2.5/(2^17)
#define LTC2991_VCC_RATIO (305.18) //单点采集的分辨率 305.18uV = 2.5/(2^13)
#define LTC2991_VCC_BASE (2.5)
#define LTC2991_TEMPERATURE_RATIO (0.0625) //温度的分辨率
#define LTC2991_DIODE_RATIO (38.15) //分辨率为38.15uV
#define I2C_TIME_OUT_SET 0xFFFF
// write register
uint8_t ltc2991_reg_write(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint8_t *pdata)
{
int rtn =0;
uint8_t reg_val = *pdata ;
rtn = HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,sub_addr, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
if(HAL_OK==rtn)
return 0;
else
return -1;
}
// read register , could be contine read
uint8_t ltc2991_reg_read(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint8_t *pdata,uint16_t size)
{
int rtn =0;
uint8_t reg_val = 0 ;
rtn = HAL_I2C_Mem_Read(hi2c,(uint16_t)dev_addr,sub_addr, I2C_MEMADD_SIZE_8BIT, ®_val,size,I2C_TIME_OUT_SET);
if(HAL_OK==rtn)
{
*pdata = reg_val;
return 0;
}
else
return -1;
}
// dump reg with global value or print fun
static uint8_t regester_value[LTC2991_Vcc_LSB_REG] = {
0};
static uint8_t ltc2991_reg_dump(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr)
{
int i ,rtn =0;
for(i = 0;i <= LTC2991_Vcc_LSB_REG; i++)
{
rtn = ltc2991_reg_read(hi2c,(uint16_t)dev_addr,i,®ester_value[i],1);
if(HAL_OK == rtn)
{
//printf("....")
return 0;
}
else{
return -1;
}
}
return 0;
}
// read Vx reg LSB and MSB into uint16
uint8_t ltc2991_measurement(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint16_t *pdata)
{
uint8_t data[2] = {
0};
uint8_t ifnew;
/*ntinue to read 2 bytes*/
if(HAL_I2C_Mem_Read(hi2c,(uint16_t)dev_addr,sub_addr,I2C_MEMADD_SIZE_8BIT,data,2,I2C_TIME_OUT_SET)==HAL_OK)
{
*pdata = (data[0]<<8)|data[1];
}
else{
return -1;
}
return 0;
}
// Calculates the LTC2991 single-ended input voltages
float ltc2991_get_single_ended_voltage(int16_t adc_code)
{
float voltage;
int16_t sign = 1;
if (adc_code >> 14)
{
adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
sign = -1;
}
adc_code = (adc_code & 0x3FFF);
voltage = ((float) adc_code) * LTC2991_SINGLE_ENDED_RATIO * sign; //! 2) Convert code to voltage from lsb
return (voltage);
}
// Calculates the LTC2991 Vcc voltage
//float
float ltc2991_get_vcc_voltage(int16_t adc_code)
{
// float
float voltage;
int16_t sign = 1;
if (adc_code >> 14)
{
adc_code = (adc_code ^ 0x7FFF) + 1; //! 1) Converts two's complement to binary
sign = -1;
}
voltage = (((float) adc_code) * LTC2991_VCC_RATIO * sign) + 2;//LTC2991_VCC_BASE; //! 2) Convert code to Vcc Voltage from single-ended lsb
return (voltage);
}
// Calculates the LTC2991 differential input voltage.
//float
float ltc2991_get_differential_voltage(int16_t adc_code)
{
// float
float voltage;
int16_t sign = 1;
if (adc_code >> 14)
{
adc_code = (adc_code ^ 0x7FFF) + 1; //! 1)Converts two's complement to binary
sign = -1;
}
voltage = ((float) adc_code) * LTC2991_DIFFERENTIAL_RATIO * sign; //! 2) Convert code to voltage form differential lsb
return (voltage);
}
// Calculates the LTC2991 temperature
//float
float ltc2991_get_temperature(int16_t adc_code, boolean unit)
{
// float
float temperature;
adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
if(!unit){
//! 2)Checks to see if it's Kelvin
if(adc_code >>12)
{
adc_code = (adc_code | 0xE000); //! Sign extend if it's not Kelvin (Celsius)
}
}
temperature = ((float) adc_code) * LTC2991_TEMPERATURE_RATIO; //! 3) Converts code to temperature from temperature lsb
return (temperature);
}
// Calculates the LTC2991 diode voltage
//float
float ltc2991_get_diode_voltage(int16_t adc_code)
{
// float
float voltage;
adc_code = (adc_code & 0x1FFF); //! 1) Removes first 3 bits
voltage = ((float) adc_code) * LTC2991_DIODE_RATIO; //! 2) Convert code to voltage from diode voltage lsb
return (voltage);
}
uint8_t ltc2991_init(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr)
{
uint8_t reg_val = 0;
if(NULL == hi2c)
MX_I2C1_Init();
else
return -1;
/*clear status reg*/
reg_val = 0;
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_STATUS_LOW_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
/*enable all channel*/
reg_val = LTC2991_ENABLE_ALL_CHANNELS;
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_CHANNEL_ENABLE_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
/*enable Repeated Aquisition Mode and Internal Temperature Filter*/
reg_val = LTC2991_REPEAT_MODE | LTC2991_INT_FILTER_ENABLE;
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_CONTROL_PWM_Tinternal_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
reg_val = 0;
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_PWM_THRESHOLD_MSB_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
switch(dev_addr)
{
case LTC2991_I2C_GLOBAL_ADDRESS_1:
{
/*Enable filter and temperate mode*/
reg_val = (LTC2991_V3_V4_FILTER_ENABLE|LTC2991_V3_V4_TEMP_ENABLE) | (LTC2991_V1_V2_FILTER_ENABLE|LTC2991_V1_V2_TEMP_ENABLE);
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_CONTROL_V1234_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
/*Enable filter and temperate mode*/
reg_val = (LTC2991_V7_V8_FILTER_ENABLE|LTC2991_V7_V8_TEMP_ENABLE) | (LTC2991_V5_V6_FILTER_ENABLE|LTC2991_V5_V6_TEMP_ENABLE);
HAL_I2C_Mem_Write(hi2c,(uint16_t)dev_addr,LTC2991_CONTROL_V5678_REG, I2C_MEMADD_SIZE_8BIT, ®_val, 1,I2C_TIME_OUT_SET);
}
break;
case LTC2991_I2C_GLOBAL_ADDRESS_2:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_2:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_3:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_4:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_5:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_6:
//to do....
break;
case LTC2991_I2C_GLOBAL_ADDRESS_7:
//to do....
break;
}
return 0;
}
ltc2991.h
/*! @file
@ingroup LTC2991
Library Header File for LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor
*/
#ifndef LTC2991_H
#define LTC2991_H
/* Types */
typedef int boolean;
typedef int FORMAT;
typedef int VOID;
extern const uint16_t LTC2991_TIMEOUT;
/*! @name I2C_Addresses
@{ */
//! LTC2991 Global I2C Address.
//!< DEFINE GLOBAL ADDRESS
typedef enum{
LTC2991_I2C_GLOBAL_ADDRESS_1 =0x90,
LTC2991_I2C_GLOBAL_ADDRESS_2 =0x92,
LTC2991_I2C_GLOBAL_ADDRESS_3 =0x94,
LTC2991_I2C_GLOBAL_ADDRESS_4 =0x96,
LTC2991_I2C_GLOBAL_ADDRESS_5 =0x98,
LTC2991_I2C_GLOBAL_ADDRESS_6 =0x9F,
LTC2991_I2C_GLOBAL_ADDRESS_7 =0x9C,
LTC2991_I2C_GLOBAL_ADDRESS_8 =0x9E,
}LTC2991_DEV_ADDR;
/*! @} */
/*! @name REGISTERS
@{ */
#define LTC2991_STATUS_LOW_REG 0x00 //!< Data_Valid Bits(V1 Through V8)
#define LTC2991_CHANNEL_ENABLE_REG 0x01 //!< Channel Enable, Vcc, T_internal Conversion Status, Trigger
#define LTC2991_CONTROL_V1234_REG 0x06 //!< V1, V2, V3, and V4 Control Register
#define LTC2991_CONTROL_V5678_REG 0x07 //!< V5, V6, V7, AND V8 Control Register
#define LTC2991_CONTROL_PWM_Tinternal_REG 0x08 //!< PWM Threshold and T_internal Control Register
#define LTC2991_PWM_THRESHOLD_MSB_REG 0x09 //!< PWM Threshold
#define LTC2991_V1_MSB_REG 0x0A //!< V1, or T_R1 T MSB
#define LTC2991_V1_LSB_REG 0x0B //!< V1, or T_R1 T LSB
#define LTC2991_V2_MSB_REG 0x0C //!< V2, V1-V2, or T_R2 Voltage MSB
#define LTC2991_V2_LSB_REG 0x0D //!< V2, V1-V2, or T_R2 Voltage LSB
#define LTC2991_V3_MSB_REG 0x0E //!< V3, or T_R2 T MSB
#define LTC2991_V3_LSB_REG 0x0F //!< V3, or T_R2 T LSB
#define LTC2991_V4_MSB_REG 0x10 //!< V4, V3-V4, or T_R2 Voltage MSB
#define LTC2991_V4_LSB_REG 0x11 //!< V4, V3-V4, or T_R2 Voltage LSB
#define LTC2991_V5_MSB_REG 0x12 //!< V5, or T_R3 T MSB
#define LTC2991_V5_LSB_REG 0x13 //!< V5, or T_R3 T LSB
#define LTC2991_V6_MSB_REG 0x14 //!< V6, V5-V6, or T_R3 Voltage MSB
#define LTC2991_V6_LSB_REG 0x15 //!< V6, V5-V6, or T_R3 Voltage LSB
#define LTC2991_V7_MSB_REG 0x16 //!< V7, or T_R4 T MSB
#define LTC2991_V7_LSB_REG 0x17 //!< V7, or T_R4 T LSB
#define LTC2991_V8_MSB_REG 0x18 //!< V8, V7-V8, or T_R4 Voltage MSB
#define LTC2991_V8_LSB_REG 0x19 //!< V8, V7-V8, or T_R4 Voltage LSB
#define LTC2991_T_Internal_MSB_REG 0x1A //!< T_Internal MSB
#define LTC2991_T_Internal_LSB_REG 0x1B //!< T_Internal LSB
#define LTC2991_Vcc_MSB_REG 0x1C //!< Vcc MSB
#define LTC2991_Vcc_LSB_REG 0x1D //!< Vcc LSB
/*! @} */
/*! @name LTC2991_CHANNEL_ENABLE_REG SETTINGS
Bitwise OR settings, and write to LTC2991_CHANNEL_ENABLE_REG to configure settings.
Bitwise AND with value read from LTC2991_CHANNEL_ENABLE_REG to determine present setting.
@{ */
#define LTC2991_V7_V8_TR4_ENABLE 0x80 //!< Enable V7-V8 measurements, including TR4 temperature
#define LTC2991_V5_V6_TR3_ENABLE 0x40 //!< Enable V5-V6 measurements, including TR3 temperature
#define LTC2991_V3_V4_TR2_ENABLE 0x20 //!< Enable V3-V4 measurements, including TR2 temperature
#define LTC2991_V1_V2_TR1_ENABLE 0x10 //!< Enable V1-V2 measurements, including TR1 temperature
#define LTC2991_VCC_TINTERNAL_ENABLE 0x08 //!< Enable Vcc internal voltage measurement
#define LTC2991_ENABLE_ALL_CHANNELS 0xF8 //!< Use to enable all LTC2991 channels. Equivalent to bitwise OR'ing all channel enables.
#define LTC2991_BUSY 0x04 //!< LTC2991 Busy Bit
/*! @} */
/*! @name LTC2991_CONTROL_V1234_REG SETTINGS
Bitwise OR settings, and write to LTC2991_CONTROL_V1234_REGG to configure settings.
Bitwise AND with value read from LTC2991_CONTROL_V1234_REG to determine present setting.
@{ */
#define LTC2991_V3_V4_FILTER_ENABLE 0x80 //!< Enable filters on V3-V4
#define LTC2991_V3_V4_KELVIN_ENABLE 0x40 //!< Enable V3-V4 for Kelvin. Otherwise, Celsius.
#define LTC2991_V3_V4_TEMP_ENABLE 0x20 //!< Enable V3-V4 temperature mode.
#define LTC2991_V3_V4_DIFFERENTIAL_ENABLE 0x10 //!< Enable V3-V4 differential mode. Otherwise, single-ended.
#define LTC2991_V1_V2_FILTER_ENABLE 0x08 //!< Enable filters on V1-V2
#define LTC2991_V1_V2_KELVIN_ENABLE 0x04 //!< Enable V1-V2 for Kelvin. Otherwise, Celsius.
#define LTC2991_V1_V2_TEMP_ENABLE 0x02 //!< Enable V1-V2 temperature mode.
#define LTC2991_V1_V2_DIFFERENTIAL_ENABLE 0x01 //!< Enable V1-V2 differential mode. Otherwise, single-ended.
/*! @} */
/*! @name LTC2991_CONTROL_V5678_REG SETTINGS
Bitwise OR settings, and write to LTC2991_CONTROL_V5678_REG to configure settings.
Bitwise AND with value read from LTC2991_CONTROL_V5678_REG to determine present setting.
@{ */
#define LTC2991_V7_V8_FILTER_ENABLE 0x80 //!< Enable filters on V7-V8
#define LTC2991_V7_V8_KELVIN_ENABLE 0x40 //!< Enable V7-V8 for Kelvin. Otherwise, Celsius.
#define LTC2991_V7_V8_TEMP_ENABLE 0x20 //!< Enable V7-V8 temperature mode.
#define LTC2991_V7_V8_DIFFERENTIAL_ENABLE 0x10 //!< Enable V7-V8 differential mode. Otherwise, single-ended.
#define LTC2991_V5_V6_FILTER_ENABLE 0x08 //!< Enable filters on V5-V6
#define LTC2991_V5_V6_KELVIN_ENABLE 0x04 //!< Enable V5-V6 for Kelvin. Otherwise, Celsius.
#define LTC2991_V5_V6_TEMP_ENABLE 0x02 //!< Enable V5-V6 temperature mode.
#define LTC2991_V5_V6_DIFFERENTIAL_ENABLE 0x01 //!< Enable V5-V6 differential mode. Otherwise, single-ended.
/*! @} */
/*! @name LTC2991_CONTROL_PWM_Tinternal_REG SETTINGS
Bitwise OR settings, and write to LTC2991_CONTROL_PWM_Tinternal_REG to configure settings.
Bitwise AND with value read from LTC2991_CONTROL_PWM_Tinternal_REG to determine present setting.
@{ */
#define LTC2991_PWM_0 0x80 //!< PWM threshold Least Significant Bit
#define LTC2991_PWM_INVERT 0x40 //!< Invert PWM
#define LTC2991_PWM_ENABLE 0x20 //!< Enable PWM
#define LTC2991_REPEAT_MODE 0x10 //!< Enable Repeated Aquisition Mode
#define LTC2991_INT_FILTER_ENABLE 0x08 //!< Enable Internal Temperature Filter
#define LTC2991_INT_KELVIN_ENABLE 0x04 //!< Enable internal temperature for Kelvin. Otherwise, Celsius.
/*!@} */
uint8_t ltc2991_reg_write(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint8_t *pdata);
uint8_t ltc2991_reg_read(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint8_t *pdata,uint16_t size);
uint8_t ltc2991_measurement(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr,uint16_t sub_addr, uint16_t *pdata);
float ltc2991_get_single_ended_voltage(int16_t adc_code);
float ltc2991_get_vcc_voltage(int16_t adc_code);
float ltc2991_get_differential_voltage(int16_t adc_code);
float ltc2991_get_temperature(int16_t adc_code, boolean unit);
float ltc2991_get_diode_voltage(int16_t adc_code);
uint8_t ltc2991_init(I2C_HandleTypeDef *hi2c,LTC2991_DEV_ADDR dev_addr);
#endif // LTC2991_H