MLx90614介绍:
MLx90614非接触红外温度传感器是一款性能和精度都较高的温度测量产品,在医疗,工业等领域应用较多;有四个引脚,使用SMBus协议,通过控制SCL引脚进行数据读取。现在给出一个自己写得关于STM32实现温度采集,通过串口输出到上位机。
电路连接说明:
SDL引脚连接STM32芯片PB.12,SCL引脚连接STM32芯片PB.13引脚。
使用USART1作为串口输出
程序代码:
/*******************************************************************************
* 文件名 : mlx90614.c
* 作 者 :
* 版 本 :
* 日 期 : 2015-08-07
* 描 述 : mlx90614函数
*******************************************************************************/
#include "stm32f10x_lib.h"
#define ACK 0
#define NACK 1
#define SA 0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a
#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(GPIOB, GPIO_Pin_12)
#define SDA_H GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define SCL_H GPIO_SetBits(GPIOB, GPIO_Pin_13)
#define SCL_L GPIO_ResetBits(GPIOB, GPIO_Pin_13)
#define SMBUS_SDA_PIN GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) //读取引脚电平
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); //获取温度值
/*******************************************************************************
* Function Name : Mlx90614_Configuration
* Description : Mlx90614_Configuration
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Mlx96014_Configuration()
{
GPIO_InitTypeDefGPIO; //声明一个结构体变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//非接触温度传感器SDAL 连接PB.15,SCL连接PB.14---打开GPIOD时钟
// 温度传感器引脚配置
GPIO.GPIO_Pin = (GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_0|GPIO_Pin_1);//非接触温度传感器SDAL 连接PD.15,SCL连接PD.14
GPIO.GPIO_Speed = GPIO_Speed_50MHz;//管脚频率为50MHZ
GPIO.GPIO_Mode = GPIO_Mode_Out_OD;//输出模式为
GPIO_Init(GPIOB,&GPIO);//初始化GPIOB寄存器
SDA_H;
SCL_H; //因为SMBus是下降沿触发
}
/*******************************************************************************
* 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
}
/**************************************************