Skip to content

Instantly share code, notes, and snippets.

@FanhuaCloud
Created December 22, 2021 08:27
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "esp_log.h"
#define I2C_MASTER_FREQ_HZ 400000
#define I2C_MASTER_SDA_IO 4
#define I2C_MASTER_SCL_IO 5
#define AHT10_ADDR 0x38
static const char *TAG = "i2c";
void user_i2c_init(void)
{
int i2c_master_port = I2C_NUM_0;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO, // select GPIO specific to your project
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO, // select GPIO specific to your project
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ, // select frequency specific to your project
};
i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
}
void aht10_init(void)
{
uint8_t buf[3] = {0xE1, 0x08, 0x00};
i2c_master_write_to_device(I2C_NUM_0, AHT10_ADDR, buf, sizeof(buf), 1000 / portTICK_RATE_MS);
vTaskDelay(pdMS_TO_TICKS(500));
// uint8_t data;
// // 读取传感器状态
// i2c_cmd_handle_t cmd = i2c_cmd_link_create();
// i2c_master_start(cmd);
// i2c_master_write_byte(cmd, AHT10_ADDR << 1 | I2C_MASTER_READ, true);
// i2c_master_read(cmd, &data, 1, I2C_MASTER_ACK);
// i2c_master_stop(cmd);
// i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
// i2c_cmd_link_delete(cmd);
// ESP_LOGW(TAG, "data: %X", data);
}
void app_main(void)
{
user_i2c_init();
aht10_init();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment