【ESP32】调试IIC功能

1.创建示例项目i2c_simple:VSCODE中->“查看”->”命令面板“->输入:Show Examples projects->选择Use current ESP-IDF(C:\Espressif\frameworks\esp-idf-v5.1)->弹出示例ESP-IDF Examples,选择peripherals->i2c->i2c_simple->Create project using example i2c_simple->选择示例保存的路径
【ESP32】调试IIC功能_第1张图片

【ESP32】调试IIC功能_第2张图片

2.选择esp32型号
3.选择COM口
4.选择烧录方式
5.点击“menuconfig”按键,根据ESP32技术参考手册中的IO_MUX Pad 列表,配置i2c引脚信息,点击保存
【ESP32】调试IIC功能_第3张图片

6.点击“编译”按键
7.点击“烧录”按键
8.点击“监控”按键,可以看到初始化和读取ADXL345测试打印正常(上图)
完整代码如下

/* i2c - Simple example

   Simple I2C example that shows how to initialize I2C
   as well as reading and writing from and to registers for a sensor connected over I2C.

   The sensor used in this example is a MPU9250 inertial measurement unit.

   For other examples please check:
   https://github.com/espressif/esp-idf/tree/master/examples

   See README.md file to get detailed usage of this example.

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include 
#include "esp_log.h"
#include "driver/i2c.h"
#include "driver/gpio.h"

static const char *TAG = "i2c-simple-example";

#define I2C_MASTER_SCL_IO           CONFIG_I2C_MASTER_SCL      /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO           CONFIG_I2C_MASTER_SDA      /*!< GPIO number used for I2C master data  */
#define I2C_MASTER_NUM              0                          /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_FREQ_HZ          400000                     /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE   0                          /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE   0                          /*!< I2C master doesn't need buffer */
#define I2C_MASTER_TIMEOUT_MS       1000

#define ADXL345_SENSOR_ADDR                 0x53        /*!< Slave address of the MPU9250 sensor */
/**
 * @brief i2c master initialization
 */
static esp_err_t i2c_master_init(void)
{
    int i2c_master_port = I2C_MASTER_NUM;

    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
    };

    i2c_param_config(i2c_master_port, &conf);

    return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}

/**
 * @brief 传感器电源开关
 */
static void sensor_switch(bool status)
{
    gpio_config_t conf = {
        .pin_bit_mask = 1 << GPIO_NUM_27,
        .mode = GPIO_MODE_OUTPUT,
        .pull_up_en = GPIO_PULLUP_ENABLE,
        .pull_down_en = GPIO_PULLUP_PULLDOWN,
        .intr_type = GPIO_INTR_DISABLE,
    };
    gpio_config(&conf);
    gpio_set_level(GPIO_NUM_27, status);
}

/**
 * @brief ADXL345初始化
 */
static esp_err_t ADXL345_Init(void)
{
    esp_err_t err = ESP_OK;
    uint8_t write_buffer[2] = {0} ;

    write_buffer[0] = 0x31; write_buffer[1] = 0x0B;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x2C; write_buffer[1] = 0x0A;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x2D; write_buffer[1] = 0x08;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x2E; write_buffer[1] = 0x80;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x1E; write_buffer[1] = 0x00;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x1F; write_buffer[1] = 0x00;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x20; write_buffer[1] = 0x01;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    write_buffer[0] = 0x2D; write_buffer[1] = 0x00;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    return err;
}

/**
 * @brief 连续读出ADXL345内部加速度数据,地址范围0x32~0x37
 */
static esp_err_t ADXL345_Get_Acc(float *ax,float *ay,float *az)
{
    esp_err_t err = ESP_OK;
    uint8_t write_buffer[2] = {0} ;
    int8_t acc_buffer[6] = {0} ;

    // //使能 DATA_READY 中断
    // write_buffer[0] = 0x2E; write_buffer[1] = 0x80;
    // err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    // if (err != ESP_OK) {
    //     return err;
    // }

    //唤醒
    write_buffer[0] = 0x2D; write_buffer[1] = 0x08;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    for(uint64_t i = 0; i < 100000; i++)
    {

    }

    write_buffer[0] = 0x32;
    err = i2c_master_write_read_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR, write_buffer, 1, (uint8_t *)acc_buffer, sizeof(acc_buffer), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
    return err;
    }

    // //禁止 DATA_READY 中断
    // write_buffer[0] = 0x2E; write_buffer[1] = 0x00;
    // err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    // if (err != ESP_OK) {
    //     return err;
    // }

    //待机
    write_buffer[0] = 0x2D; write_buffer[1] = 0x00;
    err = i2c_master_write_to_device(I2C_MASTER_NUM, ADXL345_SENSOR_ADDR,write_buffer, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
    if (err != ESP_OK) {
        return err;
    }

    for(uint8_t i = 0; i < sizeof(acc_buffer); i++)
    {
        ESP_LOGI(TAG, "acc_buffer[%d] = 0x%02X\r\n", i, acc_buffer[i]);
    }

    *ax = ((int)((acc_buffer[1] << 8) | acc_buffer[0]))*3.9/1000;//取出X轴数据,3.9LSB/mg
    *ay = ((int)((acc_buffer[3] << 8) | acc_buffer[2]))*3.9/1000;//取出Y轴数据,3.9LSB/mg
    *az = ((int)((acc_buffer[5] << 8) | acc_buffer[4]))*3.9/1000;//取出Z轴数据,3.9LSB/mg

    ESP_LOGI(TAG, "ax = %f, ay = %f, az = %f\r\n", *ax, *ay, *az);

    return err;
}

void app_main(void)
{
    sensor_switch(true);

    ESP_ERROR_CHECK(i2c_master_init());
    ESP_LOGI(TAG, "I2C initialized successfully");

    /* Read the MPU9250 WHO_AM_I register, on power up the register should have the value 0x71 */
    ESP_ERROR_CHECK(ADXL345_Init());
    ESP_LOGI(TAG, "ADXL345 initialized successfully");

    float ax = 0;
    float ay = 0;
    float az = 0;
    ESP_ERROR_CHECK(ADXL345_Get_Acc(&ax, &ay, &az));
    ESP_LOGI(TAG, "ADXL345 read successfully");
}

你可能感兴趣的:(vscode,ide,iic,adxl345,esp32)