首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在运行一个读取温湿度传感器(AHT10)的代码,但是输出始终是7。

我正在运行一个读取温湿度传感器(AHT10)的代码,但是输出始终是7。
EN

Stack Overflow用户
提问于 2022-06-01 04:13:03
回答 1查看 464关注 0票数 -1

下面给出了用ESP读取AHT10传感器的代码,输出结果如下所示。它不是读取温度,而是只打印7,始终。请帮我解决这个问题

代码语言:javascript
复制
########################### OUTPUT:######################
W (350) user: Hello world!
I (350) user: finished init
I (350) AHT10 I2C Read: I2C successfully initialized
I (410) user: byte= 7
I (410) user: temperature in degree celsius: 7.000000
I (410) user: temperature= 7.0
I (5470) user: byte= 7
I (5470) user: temperature in degree celsius: 7.000000
I (5470) user: temperature= 7.0        

############## BEGIN of CODE ############################
代码语言:javascript
复制
#include "esp_event.h"
#include "esp_log.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "driver/i2c.h"
#include <string.h>
#include "esp_err.h"


#define I2C_MASTER_NUM              0

#define ESP_ERR_AHT10_BASE                  0x30000
#define ESP_ERR_TOO_SLOW_TICK_RATE           (ESP_ERR_AHT10_BASE + 1)
#define ESP_ERR_AHT10_NOT_DETECTED          (ESP_ERR_AHT10_BASE + 2)
#define ESP_ERR_AHT10_CALIBRATION_FAILURE   (ESP_ERR_AHT10_BASE + 3)
// Tag for logging from this file
static const char* TAG = "user";
static const char *AHT10_I2C_LOG_TAG = "AHT10 I2C Read";


// slave address of aht10
uint8_t fd_AHT = 0x39;


uint8_t pl_i2c_read(uint8_t slave_addr) {
    // hold payload data to read
    uint8_t byte = 0;
    

    // create a command link which holds the sequence of
    // i2c commands to execute.
    i2c_cmd_handle_t cmd_link = i2c_cmd_link_create();

    // populate the command link with start bit, write slave
    // address (write bit 0), read payload byte and stop
    // bit.
    i2c_master_start(cmd_link);
    i2c_master_write_byte(cmd_link,
                          ((slave_addr << 1) | 0x01),
                          true);
    i2c_master_read_byte(cmd_link, &byte, true);
    i2c_master_stop(cmd_link);

    // execute commands and the read value will be saved to
    // 'byte'
    byte = i2c_master_cmd_begin(I2C_NUM_0,
                         cmd_link,
                         1000 / portTICK_PERIOD_MS);

    // delete command link
    i2c_cmd_link_delete(cmd_link);

    ESP_LOGV(TAG,
             "master read from 0x%02X: byte: 0x%02X",
             slave_addr,
             byte);

    // return the read value, invalid is 0xFF
    ESP_LOGI(TAG, "byte= %x", byte);
    return byte;
}


esp_err_t pl_i2c_init(gpio_num_t sda_pin,
                 gpio_num_t scl_pin,
                 uint32_t clock_speed) {
    // configuration object for i2c driver
    esp_err_t err;
    i2c_config_t i2c_conf;
    int i2c_master_port = I2C_MASTER_NUM;
    memset(&i2c_conf, 0, sizeof(i2c_config_t));
    // set parameters a master
    i2c_conf.mode = I2C_MODE_MASTER;
    i2c_conf.sda_io_num = sda_pin;
    i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    i2c_conf.scl_io_num = scl_pin;
    i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    i2c_conf.master.clk_speed = clock_speed;

    // apply configuration with port 0
    err = i2c_param_config(i2c_master_port, &i2c_conf);

    // install driver
    i2c_driver_install(i2c_master_port, i2c_conf.mode, 0, 0, 0);

    ESP_LOGI(TAG, "finished init");
    ESP_LOGI(AHT10_I2C_LOG_TAG, "I2C successfully initialized");

    if (err != ESP_OK) {
        ESP_LOGE(TAG, "I2C driver configuration failed with error = %d", err);
        return ESP_ERR_AHT10_NOT_DETECTED;
    }
    return ESP_OK;
}
    //end





// Get the temperature
int32_t al_aht10_get_ut(uint8_t slave_addr) {
    int32_t ut = 0;

    // delay time: 4.5ms = 4500µs
    vTaskDelay(5);

    // read out uncompensated temperature
    ut=pl_i2c_read(slave_addr);
 
    return ut;
}



int32_t al_aht10_get_temperature() {
    float t = al_aht10_get_ut(fd_AHT);
    // algorithm for the temperature

    // divide the temperature by 10 to get the value in multiples of 1.0 celsius
    ESP_LOGD(TAG,
             "temperature in degree celsius: %.1f",
             t);
    ESP_LOGI(TAG, "temperature in degree celsius: %f",
             t);

    return t;
}

// application entry point
void app_main() {
    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    
    // log on app start as warning to make it visible
    ESP_LOGW(TAG, "Hello world!");

    esp_log_level_set("user", ESP_LOG_DEBUG);


        
    //esp_err_t err;
    pl_i2c_init(GPIO_NUM_22, GPIO_NUM_23, 400000);

    // now init the aht10 itself
    while (1) {    

        float te=al_aht10_get_temperature();
        
        ESP_LOGI(TAG, "temperature= %.1f", te);
        //ESP_LOGI(TAG, "pressure= %X", p);

        vTaskDelay(5000 / portTICK_PERIOD_MS);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-01 06:20:59

i2c_master_cmd_begin的返回值为esp_err_t,而不是i2c的红色值。

创建如下内容: esp_err_t、returnValue = i2c_master_cmd_begin等,并研究返回值。试试看,如果你被困住了,就再问一遍!(返回值将为您提供一些关于正在发生的事情的线索)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72456377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档