STM32HAL库—AHT10温湿度传感器

STM32HAL库—AHT10温湿度传感器

STM32cubemx配置

在这里插入图片描述

代码部分

AHT10.h

#ifndef _AHT10_H_
#define _AHT10_H_

#include "i2c.h"

// 定义AHT10地址
#define AHT10_ADDRESS 0x70
#define AHT10_Write_ADDRESS 0x70
#define AHT10_Read_ADDRESS 0x71

// 定义AHT10命令
#define AHT10_Init_com 0xE1      // 1110 0001
#define AHT10_SoftReset_com 0xBA // 1011 1010
#define AHT10_TrigeMea_com 0xAC  // 1010 1100

void AHT10_Init(void);
uint8_t AHT10_Read_predata(float *humidity, float *temperature);

#endif

AHT10.c

#include "AHT10.h"

/**
 * @brief  AHT10 初始化
 * @param  void
 * @retval void
 */
void AHT10_Init(void)
{
    uint8_t senddata;
    senddata = AHT10_Init_com;
    HAL_I2C_Master_Transmit(&hi2c1, AHT10_Write_ADDRESS, &senddata, 1, 0xFFFF);
}

/**
 * @brief  AHT10 软复位
 * @param  void
 * @retval void
 */
void AHT10_SoftReset(void)
{
    uint8_t senddata;
    senddata = AHT10_SoftReset_com;
    HAL_I2C_Master_Transmit(&hi2c1, AHT10_Write_ADDRESS, &senddata, 1, 0xFFFF);
    HAL_Delay(20);
}

/**
 * @brief  AHT10触发测量
 * @param  void
 * @retval void
 */
void AHT10_TrigeMea(void)
{
    uint8_t senddata[3];
    senddata[0] = AHT10_TrigeMea_com;
    senddata[1] = 0x33; // DATA0
    senddata[2] = 0x00; // DATA1
    HAL_I2C_Master_Transmit(&hi2c1, AHT10_Write_ADDRESS, senddata, 3, 0xFFFF);
    HAL_Delay(80);
}

/**
 * @brief  AHT10 设备读取 相对湿度和温度
 * @param  float *humidity:湿度信息;
 *         float *temperature:温度信息。
 * @retval uint8_t:0-读取数据正常; 1-读取设备失败,设备一直处于忙状态,不能获取数据
 */
uint8_t AHT10_Read_predata(float *humidity, float *temperature)
{
    AHT10_TrigeMea();
    HAL_Delay(75);
    static uint32_t humi, temp;
    uint8_t readdata[6];
    uint8_t BusyIndication, CalEnable;
    HAL_I2C_Master_Receive(&hi2c1, AHT10_Read_ADDRESS, readdata, 6, 0xFFFF);
    BusyIndication = (readdata[0] >> 7) & 0x01; // 状态标志位
    CalEnable = (readdata[0] >> 3) & 0x01;      // 校准标志位
    if (CalEnable == 0)
    {
        AHT10_SoftReset();
    }
    if (BusyIndication == 0)
    {
        humi = (readdata[1] << 12) | (readdata[2] << 4) | (readdata[3] >> 4);
        temp = ((readdata[3] & 0x0F) << 16) | (readdata[4] << 8) | (readdata[5]);
        *humidity = ((humi * 100.0) / 1048576);
        *temperature = ((float)temp / 1048576) * 200 - 50;
        return 0;
    }
    return 1;
}

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,下面是基于STM32H750VBT6单片机AHT10湿度传感器程序的步骤: 1. 首先,需要连接STM32H750VBT6单片机AHT10传感器。将AHT10的VCC引脚连接到STM32H750VBT6的3.3V电源引脚,将GND引脚连接到STM32H750VBT6的GND引脚,将SCL引脚连接到STM32H750VBT6的SCL引脚,将SDA引脚连接到STM32H750VBT6的SDA引脚。 2. 在STM32CubeIDE中创建一个新的工程,然后选择STM32H750VBT6单片机作为目标设备。 3. 在工程中添加I2C库文件和AHT10湿度传感器驱动程序。 4. 配置I2C总线的时钟和GPIO引脚。可以使用STM32CubeIDE提供的配置向导来完成此操作。 5. 初始化AHT10传感器。可以使用以下代码来初始化: ```c void AHT10_Init(void) { uint8_t buf[3]; buf[0] = 0xE1; buf[1] = 0x08; buf[2] = 0x00; HAL_I2C_Master_Transmit(&hi2c1, AHT10_ADDR, buf, 3, 1000); } ``` 6. 读取AHT10传感器的度和湿度值。可以使用以下代码来读取: ```c void AHT10_ReadData(float *temperature, float *humidity) { uint8_t buf[6]; buf[0] = 0xAC; buf[1] = 0x33; buf[2] = 0x00; HAL_I2C_Master_Transmit(&hi2c1, AHT10_ADDR, buf, 3, 1000); HAL_Delay(100); HAL_I2C_Master_Receive(&hi2c1, AHT10_ADDR, buf, 6, 1000); *humidity = ((buf[1] << 12) | (buf[2] << 4) | (buf[3] >> 4)) * 100 / 0x100000; *temperature = ((buf[3] & 0x0F) << 16 | (buf[4] << 8) | buf[5]) * 200 / 0x100000 - 50; } ``` 7. 在主函数中调用AHT10_Init函数和AHT10_ReadData函数来获取度和湿度值。 ```c int main(void) { float temperature, humidity; HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init(); AHT10_Init(); while (1) { AHT10_ReadData(&temperature, &humidity); printf("Temperature: %.2f C\r\n", temperature); printf("Humidity: %.2f %%\r\n", humidity); HAL_Delay(1000); } } ``` 这样,就完成了基于STM32H750VBT6单片机AHT10湿度传感器程序的步骤。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值