GY906 温度的传感器

外形

在这里插入图片描述

注意事项

引脚设置为开漏输出
模块可以3.3v供电

代码

代码来自STM32 单片机 GY-906 MLX90614 红外温度读取
这位大哥的

main

temp=SMBus_ReadTemp();
		printf("ζÈֵΪ£º%.2f\r\n",temp);
HAL_Delay(100);

.c

#include "mlx90614.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK	 0 
#define	NACK 1 
#define SA				0x00 //Slave address x
#define RAM_ACCESS		0x00 //RAM access command RAM´æÈ¡ÃüÁî
#define EEPROM_ACCESS	0x20 //EEPROM access command EEPROM´æÈ¡ÃüÁî
#define RAM_TOBJ1		0x07 //To1 address in the eeprom Ä¿±ê1ζÈ,¼ì²âµ½µÄºìÍâÎÂ¶È -70.01 ~ 382.19¶È

#define SMBUS_PORT	GPIOB      //PB¶Ë¿Ú(¶Ë¿ÚºÍÏÂÃæµÄÁ½¸öÕë½Å¿É×Ô¶¨Òå)
#define SMBUS_SCK		GPIO_PIN_6 //PB6£ºSCL
#define SMBUS_SDA		GPIO_PIN_7 //PB7£ºSDA2

#define RCC_APB2Periph_SMBUS_PORT		RCC_APB2Periph_GPIOB

#define SMBUS_SCK_H()	    SMBUS_PORT->BSRR = SMBUS_SCK //ÖÃ¸ßµçÆ½
#define SMBUS_SCK_L()	    SMBUS_PORT->BRR = SMBUS_SCK  //ÖÃµÍµçÆ½
#define SMBUS_SDA_H()	    SMBUS_PORT->BSRR = SMBUS_SDA
#define SMBUS_SDA_L()	    SMBUS_PORT->BRR = SMBUS_SDA

#define SMBUS_SDA_PIN()	    SMBUS_PORT->IDR & SMBUS_SDA //¶ÁÈ¡Òý½Åµçƽ

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : SMBus_StartBit
* Description    : Generate START condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_StartBit(void)
{
    SMBUS_SDA_H();		// Set SDA line
    SMBus_Delay(5);	    // Wait a few microseconds
    SMBUS_SCK_H();		// Set SCL line
    SMBus_Delay(5);	    // Generate bus free time between Stop
    SMBUS_SDA_L();		// Clear SDA line
    SMBus_Delay(5);	    // Hold time after (Repeated) Start
    // Condition. After this period, the first clock is generated.
    //(Thd:sta=4.0us min)ÔÚSCK=1ʱ£¬¼ì²âµ½SDAÓÉ1µ½0±íʾͨÐÅ¿ªÊ¼£¨Ï½µÑØ£©
    SMBUS_SCK_L();	    // Clear SCL line
    SMBus_Delay(5);	    // Wait a few microseconds
}

/*******************************************************************************
* Function Name  : SMBus_StopBit
* Description    : Generate STOP condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_StopBit(void)
{
    SMBUS_SCK_L();		// Clear SCL line
    SMBus_Delay(5);	    // Wait a few microseconds
    SMBUS_SDA_L();		// Clear SDA line
    SMBus_Delay(5);	    // Wait a few microseconds
    SMBUS_SCK_H();		// Set SCL line
    SMBus_Delay(5);	    // Stop condition setup time(Tsu:sto=4.0us min)
    SMBUS_SDA_H();		// Set SDA lineÔÚSCK=1ʱ£¬¼ì²âµ½SDAÓÉ0µ½1±íʾͨÐŽáÊø£¨ÉÏÉýÑØ£©
}

/*******************************************************************************
* Function Name  : SMBus_SendByte
* Description    : Send a byte on SMBus
* Input          : Tx_buffer
* Output         : None
* Return         : None
*******************************************************************************/
u8 SMBus_SendByte(u8 Tx_buffer)
{
    u8	Bit_counter;
    u8	Ack_bit;
    u8	bit_out;

    for(Bit_counter=8; Bit_counter; Bit_counter--)
    {
        if (Tx_buffer&0x80)
        {
            bit_out=1;   // If the current bit of Tx_buffer is 1 set bit_out
        }
        else
        {
            bit_out=0;  // else clear bit_out
        }
        SMBus_SendBit(bit_out);		// Send the current bit on SDA
        Tx_buffer<<=1;				// Get next bit for checking
    }

    Ack_bit=SMBus_ReceiveBit();		// Get acknowledgment bit
    return	Ack_bit;
}

/*******************************************************************************
* Function Name  : SMBus_SendBit
* Description    : Send a bit on SMBus 82.5kHz
* Input          : bit_out
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
    if(bit_out==0)
    {
        SMBUS_SDA_L();
    }
    else
    {
        SMBUS_SDA_H();
    }
    SMBus_Delay(2);					// Tsu:dat = 250ns minimum
    SMBUS_SCK_H();					// Set SCL line
    SMBus_Delay(6);					// High Level of Clock Pulse
    SMBUS_SCK_L();					// Clear SCL line
    SMBus_Delay(3);					// Low Level of Clock Pulse
//	SMBUS_SDA_H();				    // Master release SDA line ,
    return;
}

/*******************************************************************************
* Function Name  : SMBus_ReceiveBit
* Description    : Receive a bit on SMBus
* Input          : None
* Output         : None
* Return         : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
    u8 Ack_bit;

    SMBUS_SDA_H();          //Òý½Å¿¿Íⲿµç×èÉÏÀ­£¬µ±×÷ÊäÈë
	SMBus_Delay(2);			// High Level of Clock Pulse
    SMBUS_SCK_H();			// Set SCL line
    SMBus_Delay(5);			// High Level of Clock Pulse
    if (SMBUS_SDA_PIN())
    {
        Ack_bit=1;
    }
    else
    {
        Ack_bit=0;
    }
    SMBUS_SCK_L();			// Clear SCL line
    SMBus_Delay(3);			// Low Level of Clock Pulse

    return	Ack_bit;
}

/*******************************************************************************
* Function Name  : SMBus_ReceiveByte
* Description    : Receive a byte on SMBus
* Input          : ack_nack
* Output         : None
* Return         : RX_buffer
*******************************************************************************/
u8 SMBus_ReceiveByte(u8 ack_nack)
{
    u8 	RX_buffer;
    u8	Bit_Counter;

    for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
    {
        if(SMBus_ReceiveBit())			// Get a bit from the SDA line
        {
            RX_buffer <<= 1;			// If the bit is HIGH save 1  in RX_buffer
            RX_buffer |=0x01;
        }
        else
        {
            RX_buffer <<= 1;			// If the bit is LOW save 0 in RX_buffer
            RX_buffer &=0xfe;
        }
    }
    SMBus_SendBit(ack_nack);			// Sends acknowledgment bit
    return RX_buffer;
}

/*******************************************************************************
* Function Name  : SMBus_Delay
* Description    : ÑÓʱ  Ò»´ÎÑ­»·Ô¼1us
* Input          : time
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
   delay_us(time);
}

/*******************************************************************************
* Function Name  : SMBus_Init
* Description    : SMBus³õʼ»¯
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Init()
{

    SMBUS_SCK_H();
    SMBUS_SDA_H();
}

/*******************************************************************************
 * Function Name  : SMBus_ReadMemory
 * Description    : READ DATA FROM RAM/EEPROM
 * Input          : slaveAddress, command
 * Output         : None
 * Return         : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
    u16 data;			// Data storage (DataH:DataL)
    u8 Pec;				// PEC byte storage
    u8 DataL=0;			// Low data byte storage
    u8 DataH=0;			// High data byte storage
    u8 arr[6];			// Buffer for the sent bytes
    u8 PecReg;			// Calculated PEC byte storage
    u8 ErrorCounter;	// Defines the number of the attempts for communication with MLX90614

    ErrorCounter=0x00;				// Initialising of ErrorCounter
	slaveAddress <<= 1;	//2-7λ±íʾ´Ó»úµØÖ·
	
    do
    {
repeat:
        SMBus_StopBit();			    //If slave send NACK stop comunication
        --ErrorCounter;				    //Pre-decrement ErrorCounter
        if(!ErrorCounter) 			    //ErrorCounter=0?
        {
            break;					    //Yes,go out from do-while{}
        }

        SMBus_StartBit();				//Start condition
        if(SMBus_SendByte(slaveAddress))//Send SlaveAddress ×îµÍλWr=0±íʾ½ÓÏÂÀ´Ð´ÃüÁî
        {
            goto	repeat;			    //Repeat comunication again
        }
        if(SMBus_SendByte(command))	    //Send command
        {
            goto	repeat;		    	//Repeat comunication again
        }

        SMBus_StartBit();					//Repeated Start condition
        if(SMBus_SendByte(slaveAddress+1))	//Send SlaveAddress ×îµÍλRd=1±íʾ½ÓÏÂÀ´¶ÁÊý¾Ý
        {
            goto	repeat;             	//Repeat comunication again
        }

        DataL = SMBus_ReceiveByte(ACK);	//Read low data,master must send ACK
        DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK
        Pec = SMBus_ReceiveByte(NACK);	//Read PEC byte, master must send NACK
        SMBus_StopBit();				//Stop condition

        arr[5] = slaveAddress;		//
        arr[4] = command;			//
        arr[3] = slaveAddress+1;	//Load array arr
        arr[2] = DataL;				//
        arr[1] = DataH;				//
        arr[0] = 0;					//
        PecReg=PEC_Calculation(arr);//Calculate CRC
    }
    while(PecReg != Pec);		//If received and calculated CRC are equal go out from do-while{}

	data = (DataH<<8) | DataL;	//data=DataH:DataL
    return data;
}

/*******************************************************************************
* Function Name  : PEC_calculation
* Description    : Calculates the PEC of received bytes
* Input          : pec[]
* Output         : None
* Return         : pec[0]-this byte contains calculated crc value
*******************************************************************************/
u8 PEC_Calculation(u8 pec[])
{
    u8 	crc[6];
    u8	BitPosition=47;
    u8	shift;
    u8	i;
    u8	j;
    u8	temp;

    do
    {
        /*Load pattern value 0x000000000107*/
        crc[5]=0;
        crc[4]=0;
        crc[3]=0;
        crc[2]=0;
        crc[1]=0x01;
        crc[0]=0x07;

        /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
        BitPosition=47;

        /*Set shift position at 0*/
        shift=0;

        /*Find first "1" in the transmited message beginning from the MSByte byte5*/
        i=5;
        j=0;
        while((pec[i]&(0x80>>j))==0 && i>0)
        {
            BitPosition--;
            if(j<7)
            {
                j++;
            }
            else
            {
                j=0x00;
                i--;
            }
        }/*End of while */

        /*Get shift value for pattern value*/
        shift=BitPosition-8;

        /*Shift pattern value */
        while(shift)
        {
            for(i=5; i<0xFF; i--)
            {
                if((crc[i-1]&0x80) && (i>0))
                {
                    temp=1;
                }
                else
                {
                    temp=0;
                }
                crc[i]<<=1;
                crc[i]+=temp;
            }/*End of for*/
            shift--;
        }/*End of while*/

        /*Exclusive OR between pec and crc*/
        for(i=0; i<=5; i++)
        {
            pec[i] ^=crc[i];
        }/*End of for*/
    }
    while(BitPosition>8); /*End of do-while*/

    return pec[0];
}

 /*******************************************************************************
 * Function Name  : SMBus_ReadTemp
 * Description    : Calculate and return the temperature
 * Input          : None
 * Output         : None
 * Return         : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{   
	float temp;
	temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
	return temp;
}


.h

#ifndef __MLX90614_H
#define __MLX90614_H

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "tim.h"
/* Exported types ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void SMBus_StartBit(void);
void SMBus_StopBit(void);
void SMBus_SendBit(u8);
u8 SMBus_SendByte(u8);
u8 SMBus_ReceiveBit(void);
u8 SMBus_ReceiveByte(u8);
void SMBus_Delay(u16);
void SMBus_Init(void);
u16 SMBus_ReadMemory(u8, u8);
u8 PEC_Calculation(u8*);
float SMBus_ReadTemp(void); //»ñȡζÈÖµ

#endif

实际效果

在这里插入图片描述
非常灵敏
不愧是红外

<think>好的,用户想了解DS18B20温度传感器GY-906红外传感器的区别、优劣势以及适用场景。首先,我需要回顾这两种传感器的工作原理和特点。 DS18B20是常用的数字温度传感器,通过单总线协议通信,直接接触测量物体温度,适合液体或表面接触的环境。而GY-906基于红外热辐射原理,非接触式测量,适合移动物体或高温场景。 接下来,需要比较它们的测量方式、精度、响应时间、价格等方面。DS18B20精度在±0.5°C,而GY-906精度可能稍低,约±1°C。响应时间方面,DS18B20可能需要较长时间稳定,而红外传感器响应更快。 适用场景方面,DS18B20适合环境温度监测,如水温、土壤温度等;GY-906适合测量难以接触的物体,如电机表面、人体体温筛查等。 还要考虑成本,DS18B20价格较低,适合预算有限的项目。GY-906成本较高,但非接触式有其独特优势。 最后,需要检查是否有遗漏的对比点,比如通信接口、功耗、环境影响(如环境光对红外传感器的干扰)等,确保回答全面准确。</think>### DS18B20与GY-906传感器对比分析 #### 一、核心原理差异 1. **DS18B20** 接触式数字温度传感器,通过半导体材料的热敏特性测量物体表面温度,采用单总线协议传输数据[^1]。 数学表达式示例:温度转换公式为$T = \frac{Dig_{out} \times 0.0625}{16}$,其中$Dig_{out}$为数字输出值 2. **GY-906 (MLX90614)** 非接触式红外传感器,通过接收目标物体的红外辐射能量计算温度,集成I²C/PWM输出接口。遵循斯特藩-玻尔兹曼定律: $$P = \epsilon \sigma T^4$$ 其中$\sigma=5.67\times10^{-8}W/m^2K^4$为斯特藩常量 #### 二、性能参数对比 | 特性 | DS18B20 | GY-906 | |--------------|-----------------------|----------------------| | 测量范围 | -55°C ~ +125°C | -70°C ~ +380°C | | 测量精度 | ±0.5°C (典型值) | ±0.5°C (医疗级版本) | | 响应时间 | 750ms (最大值) | <10ms | | 测量距离 | 接触式 | 0~50cm (典型值) | | 通信接口 | 单总线 | I²C/PWM | | 功耗 | 1mA (工作) | 2mA (工作) | #### 三、应用场景选择 1. **DS18B20优势场景** - 液体温度测量(水箱、管道) - 土壤温度监测 - 低成本温控系统 - 需要多点组网的应用(单总线支持多设备) 2. **GY-906优势场景** - 运动物体温度检测(电机轴承、传送带) - 医疗体温筛查 - 高温物体测量(发动机、加热器) - 非接触式安防系统 #### 四、典型电路设计对比 ```c // DS18B20读取示例 float read_ds18b20() { DS18B20_Start(); // 启动转换 delay(750); // 等待转换完成 return DS18B20_Get_Temp();// 读取温度值 } // GY-906读取示例 float read_gy906() { Wire.requestFrom(0x5A, 3); // I²C地址0x5A return Wire.read() | Wire.read()<<8; // 组合数据位 } ```
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花椒且喵酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值