2020年ADI公司推出用于印刷电路板的数字温度传感器ADT7422。因为推出的时间较短,网络上关于ADT7422的驱动代码还很缺乏,官方上的示例代码多用于LINUX驱动的工业化应用中,而平民级别的STM32系列代码可以说是没有,我最近由于工作需要,自己按照的示例代码编写出ADT7422+IIC+STM32F4的代码,并且已经实现通信。
首先介绍一下ADT7422。ADI公司的ADT7422是一种高精度数字 2C温度传感器,设计用于在焊接到最终印刷电路板(PCB)时符合ASTM E1112标准的临床测温规范。ADT7422包含一个内部带隙基准电压源,一个温度传感器和一个精密模数转换器(ADC)。ADT7422提供16位温度结果,分辨率为0.0078°C,在25°至50°C的温度范围内的精度高达±0.10°C,无需在PCB焊接后进行校准。
ADT7422包含一个内部带隙基准电压源,一个温度传感器和一个精密模数转换器(ADC)。ADT7422提供16位温度结果,分辨率为0.0078°C,在25°至50°C的温度范围内的精度高达±0.10°C,无需在PCB焊接后进行校准。
工作于3.0 V时,平均电源电流通常为210μA。ADT7422具有关断模式,可关断器件电源,并在3.0V时提供典型值为2.0A的关断电流。ADT7422的额定工作温度范围为40°C至+ 125°C。
引脚A0和引脚A1可用于地址选择,并为ADT7422 提供四个可能的I 2 C地址。CT引脚是漏极开路输出,当温度超过可编程的临界温度限值时,该引脚变为有效。INT引脚也是漏极开路输出,当温度超过可编程限值时,该引脚变为有效。INT引脚和CT引脚可以在比较器和中断事件模式下工作。
数字温度传感器ADT7422的特性
1.用户无需校准或校正
2.低功耗
3.长期稳定性和可靠性
4.适用于工业,仪器和医疗应用的高精度。
数字温度传感器ADT7422的应用领域
生命体征监测(VSM)
医疗设备
电阻温度检测器(RTD)和热敏电阻的更换
食品运输与储存
热电偶冷端补偿
环境监测以及采暖,通风和空调(HVAC)
激光二极管温度控制
代码介绍:
首先要注意:
1、设置好A0和A1的地址,我的为A0=0,A1=1;
2、注意SCL和SDA是需要接上拉电阻,同时代码初始化要选择通信引脚为开漏模式。
3、IIC通信为正常的STM32模拟IIC通信代码。
最后就是上代码:
因为我是参考官方示例代码改写的,里面的定义为ADT7420,与ADT7422可通用。
ADT7422.C
#ifndef _ADT7420_H_
#define _ADT7420_H_
/***************************** Include Files **********************************/
/******************************************************************************/
#include "./Communication/Communication.h"
/******************************************************************************/
/************************** ADT7420 Definitions *******************************/
/******************************************************************************/
/*! ADT7420 address */
#define ADT7420_A0_PIN (((0) & 0x1) << 0) /*!< I2C Serial Bus Address Selection Pin */
#define ADT7420_A1_PIN (((1) & 0x1) << 1) /*!< I2C Serial Bus Address Selection Pin */
#define ADT7420_ADDRESS (0x48 + (ADT7420_A1_PIN ) + ADT7420_A0_PIN)
/*! ADT7420 registers */
#define ADT7420_REG_TEMP_MSB 0x00 /*!< Temperature value MSB */
#define ADT7420_REG_TEMP_LSB 0x01 /*!< Temperature value LSB */
#define ADT7420_REG_STATUS 0x02 /*!< Status */
#define ADT7420_REG_CONFIG 0x03 /*!< Configuration */
#define ADT7420_REG_T_HIGH_MSB 0x04 /*!< Temperature HIGH setpoint MSB */
#define ADT7420_REG_T_HIGH_LSB 0x05 /*!< Temperature HIGH setpoint LSB */
#define ADT7420_REG_T_LOW_MSB 0x06 /*!< Temperature LOW setpoint MSB */
#define ADT7420_REG_T_LOW_LSB 0x07 /*!< Temperature LOW setpoint LSB */
#define ADT7420_REG_T_CRIT_MSB 0x08 /*!< Temperature CRIT setpoint MSB */
#define ADT7420_REG_T_CRIT_LSB 0x09 /*!< Temperature CRIT setpoint LSB */
#define ADT7420_REG_HIST 0x0A /*!< Temperature HYST setpoint */
#define ADT7420_REG_ID 0x0B /*!< ID */
#define ADT7420_REG_RESET 0x2F /*!< Software reset */
/*! ADT7420_REG_STATUS definition */
#define ADT7420_STATUS_T_LOW (1 << 4)
#define ADT7420_STATUS_T_HIGH (1 << 5)
#define ADT7420_STATUS_T_CRIT (1 << 6)
#define ADT7420_STATUS_RDY (1 << 7)
/*! ADT7420_REG_CONFIG definition */
#define ADT7420_CONFIG_FAULT_QUEUE(x) (x & 0x3)
#define ADT7420_CONFIG_CT_POL (1 << 2)
#define ADT7420_CONFIG_INT_POL (1 << 3)
#define ADT7420_CONFIG_INT_CT_MODE (1 << 4)
#define ADT7420_CONFIG_OP_MODE(x) ((x & 0x3) << 5)
#define ADT7420_CONFIG_RESOLUTION (1 << 7)
/*! ADT7420_CONFIG_FAULT_QUEUE(x) options */
#define ADT7420_FAULT_QUEUE_1_FAULT 0
#define ADT7420_FAULT_QUEUE_2_FAULTS 1
#define ADT7420_FAULT_QUEUE_3_FAULTS 2
#define ADT7420_FAULT_QUEUE_4_FAULTS 3
/*! ADT7420_CONFIG_OP_MODE(x) options */
#define ADT7420_OP_MODE_CONT_CONV 0
#define ADT7420_OP_MODE_ONE_SHOT 1
#define ADT7420_OP_MODE_1_SPS 2
#define ADT7420_OP_MODE_SHUTDOWN 3
/*! ADT7420 default ID */
#define ADT7420_DEFAULT_ID 0xCB
/******************************************************************************/
/************************ Functions Declarations ******************************/
/******************************************************************************/
/*! Reads the value of a register. */
unsigned char ADT7420_GetRegisterValue(unsigned char registerAddress);
/*! Sets the value of a register. */
void ADT7420_SetRegisterValue(unsigned char registerAddress,
unsigned char registerValue);
/*! Initializes the comm. peripheral and checks if the device is present. */
bool ADT7420_Init(void);
/*! Resets the ADT7420. */
void ADT7420_Reset(void);
/*! Sets the operational mode for ADT7420. */
void ADT7420_SetOperationMode(unsigned char mode);
/*! Sets the resolution for ADT7420. */
void ADT7420_SetResolution(unsigned char resolution);
/*! Reads the temperature data and converts it to Celsius degrees. */
float ADT7420_GetTemperature(void);
#endif // _ADT7420_H_
ADT7422.H
#ifndef _ADT7420_H_
#define _ADT7420_H_
/***************************** Include Files **********************************/
/******************************************************************************/
#include "./Communication/Communication.h"
/******************************************************************************/
/************************** ADT7420 Definitions *******************************/
/******************************************************************************/
/*! ADT7420 address */
#define ADT7420_A0_PIN (((0) & 0x1) << 0) /*!< I2C Serial Bus Address Selection Pin */
#define ADT7420_A1_PIN (((1) & 0x1) << 1) /*!< I2C Serial Bus Address Selection Pin */
#define ADT7420_ADDRESS (0x48 + (ADT7420_A1_PIN ) + ADT7420_A0_PIN)
/*! ADT7420 registers */
#define ADT7420_REG_TEMP_MSB 0x00 /*!< Temperature value MSB */
#define ADT7420_REG_TEMP_LSB 0x01 /*!< Temperature value LSB */
#define ADT7420_REG_STATUS 0x02 /*!< Status */
#define ADT7420_REG_CONFIG 0x03 /*!< Configuration */
#define ADT7420_REG_T_HIGH_MSB 0x04 /*!< Temperature HIGH setpoint MSB */
#define ADT7420_REG_T_HIGH_LSB 0x05 /*!< Temperature HIGH setpoint LSB */
#define ADT7420_REG_T_LOW_MSB 0x06 /*!< Temperature LOW setpoint MSB */
#define ADT7420_REG_T_LOW_LSB 0x07 /*!< Temperature LOW setpoint LSB */
#define ADT7420_REG_T_CRIT_MSB 0x08 /*!< Temperature CRIT setpoint MSB */
#define ADT7420_REG_T_CRIT_LSB 0x09 /*!< Temperature CRIT setpoint LSB */
#define ADT7420_REG_HIST 0x0A /*!< Temperature HYST setpoint */
#define ADT7420_REG_ID 0x0B /*!< ID */
#define ADT7420_REG_RESET 0x2F /*!< Software reset */
/*! ADT7420_REG_STATUS definition */
#define ADT7420_STATUS_T_LOW (1 << 4)
#define ADT7420_STATUS_T_HIGH (1 << 5)
#define ADT7420_STATUS_T_CRIT (1 << 6)
#define ADT7420_STATUS_RDY (1 << 7)
/*! ADT7420_REG_CONFIG definition */
#define ADT7420_CONFIG_FAULT_QUEUE(x) (x & 0x3)
#define ADT7420_CONFIG_CT_POL (1 << 2)
#define ADT7420_CONFIG_INT_POL (1 << 3)
#define ADT7420_CONFIG_INT_CT_MODE (1 << 4)
#define ADT7420_CONFIG_OP_MODE(x) ((x & 0x3) << 5)
#define ADT7420_CONFIG_RESOLUTION (1 << 7)
/*! ADT7420_CONFIG_FAULT_QUEUE(x) options */
#define ADT7420_FAULT_QUEUE_1_FAULT 0
#define ADT7420_FAULT_QUEUE_2_FAULTS 1
#define ADT7420_FAULT_QUEUE_3_FAULTS 2
#define ADT7420_FAULT_QUEUE_4_FAULTS 3
/*! ADT7420_CONFIG_OP_MODE(x) options */
#define ADT7420_OP_MODE_CONT_CONV 0
#define ADT7420_OP_MODE_ONE_SHOT 1
#define ADT7420_OP_MODE_1_SPS 2
#define ADT7420_OP_MODE_SHUTDOWN 3
/*! ADT7420 default ID */
#define ADT7420_DEFAULT_ID 0xCB
/******************************************************************************/
/************************ Functions Declarations ******************************/
/******************************************************************************/
/*! Reads the value of a register. */
unsigned char ADT7420_GetRegisterValue(unsigned char registerAddress);
/*! Sets the value of a register. */
void ADT7420_SetRegisterValue(unsigned char registerAddress,
unsigned char registerValue);
/*! Initializes the comm. peripheral and checks if the device is present. */
bool ADT7420_Init(void);
/*! Resets the ADT7420. */
void ADT7420_Reset(void);
/*! Sets the operational mode for ADT7420. */
void ADT7420_SetOperationMode(unsigned char mode);
/*! Sets the resolution for ADT7420. */
void ADT7420_SetResolution(unsigned char resolution);
/*! Reads the temperature data and converts it to Celsius degrees. */
float ADT7420_GetTemperature(void);
#endif // _ADT7420_H_
COMMUNICATION.C
#include "Communication.h" /*!< Communication definitions */
u8 BUF[2];
/***************************************************************************//**
* @brief Writes data to a slave device.
*
* @param slaveAddress - Adress of the slave device.
* @param dataBuffer - Pointer to a buffer storing the transmission data.
* @param bytesNumber - Number of bytes to write.
* @param stopBit - Stop condition control.
* Example: 0 - A stop condition will not be sent;
* 1 - A stop condition will be sent.
*
* @return status - Number of read bytes or 0xFF if the slave address was not
* acknowledged by the device.
*******************************************************************************/
bool I2C_Write_adt7420(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
{
unsigned char acknowledge = 0;
unsigned char byte = 0;
unsigned char status = 0;
IIC_Start();
IIC_Send_Byte(slaveAddress<< 1);
acknowledge = IIC_Wait_Ack();
if(acknowledge==0)
{
for(byte = 0; byte < bytesNumber; byte++)
{
IIC_Send_Byte( dataBuffer[byte]);
}
}
else
{
printf("error in writting_communication");
}
IIC_Wait_Ack();
if(stopBit)
{
IIC_Stop();
}
return (true);
}
/***************************************************************************//**
* @brief Reads data from a slave device.
*
* @param slaveAddress - Adress of the slave device.
* @param dataBuffer - Pointer to a buffer that will store the received data.
* @param bytesNumber - Number of bytes to read.
* @param stopBit - Stop condition control.
* Example: 0 - A stop condition will not be sent;
* 1 - A stop condition will be sent.
*
* @return status - Number of read bytes or 0xFF if the slave address was not
* acknowledged by the device.
*******************************************************************************/
bool I2C_Read_adt7420(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
{
unsigned char acknowledge = 0;
unsigned char byte = 0;
IIC_Start();
IIC_Send_Byte((slaveAddress<< 1)+1 );
acknowledge = IIC_Wait_Ack();
if(acknowledge == 0)
{
for(byte=0;byte<bytesNumber;byte++)
{
dataBuffer[byte]=ADT7420_i2c_ReadByte();
if(byte==(bytesNumber-1))
{
IIC_Ack ();
}
else
{
IIC_NAck();
}
}
}
else
{
printf("error in reading_communication");
}
if(stopBit)
{
IIC_Stop();
}
return (true);
}
COMMUNICATION.H
#ifndef __COMMUNICATION_H__
#define __COMMUNICATION_H__
#include "stdint.h"
#include "stdbool.h"
#include "./iic/iic.h"
bool ADT7420_PointRegister(uint8_t w_device_addr , uint8_t register_addr);
bool ADT7420_Read_AD(uint8_t r_device_addr);
bool I2C_Write_adt7420(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit);
/*!< Reads data from a slave device. */
bool I2C_Read_adt7420(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit);
#endif /*__COMMUNICATION_H__*/
文章写的不好,如果有错漏地方请多指教讨论,大家一起学习进步。