使用TMP175数字温度传感器读温度,使用IIC通讯

1.设备有多个TMP175 I2C设备,.h文件如下

#ifndef __TMP175_H
#define __TMP175_H
#include "gpio.h"


//以下为I2C设备地址

#define  TMP175_U39_ADDR     			0xE2		//Channel 4
#define  TMP175_U61_ADDR     			0xE6		//Channel 4		
#define  TMP175_U107_ADDR     			0xE0		//Channel 3	
#define  TMP175_U111_ADDR				0xE0		//I2C_4 + SET_PDBSW1:6
#define  TMP175_U114_ADDR				0x52		//TMP175_U111_ADDR
#define  TMP175_U115_ADDR				0x50		//TMP175_U111_ADDR
#define  TMP175_U116_ADDR				0x56		//TMP175_U111_ADDR

#define  TMP175_TMP_REG_ADDR            0x00	


void TMP175_Tmp_Read (void);
void BSP_U39_TMP175 (void); 
void BSP_U61_TMP175 (void);
void BSP_U107_TMP175 (void); //0xE0
void BSP_U111_TMP175 (void); //
void BSP_U114_TMP175 (void); //
void BSP_U115_TMP175 (void); //
void BSP_U116_TMP175 (void);//

void TMP175_WriteData(uint8_t SlvAddr,uint8_t P_Reg_Byte,uint8_t *dat,uint16_t size,uint16_t Timeout) ;
void TMP175_ReadData(uint8_t SlvAddr,uint8_t P_Reg_Byte,uint8_t *dat,uint16_t size,uint16_t Timeout);

uint8_t TMP175_Temp_Read(uint8_t low_limit,uint8_t high_limit);




#endif

 2. .c文件如下

#include "TMP175.h"

#include "gpio.h"
#include "I2C.h"
#include "usart.h"
#include "stdio.h"
#include "MyIIC.h"
#include "stdlib.h"
#include "String.h"
#include "BSP_Protocol.h"
#include "BSP_IIC.h"
#include "PCA9546A_SW.h"
#include "TCA9548ASW0.h"

uint8_t TMP175_Temp_Read(uint8_t low_limit,uint8_t high_limit)
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
//	unsigned int low=0; 
	char String[64];
	char *Str;
	uint16_t  temp_value;

	uint8_t TMP175[2];
	if(strncmp((char*)RxBuffer1,"TMP175_RD",9) == 0)
	{
		strtok_r((char*)RxBuffer1,":",&Str);
	}
	if(strncmp((char*)Str,"E2",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0xE2,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
		//		printf ("Temp_U39 is : 0x%02X,0x%02X",TMP175[1],TMP175[0]);
		
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U39 is: 0x%02X\r\n",temp_value);//读出来是个16进制,可以换算下
	}
	if(strncmp((char*)Str,"E6",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0xE6,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
				
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U61 is: 0x%02X\r\n",temp_value);
	}
	if(strncmp((char*)Str,"E0",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0xE0,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
				
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U107 is: 0x%02X\r\n",temp_value);
		
	}
	if(strncmp((char*)Str,"52",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0x52,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
				
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U114 is: 0x%02X\r\n",temp_value);
	}
	if(strncmp((char*)Str,"50",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0x50,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
				
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U115 is: 0x%02X\r\n",temp_value);
	}
	if(strncmp((char*)Str,"56",2) == 0)
	{
		HAL_I2C_Mem_Read(&hi2c2 ,0x56,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
				
		temp_low=TMP175[1];
		temp_high=TMP175[0];
		temp_value =(temp_high<<8)+temp_low;
		printf ("Temp_U116 is: 0x%02X\r\n",temp_value);
	}

}	

/* 写入某寄存器的任意个数据大小 */
void TMP175_WriteData(uint8_t SlvAddr,uint8_t P_Reg_Byte,uint8_t *dat,uint16_t size,uint16_t Timeout)    //设备地址,寄存器地址,数据,数据大小,超时时间
{
	uint8_t data[size+1];       //先发寄存器地址,再发数据
	 data[0] = P_Reg_Byte;      //寄存器地址
	 
	 uint8_t i;
	 for(i=1;i<size+1;i++)
	{
		data[i] = *dat;        //赋值
		dat++;
	}
	
   while(HAL_I2C_Master_Transmit(&hi2c2,SlvAddr,data,size+1,Timeout)!= HAL_OK);
}

/* 读取某寄存器的任意个数据大小 */
void TMP175_ReadData(uint8_t SlvAddr,uint8_t P_Reg_Byte,uint8_t *dat,uint16_t size,uint16_t Timeout)  //设备地址,寄存器地址,数据,数据大小,超时时间
{
	 uint8_t data[size];
	 
   while(HAL_I2C_Master_Transmit(&hi2c2,SlvAddr,&P_Reg_Byte,1,Timeout)!= HAL_OK); 	  //先写要读的寄存器
	
	 while(HAL_I2C_Master_Receive(&hi2c2,SlvAddr,data,size,Timeout)!= HAL_OK);      //读数据
	
	 uint8_t i;
	 for(i=0;i<size;i++)
	{
		*dat = data[i];        //赋值
		dat++;
	}
}


void TMP175_Tmp_Read (void)
{
	uint8_t data[2];
	uint8_t TMP175_Tmp= HAL_I2C_Mem_Read(&hi2c2, 0xE2, 0x00, I2C_MEMADD_SIZE_8BIT, data, 1, 0xff);
	printf("TMP175_Tmp is: %d C",data[0]);

}

 void BSP_U39_TMP175 (void)  //0xE2,串口检测到U39指令时,读去U39的温度寄存器数值
 {
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U39_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U39 is: 0x%02X\r\n",temp_value);
	 
 }
void BSP_U61_TMP175 (void) //0xE6,检测U61
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U61_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U61 is: 0x%02X\r\n",temp_value);
}
void BSP_U107_TMP175 (void) //0xE0
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(3);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U107_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U107 is: 0x%02X\r\n",temp_value);
}
void BSP_U111_TMP175 (void) //0xE0
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	TCA9548A_SW0_SetChannel(6);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U111_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U111 is: 0x%02X\r\n",temp_value);
	
}
void BSP_U114_TMP175 (void) //
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	TCA9548A_SW0_SetChannel(6);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U114_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U114 is: 0x%02X\r\n",temp_value);
}
void BSP_U115_TMP175 (void) //
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	TCA9548A_SW0_SetChannel(6);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U115_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U115 is: 0x%02X\r\n",temp_value);
}
void BSP_U116_TMP175 (void) //
{
	unsigned int temp_high=0; 
	unsigned int temp_low=0;
	uint8_t TMP175[2];
	char String[64];
	char *Str;
	uint16_t  temp_value;
	TCA9548A_SetChannel(4);
	TCA9548A_SW0_SetChannel(6);
	HAL_I2C_Mem_Read(&hi2c2 ,TMP175_U115_ADDR,TMP175_TMP_REG_ADDR,I2C_MEMADD_SIZE_8BIT,TMP175,2,1000);
			
	temp_low=TMP175[1];
	temp_high=TMP175[0];
	temp_value =(temp_high<<8)+temp_low;
	printf ("Temp_U116 is: 0x%02X\r\n",temp_value);
}
 
 
 

3.指令函数

//指令的C文件内容
const FUNCTIOM_ITEM  FunItem[]= 
{

	"U39",				"读Temp_U39 的温度",						BSP_U39_TMP175,
	"U61",				"读Temp_U61 的温度",						BSP_U61_TMP175,
	"U107",				"读Temp_U107 的温度",					BSP_U107_TMP175,
	"U111",				"读Temp_U111 的温度"	,					BSP_U111_TMP175,
	"U114",				"读Temp_U114 的温度",					BSP_U114_TMP175,
	"U115",				"读Temp_U115 的温度",					BSP_U115_TMP175,
	"U116",				"读Temp_U116 的温度",					BSP_U116_TMP175,


};
//指令.h文件的内容

#ifndef __BSP_Protocol_H
#define __BSP_Protocol_H

#include "stm32f4xx_it.h"
typedef struct Function_Item
{
	char FunctionCMD[64];
	char FunctionInfo[127];
	void (*FunctionNum)(void);
}FUNCTIOM_ITEM;

#define CmdCunt sizeof(FunItem)/sizeof(FUNCTIOM_ITEM)

#endif

 4.效果图

其中0x1700,前两位17是是16+7=23℃,00代表23.0℃,0x1580,温度是16+5=21摄氏度,80代表0.5摄氏度,故U61是21.5摄氏度。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值