基于STM32的GY906红外测温

这篇具有很好参考价值的文章主要介绍了基于STM32的GY906红外测温。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

本文实现了利用GY906红外测温模块检测温度。通过定时器中断取平均获得更优的温度曲线。

一、GY906代码

c文件

#include "gy906.h"

/*******************************************************************************
* Function Name  : Mlx90614_Configuration
* Description    : Mlx90614_Configuration
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Mlx96014_Configuration(void)
{
		GPIO_InitTypeDef  GPIO_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
	
		GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;//SCL
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
		GPIO_Init(GPIOB,&GPIO_InitStructure);
   
	  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;//SDA
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
		GPIO_Init(GPIOA,&GPIO_InitStructure);
		SDA_H;
		SCL_H; 
}

/*******************************************************************************
* Function Name  : SMBus_StartBit
* Description    : Generate START condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_StartBit(void)
{
    SDA_H;               // Set SDA line 
    SMBus_Delay(1);      // Wait a few microseconds 
    SCL_H;               // Set SCK line  
    SMBus_Delay(5);      // Generate bus free time between Stop
    SDA_L;               // Clear SDA line
    SMBus_Delay(10);     // Hold time after (Repeated) Start
                         // Condition. After this period, the first clock is generated.
                         //(Thd:sta=4.0us min)
    SCL_L;               // Clear SCK line
    SMBus_Delay(2);      // Wait a few microseconds
}

/*******************************************************************************
* Function Name  : SMBus_StopBit
* Description    : Generate STOP condition on SMBus
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/

void SMBus_StopBit(void)
{
    SCL_L;                // Clear SCK line
    SMBus_Delay(5);       // Wait a few microseconds
    SDA_L;                // Clear SDA line
    SMBus_Delay(5);       // Wait a few microseconds
    SCL_H;                // Set SCK line
    SMBus_Delay(10);      // Stop condition setup time(Tsu:sto=4.0us min)
    SDA_H;                // Set SDA line
}

/*******************************************************************************
* 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
* Input          : bit_out
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
    if(bit_out==0)
    {
      SDA_L;   
    }
    else
    {
    SDA_H;
    }
    SMBus_Delay(2);                            // Tsu:dat = 250ns minimum
    SCL_H;                                     // Set SCK line
    SMBus_Delay(10);                           // High Level of Clock Pulse
    SCL_L;                                     // Clear SCK line
    SMBus_Delay(10);                           // 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;


    SDA_H;             //?????????,????
    SCL_H;             // Set SCL line
    SMBus_Delay(2);    // High Level of Clock Pulse
    if (SMBUS_SDA_PIN)
    {
        Ack_bit=1;
    }
    else
    {
        Ack_bit=0;
    }
    SCL_L;                    // Clear SCL line
    SMBus_Delay(4);           // 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_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)
{   
    return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
}
/*********************************END OF FILE*********************************/


h文件

#ifndef _GY906_H_
#define _GY906_H_ 

#include "delay.h"
#include "sys.h"

#define ACK           0
#define NACK          1
#define SA            0x00 //Slave address 
#define RAM_ACCESS    0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ1     0x07 //To1 address in the eeprom


#define SDA_L     GPIO_ResetBits(GPIOA, GPIO_Pin_8)
#define SDA_H     GPIO_SetBits(GPIOA, GPIO_Pin_8)  
#define SCL_H     GPIO_SetBits(GPIOB, GPIO_Pin_15)
#define SCL_L     GPIO_ResetBits(GPIOB, GPIO_Pin_15)


#define SMBUS_SDA_PIN    GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) 

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); 
void Mlx96014_Configuration(void);

#endif

二、定时器中断代码

#include "stm32f10x.h"  //包含需要的头文件
#include "timer1.h"
#include "gy906.h"

/*-------------------------------------------------*/
/*函数名:定时器1使能1分钟定时                     */
/*参  数:无                                       */
/*返回值:无                                       */
/*-------------------------------------------------*/
void TIM1_Time_Init(u16 arr,u16 psc)
{
		//TIM1_Init(4999,7199);
	  //计数频率=Tclk/7200=72000000/7200=10000Hz  计一个数用时1/10000s  计数5000次=5000/10000s=500ms
	  //Tout(溢出时间)=(arr+1)*(psc+1)/Tclk =5000*7200/72000000s=500ms
		TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		NVIC_InitTypeDef NVIC_InitStructure;

		RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); 
		
		TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);//设置缺省值,这一步最好加上
		TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载寄存器周期的值,溢出值	
		TIM_TimeBaseStructure.TIM_Prescaler =psc; //时钟频率预分频值
		TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:输入捕获模式用来滤波
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	    TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//设置重复溢出次数,就是多少次溢出后进入中断,一般为0,只有高级定时器才有用
		TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 

		TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //允许更新中断

		NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; 
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;  
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
		NVIC_Init(&NVIC_InitStructure);  

		TIM_Cmd(TIM1, ENABLE);  
}
float Temperature=0;
/*-------------------------------------------------*/
/*函数名:串口1接收中断函数                        */
/*参  数:无                                       */
/*返回值:无                                       */
/*-------------------------------------------------*/
void TIM1_UP_IRQHandler(void)   
{
	  static u8 i=0;
	  static float add=0.0f;
		if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)  //检查更新中断发生与否
		{
			i++;  
			add+=SMBus_ReadTemp();                             //得到温度数据
			if(i>=50)
			{
				Temperature = add/50  +  1.5000;
        add=0;
        i=0;				
			}
			TIM_ClearITPendingBit(TIM1, TIM_IT_Update);  //清除TIMx更新中断标志 
		}
}

Temperature即为检测到的数据,精度还可以,只不过对于距离需要控制一下,大概5-10cm左右的距离。Temperature累加那里加上1.5000是增加1.5度的意思,这用于线性修正。我寻思我的体温是36.5度左右,测出来35,所以就加了一个修正系数哈哈哈。

总结

三连支持博主,博主更新的动力源泉!!!文章来源地址https://www.toymoban.com/news/detail-837987.html

到了这里,关于基于STM32的GY906红外测温的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STM32--光照强度传感器(BH1750 、GY302)

    代码链接,附文章下方。 本系列文章,只聊快速应用, 3分钟上手使用速读。需要详尽底层原理的网友,请另行搜索。 目录 1、BH1750  特点、参数 2、接线说明 3、代码速读 4、实验效果 5、代码下载链接 淘宝有两种BH1750模块,一长一短两款,短款更常见,本篇使用下图中短款

    2024年04月14日
    浏览(31)
  • STM32案例学习 GY-39环境监测传感器模块

    野火STM32F1系列开发板 正点STM32F1系列开发板 STM32F103ZET6核心板 GY-39环境监测传感器模块 GY-39 是一款低成本,气压,温湿度,光强度传感器模块。工作电压 3-5v,功耗小,安装方便。 其工作原理是,MCU 收集各种传感器数据,统一处理,直接输出计算后的结果。此模块,有两种方

    2024年02月12日
    浏览(32)
  • 基于STM32单片机的智能家居窗户窗帘控制系统红外遥控proteus仿真原理图PCB

    功能: 0.本系统采用STM32F103RCT6作为单片机 1.LCD1602液晶实时显示当前时间/窗帘状态/光照强度 2.支持手动按键/定时/光强检测三种模式控制窗帘 3.\\\'设置’键进入设置界面 4.默认状态下’确定’键可切换模式 5.设置界面’确定’为确定 6.支持掉电保存 7.采用DC002作为电源接口可直

    2024年02月11日
    浏览(36)
  • 105-基于stm32单片机智能家居温湿度烟雾监测人体红外防盗报警系统Proteus仿真+程序源码

    一:功能介绍 1、采用stm32单片机+LCD1602+DHT11温湿度传感器+烟雾传感器+按键开关+蜂鸣器+LED灯,制作一个智能家居温湿度烟雾监测人体红外防盗报警系统; 2、通过按键开关来模拟人体红外检测触发,并且可以通过按键开关来设置正在布防和停止布防状态,布防状态下,当检测

    2024年02月03日
    浏览(38)
  • 【stm32单片机基础】红外NEC协议解码

    红外通信协议是一种基于红外线的传输技术,广泛使用的家电遥控器几乎都是采用的红外线传输技术,由于红外线为不可见光,对环境影响很小,红外线遥控不会影响其他家用电器,也不会影响临近的无线电设备。红外遥控的编码方式目前广泛使用的是: PWM(脉冲宽度调制)的

    2024年02月01日
    浏览(28)
  • K_A12_004 基于STM32等单片机采集人体红外感应(HC-SR501)模块串口与OLED0.96双显示

    单片机型号 测试条件 模块名称 代码功能 STC89C52RC 晶振11.0592M HC-SR501模块 STC89C52RC采集HC-SR501模块参数 串口与OLED0.96双显示 STM32F103C8T6 晶振8M/系统时钟72M HC-SR501模块 STM32F103C8T6采集HC-SR501模块参数 串口与OLED0.96双显示 其他资料目录 直戳跳转 HC-SR501模块 引脚说明 VCC 正极 3.3-5V供电

    2024年01月19日
    浏览(36)
  • STM32模拟IIC与IIC四种实现数字光强采集模块GY30(标准库与HAL库)

    目录 代码实现是的IIC通信,数据采集后在串口显示,方便大家实现二次开发 原件选择 GY-30 数字光强度介绍 BH1750芯片参数 引脚说明  BH1750指令集 接线表设计 通过四种方式实现GY-30数据采集 1.标准库模拟IIC实现GY-30采集并串口1显示  2.标准库IIC实现GY-30采集并串口1显示 3.HAL库

    2023年04月26日
    浏览(50)
  • stm32毕设分享 单片机远程wifi红外无接触体温测量系统 - 物联网 stm32

    🔥 这两年开始毕业设计和毕业答辩的要求和难度不断提升,传统的毕设题目缺少创新和亮点,往往达不到毕业答辩的要求,这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。 为了大家能够顺利以及最少的精力通过毕设,学长分享优质毕业设计项目,今天

    2024年02月19日
    浏览(57)
  • 101、基于STM32单片机智能输液器红外点滴液位监控报警系统设计(程序+原理图+PCB源文件+参考论文+硬件设计资料+元器件清单等)

    单片机主芯片选择方案 方案一:AT89C51是美国ATMEL公司生产的低电压,高性能CMOS型8位单片机,器件采用ATMEL公司的高密度、非易失性存储技术生产,兼容标准MCS-51指令系统,片内置通用8位中央处理器(CPU)和Flash存储单元,功能强大。其片内的4K程序存储器是FLASH工艺的,这种单

    2024年02月13日
    浏览(51)
  • 【单片机毕设选题】单片机远程wifi红外无接触体温测量系统 - 物联网 stm32

    🔥 这两年开始毕业设计和毕业答辩的要求和难度不断提升,传统的毕设题目缺少创新和亮点,往往达不到毕业答辩的要求,这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。 为了大家能够顺利以及最少的精力通过毕设,学长分享优质毕业设计项目,今天

    2024年02月20日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包