STM32 单片机 读取 GY906 ML90614非接触式红外传感器

本文介绍了如何利用STM32微控制器与GY-906红外温度传感器通信,通过SMBus协议获取数据,并结合HC-02蓝牙模块实现远程温度监测。重点展示了SMBus读取过程和蓝牙数据发送功能的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

单片机毕业设计-远望创客学堂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值