使用STM32的GY906红外测温技术

文章目录

  • 前言
  • 一、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,所以就加了一个修正系数哈哈哈。

    总结

    三连支持博主,博主更新的动力源泉!!!

    作者:技术小董

    物联沃分享整理
    物联沃-IOTWORD物联网 » 使用STM32的GY906红外测温技术

    发表回复