当前位置: 首页 > article >正文

基于STM32的非接触式红外测温系统

        基于STM32的非接触式红外测温系统采用vl53l1X进行测距,使用mlx90614进行测温,二者结合构成了一个简单的测温枪系统,数据使用OLED进行显示,系统设置有阈值,当温度高于阈值的时候,蜂鸣器会进行报警,表示温度过高,同时app可以显示温度、距离,并且集成了按键,可以实现无线修改阈值,由于App采取蓝牙和系统通信,数据时效性较高。

一、硬件设计

1、vl53l1X

        VL53L1X,一款由全球领先的光学传感器制造商设计的高精度、长距离飞行时间(TOF)激光测距传感器。该传感器采用创新的SPAD(单光子雪崩二极管)技术,能够实现从30厘米到4米的精确测距,精度高达±3%。VL53L1x在各种光照条件下均表现出色,无论是室内还是室外,白天还是夜晚,都能提供稳定可靠的测量结果。相对于普通的超声波测距,VL53L1X的测量精度更高,测量更为稳定,vl53l1x使用IIC协议,利用IIC协议对vl53l1x进行读写,可以得到最终的距离数据,无需过多处理。

2、mlx90614

        MLX90614,一款由Melexis公司精心研发的非接触式红外温度传感器,以其卓越的性能和广泛的工业应用而闻名。这款传感器采用先进的红外探测技术,能够在-70°C至+380°C的范围内进行非接触温度测量,精度高达±2°C。其核心优势在于能够快速、准确地测量目标物体的表面温度,无需物理接触,从而避免了传统接触式测量可能带来的热漂移或污染问题。MLX90614具备出色的响应速度,测量时间仅需0.5秒,适用于动态环境下的温度监控。这款传感器的集成度高,体积小巧,采用TO-92封装,易于集成到各种设备中,如工业自动化系统、医疗设备、智能家居产品等。其工作电压范围宽泛(2.7V至5.5V),功耗低,确保在多种供电条件下稳定运行。MLX90614还具备可配置的数字输出功能,支持PWM和数字温度信号输出,便于与微控制器接口,简化了系统设计和数据处理流程。无论是需要高精度温度测量的工业应用,还是追求小巧设计的消费电子产品,MLX90614都是理想的选择。mlx90614采取IIC通信,利用IIC协议读写mlx90614便可以完成温度的读取前不需要通过和物体接触。

二、软件设计

1、vl53l1X

        vl53l1X的驱动程序比较复杂,一般而言,厂家在出厂的时候会提供他的驱动函数,如果自己书写驱动,则比较复杂,如下:

        这些函数,如果细究起来是比较复杂的,作为一个应用开发者,我们只需要知道如何使用这些函数即可,如下:

#ifndef __VL53L1_H
#define __VL53L1_H

#include "stm32f10x.h"
#include "laser_def.h"
#include "VL53L1_register_map.h"
#include "VL53L1_api.h"
#include "VL53L1_platform.h"
//#include "VL53L1X_cali.h"
//#include "VL53L1_it.h"

//enable 2.8V IO mode
#define USE_I2C_2V8		1

//VL53L1X default I2C addr is 0x52 after power on
#define VL53L1_Addr0 0x52
#define VL53L1_Addr1 0x54

//detection mode
#define DEFAULT_MODE   0	//default,see manul 5.3.1
#define HIGH_ACCURACY  1
#define LONG_RANGE     2
#define HIGH_SPEED     3

//param struct for vl53l1x mode option, in manual 6.2 
typedef __packed struct
{
	FixPoint1616_t signalLimit;    //Signal,related to reflected amplitude 
	FixPoint1616_t sigmaLimit;     //Sigmal, related to distance mm
//	FixPoint1616_t ignoreThres;  //ignore threshold
	uint32_t timingBudget;         //When the ranging mode is set to timed ranging, user has to define the period of time
																 //between two consecutive measurements.
	uint8_t preRangeVcselPeriod ;  //VCSEL pulse cycle
	uint8_t finalRangeVcselPeriod ;//VCSEL pulse cycle period
}mode_data;


extern mode_data Mode_data[];
extern uint8_t Ajusted[];
extern VL53L1_RangingMeasurementData_t VL53L1_data[];
extern uint16_t Distance_data[];//the detected distance;
extern short Distance;//保存测距数据

VL53L1_Error VL53L1_addr_set(VL53L1_Dev_t *dev, uint8_t newaddr);
VL53L1_Error VL53L1_reset(VL53L1_Dev_t *pDev);
VL53L1_Error VL53L1_init(VL53L1_Dev_t *pDev);	//init vl53l1x
void VL53L1_test(void);//vl53l1x test

void VL53L1_info(void);//get vl53l1x ID info
void One_measurement(uint8_t mode);//获取一次测量距离数据

VL53L1_Error VL53L1_set_mode(VL53L1_Dev_t *dev,uint8_t mode);
VL53L1_Error VL53L1_single_test(VL53L1_Dev_t *dev,VL53L1_RangingMeasurementData_t *pdata);
void VL53L1_general_start(VL53L1_Dev_t *dev,uint8_t mode);
void VL53L1_general_test(VL53L1_Dev_t *dev);
#endif


2、mlx90614

        mlx90614,相对简单一些,我们只需要知道它的从机地址、RAM存取命令、EEPROM存取命令和目标1温度地址即可,如下:

#define ACK	 0 //应答
#define	NACK 1 //无应答
#define SA				0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a
#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度

        驱动代码如下:

/*******************************************************************************
* 文件名		: mlx90614.c
* 作  者	: 
* 版  本	: 
* 日  期	: 2013-08-07
* 描  述	: mlx90614函数
PB6:SCL
PB7:SDA
在主函数中先初始化SMBus_Init();
需要读取温度就调用temp=SMBus_ReadTemp();  //读取温度,temp是浮点数,转整数:i=ceil(temp); 
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include "mlx90614.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK	 0 //应答
#define	NACK 1 //无应答
#define SA				0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a
#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)
{
    u16 i, j;
    for (i=0; i<4; i++)
    {
        for (j=0; j<time; j++);
    }
}

/*******************************************************************************
* Function Name  : SMBus_Init
* Description    : SMBus初始化
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Init()
{
    GPIO_InitTypeDef    GPIO_InitStructure;

	/* Enable SMBUS_PORT clocks */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);

    /*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出*/
    GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);

    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;
}

/*********************************END OF FILE*********************************/

3、系统主程序

        主函数比较简单,只需要循环读取温度、距离数据,并在OLED显示即可,同时串口不断上传数据到我们的手机,数据格式是“0xA5 数据 校验位 0x5A”。如下:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Buzzer.h"
#include "timer.h"
#include "mlx90614.h"
#include "laser_def.h"
#include "vl53l1x.h"
#include "vl53l1x_i2c.h"
#include "Serial.h"

uint16_t Distance_data[2] = {0,0};
extern VL53L1_Dev_t VL53L1_dev[];	//2, device param, include I2C
extern uint8_t Ajusted[];//2, adjusted sign, 0-not, 1-had
extern VL53L1_RangingMeasurementData_t VL53L1_data[];//2, ranging result struct, distance, max distance,etc.
extern uint16_t Distance_data[];//2, the catched distance. VL53L1_data->RangeMilliMeter; 
/******************************************************************************/

int main(void)
{
	//定义变量
	uint8_t check = 0;
	uint8_t dis_h = 0;
	uint8_t dis_l = 0;
	uint8_t Status = 0;
	uint8_t temp=0;
	//OLED初始化
	OLED_Init();	
	//等待完成
	Delay_ms(100);	
	//显示提示
	OLED_ShowString(1, 1, "Dis:");
	OLED_ShowString(1, 9, "Tem:");
	OLED_ShowString(3, 1, "Tem_th:");
	//蜂鸣器初始化
	Buzzer_Init();
	Buzzer0=1;
	//MX90614初始化
	SMBus_Init();
	//等待完成
	Delay_ms(100);
	//距离传感器初始化
	Status = VL53L1_init(&VL53L1_dev[0]);
	//等待完成
	Delay_ms(100);
	//串口初始化
	Serial_Init();
	//等待完成
	Delay_ms(100);
	while (1)
	{
		//进行距离的获取
		Status = VL53L1_single_test(&VL53L1_dev[0],&VL53L1_data[0]);
		//获取成功(无错误)
		if(Status==VL53L1_Error_NONE)
		{
			//显示距离
			OLED_ShowNum(2, 1, Distance, 4);
		}
		else
		{
			
		}
		
		//当高于温度阈值的时候,蜂鸣器响
		if(temp>=tem_th){
			Buzzer0=0;
		}
		//否则,蜂鸣器不响
		else{
			Buzzer0=1;
		}
		
		//获取温度数据
		temp=SMBus_ReadTemp();
		//显示温度和温度阈值
		OLED_ShowNum(2, 9, temp, 3);
		OLED_ShowNum(4, 1, tem_th, 4);
		
		//发送数据到蓝牙助手,数据格式为0xA5 data1 data2 data_check(数据和校验) 0x5A
		Serial_SendByte(0xA5);
		Serial_SendByte(temp);
		dis_h = Distance%255;
		Serial_SendByte(dis_h);
		dis_l = Distance/255;
		Serial_SendByte(dis_l);
		check = (temp+dis_h+dis_l)%255;
		Serial_SendByte(check);
		Serial_SendByte(0x5A);
		Delay_ms(100);
	}
}

三、项目演示

        程序运行,OLED显示温度、距离、温度阈值等信息,如下:

        将手靠近测温传感器,让当前温度高于温度阈值,此时蜂鸣器会响起,如下:

        同时,APP实时更新温度、距离等信息,如下

        使用手机端的按键,可以实现阈值的调节,如下:

四、项目总结

        本次设计使用激光距离传感器和红外测温器件,实现了非接触式测温,类似于测温枪的原理,只需要靠近本系统就可以实现测温,十分方便,详请可参考本人的bilibili,,如下:

基于stm32的非接触式红外测距测温系统(蓝牙连接手机)_哔哩哔哩_bilibili


http://www.kler.cn/news/309049.html

相关文章:

  • 采用qt做一个命令行终端
  • 计算机人工智能前沿进展-大语言模型方向-2024-09-17
  • Vue点击按钮生成pdf文件/Vue点击按钮生成png图片
  • AJAX(一)HTTP协议(请求响应报文),AJAX发送请求,请求问题处理
  • SAM 2: Segment Anything in Images and Videos
  • c语言中“typedef”关键字,对类型进行重命名
  • 渗透测试入门学习——php文件上传与文件包含
  • ubuntu个人实用配置问题
  • 零基础小白能学网络安全吗?
  • 科技引领未来生活——“光影漫游者”展览馆应用—轻空间
  • Linux-mysql5.7-mysql8.0安装包下载及安装教程,二合一
  • 第J3-1周:DenseNet算法 实现乳腺癌识别(pytorch)
  • 语音识别(Speech Recongnition)
  • C语言-数据结构 有向图拓扑排序TopologicalSort(邻接表存储)
  • 基于LangChain的Embedding开发手册(保姆级)
  • SQL 基础知识
  • ubuntu20.04下载cuda11.8
  • Linux 系统
  • 清理C盘缓存的垃圾,专业清理C盘缓存垃圾与优化运行内存的策略
  • 026.(娱乐)魔改浏览器-任务栏图标右上角加提示徽章
  • C++ List (带你一篇文章搞定C++中的List类)
  • 复选按钮QCheckBox
  • 【C++】模版的进阶
  • 【Paper Reading】结合 NanoFlow 研究,优化大语言模型服务效率的探索
  • UE5中使用UTexture2D进行纹理绘制
  • 【OpenAPI】Spring3 集成 OpenAPI 生成接口文档
  • (web自动化测试+python)1
  • 金蝶云星空和金蝶云星空接口打通对接实战
  • Vite:快速构建现代Web应用的工具
  • 硬件工程师笔试面试——无线通讯模块