Infrared temperature measurement
Article directory
foreword
1. Principle
The principle of infrared temperature measurement can go directly to the seller's manual. There are too many redundant words in the manual. Just know that it is IIC communication. The
code is directly given below
Two, STM32 code
1.MLX90614.c
#include "MLX90614.h"
#include "sys.h"
#include "delay.h"
u8 bit_in=0;
u8 bit_out=0;
u8 DataL;
u8 DataH;
u8 Pecreg;
/******************************************************************************
* Mlx90614_Init()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : PA5--MLX90614_SCL
PA6--MLX90614_SDA
*******************************************************************************/
void Mlx90614_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PORTA口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PORTA5 推挽输出,速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PORTA6 推挽输出,速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/******************************************************************************
* start_bit()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 开始位
*******************************************************************************/
void start_bit()
{
GPIOA6_High;
delay_us(1);
GPIOA5_High;
delay_us(1);
GPIOA6_Low;
delay_us(1);
GPIOA5_Low;
delay_us(1);
}
/******************************************************************************
* stop_bit()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 开始位
*******************************************************************************/
void stop_bit()
{
GPIOA5_Low;
delay_us(1);
GPIOA6_Low;
delay_us(1);
GPIOA5_High;
delay_us(1);
GPIOA6_High;
}
/******************************************************************************
* send_bit()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 发送一个字节
*******************************************************************************/
void send_bit(void)
{
if(bit_out==0)
GPIOA6_Low;
else
GPIOA6_High;
delay_us(1);
GPIOA5_High;
delay_us(1);
GPIOA5_Low;
delay_us(1);
}
/******************************************************************************
* receive_bit()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 接收一个位
*******************************************************************************/
void receive_bit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIOA6_High;
bit_in=1;
GPIOA5_High;
delay_us(1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PORTA6 浮动输入,速度50MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
bit_in=SDA;
delay_us(1);
GPIOA5_Low;
delay_us(1);
//PORTA6 推挽输出,速度50MHz
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/******************************************************************************
* rx_byte()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 接收一个字节
*******************************************************************************/
u8 rx_byte(void)
{
u8 i,dat;
dat=0;
for(i=0;i<8;i++)
{
dat=dat<<1;
receive_bit();
if(bit_in==1)
{
dat=dat+1;
}
}
send_bit();
return dat;
}
/******************************************************************************
* tx_byte()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : none
*Notes : 发送一个字节
*******************************************************************************/
void tx_byte(u8 dat_byte)
{
char i,n,dat;
n=10;
TX_again:
dat=dat_byte;
for(i=0;i<8;i++)
{
if(dat&0x80)
bit_out=1;
else
bit_out=0;
send_bit();
dat=dat<<1;
}
receive_bit();
if(bit_in==1)
{
stop_bit();
if(n!=0)
{
n--;goto Repeat;}
else
goto exit;
}
else
goto exit;
Repeat:
start_bit();
goto TX_again;
exit: ;
}
/******************************************************************************
* memread()
*
*Description : STM32外设以及传感器初始化
*Arguments : none
*Returns : 温度
*Notes : 读取温度
*******************************************************************************/
u16 memread(void)
{
start_bit();
// tx_byte(0xB4); //Send SlaveAddress ==============================
tx_byte(0x00);
tx_byte(0x07); //Send Command
//------------
start_bit();
tx_byte(0x01);
bit_out=0;
DataL=rx_byte();
bit_out=0;
DataH=rx_byte();
bit_out=1;
Pecreg=rx_byte();
stop_bit();
return(DataH*256+DataL);
}
2.MLX90614.h
#ifndef __MLX90614_H
#define __MLX90614_H
#include "sys.h"
#define GPIOA5_High GPIO_SetBits(GPIOA, GPIO_Pin_5)
#define GPIOA5_Low GPIO_ResetBits(GPIOA, GPIO_Pin_5)
#define GPIOA6_High GPIO_SetBits(GPIOA, GPIO_Pin_6)
#define GPIOA6_Low GPIO_ResetBits(GPIOA, GPIO_Pin_6)
#define SDA GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6)
//************ 函数声明*****************************************
void Mlx90614_Init(void);
void start_bit();
void stop_bit();
void send_bit(void);
void receive_bit(void);
u8 rx_byte(void);
void tx_byte(u8 dat_byte);
u16 memread(void);
#endif
main function
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "MLX90614.h"
int main(void)
{
u16 Tem;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
uart_init(9600); //串口初始化为9600
Mlx90614_Init();
GPIOA5_High;
GPIOA6_High;
delay_us(1);
GPIOA5_Low;
delay_ms(1000);
GPIOA5_High;
while(1)
{
Tem=memread();
printf("温度是:");
printf("%d",Tem);
printf("/n");
delay_ms(1000);
}
}
Put these three pieces of code into the project and then print the temperature through the serial port. I use the temperature displayed on the display.
Summarize
In fact, after understanding IIC, it is very easy to write code for this IIC communication module.