MLX90614是一款红外非接触温度计,由内部状态机控制物体温度和环境温度的测量和计算,进行温度后处理,并将结果通过PWM或是SMBus模式输出。MCU主要通过SMBus协议与MLX90614通信来读取它的数据。
GY906模块主要分GY-906-BAA(或DAA,测距2CM),GY-906-BCC(或DCC,测距10CM)和GY-906-DCI(测距1M),这里使用的是GY906-DAA模块。
程序使用最新的STM32 HAL库产生, 与GY906模块通信的SDA SCL脚设置为开漏输出。
STM32使用IO口模拟来和GY906的SMBus总线通信,
启动信号:
停止信号:
调用SMBus_ReadMemory即可读取float型温度值:
/*******************************************************************************
* Function Name : SMBus_ReadMemory
* Description : READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Output : None
* Return : Data
*******************************************************************************/
uint16_t SMBus_ReadMemory(uint8_t slaveAddress, uint8_t command)
{
uint16_t data; // Data storage (DataH:DataL)
uint8_t Pec; // PEC byte storage
uint8_t DataL=0; // Low data byte storage
uint8_t DataH=0; // High data byte storage
uint8_t arr[6]; // Buffer for the sent bytes
uint8_t PecReg; // Calculated PEC byte storage
uint8_t 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(MLX90614_ACK); //Read low data,master must send ACK
DataH = SMBus_ReceiveByte(MLX90614_ACK); //Read high data,master must send ACK
Pec = SMBus_ReceiveByte(MLX90614_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;
}
与上文介绍的 HC-02蓝牙串口模块的配置和使用结合即可实现将GY906模块的温度值通过蓝牙发送到手机上。
技术支持请加Q,一起学习新知识。删除& —等特殊字符277&94一3&652。