ESP32s3与Lsm6ds3通信---i2c【开源】

接线

ESPS3,I2C的初始化

复制代码
#ifdef __cplusplus
extern "C" {
#endif
#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          1000000                     /*!< 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 LED_GPIO_1  GPIO_NUM_25
static esp_err_t i2c_master_init(void);
#ifdef __cplusplus
}
#endif
/**
 * @brief i2c master initialization
 */
static esp_err_t i2c_master_init(void){

	i2c_config_t conf;
	conf.mode = I2C_MODE_MASTER;
	conf.sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO;
	conf.scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO;
	conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
	conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
	conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
    conf.clk_flags = 0;
	ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
	return i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
}

主要任务

该任务主要是把Lsm6ds3的数据读取出来,并打印

复制代码
void lsm6ds3(void *pvParameters){
	// Initialize device
	if (imu.begin() == 0) {
		ESP_LOGE(TAG, "Connection fail");
		vTaskDelete(NULL);
	}else{
		ESP_LOGE(TAG, "Connection successfully");		
	}
	// double accX, accY, accZ;
	// double gyroX, gyroY, gyroZ;
	// _getMotion6(&accX, &accY, &accZ, &gyroX, &gyroY, &gyroZ);
	// ESP_LOGE(TAG,"%f %f %f - %f %f %f\n", accX, accY, accZ, gyroX, gyroY, gyroZ);
	// vTaskDelete(NULL);
	
	// Set Kalman and gyro starting angle
	double accX, accY, accZ;
	double gyroX, gyroY, gyroZ;

int ioioi =0;
	while(1){
		
		_getMotion6(&accX, &accY, &accZ, &gyroX, &gyroY, &gyroZ);

		gpio_set_level(GPIO_NUM_48, ioioi);
		ioioi = !ioioi;
		//     int64_t time_since_boot = esp_timer_get_time();
    	//ESP_LOGI("TIME", "Time since boot: %lld microseconds", time_since_boot);
		ESP_LOGE(TAG,"%f %f %f - %f %f %f", accX, accY, accZ, gyroX, gyroY, gyroZ);
		//printf("%f %f %f - %f %f %f/n", accX, accY, accZ, gyroX, gyroY, gyroZ);
	}
}

核心代码

主要就是把esp-idf写好的I2C接口,和LSM6DS3的驱动接上,分别是读和写数据

复制代码
int LSM6DS3::readRegisters(uint8_t address, uint8_t* data, size_t length)
{
    esp_err_t ret = i2c_master_write_read_device(I2C_NUM_0, CONFIG_I2C_ADDR,
                                                 &address, 1, data, length, 100 / portTICK_RATE_MS);
    return 1;
}

int LSM6DS3::writeRegister(uint8_t address, uint8_t value)
{
    uint8_t write_buf[2] = { address, value };
    esp_err_t ret = i2c_master_write_to_device(I2C_NUM_0, CONFIG_I2C_ADDR,
                                               write_buf, sizeof(write_buf), 100 / portTICK_RATE_MS);
    return 1;
}

开源地址

链接:https://pan.baidu.com/s/1Tbnz3A4ykt20kQf2UcANWA

提取码:oy8u

--来自百度网盘超级会员V4的分享

相关推荐
HelloGitHub21 小时前
让 AI 记住我家狗叫「十六」,原来只需要 5 分钟
开源·github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2025-11-04)
开源·github·ai编程·github热榜
wuk9982 天前
基于开源操作系统搭建K8S高可用集群
容器·kubernetes·开源
ApacheSeaTunnel2 天前
LLM 时代,DataAgent × WhaleTunnel 如何将数据库变更瞬时 “转译” 为洞察?
大数据·ai·开源·llm·数据同步·白鲸开源·whaletunnel
MobotStone2 天前
告别就业焦虑!普通人靠AI“工具”创业的3条捷径
开源
武子康2 天前
AI研究-118 具身智能 Mobile-ALOHA 解读:移动+双臂模仿学习的开源方案(含论文/代码/套件链接)
人工智能·深度学习·学习·机器学习·ai·开源·模仿学习
jonyleek2 天前
项目管理太混乱?开源的私有化项目管理系统了解一下!
开源·项目管理·团队开发·甘特图·软件开发·项目管理系统
HelloGitHub2 天前
降薪跳槽,投身开源!只为 AI 落地“最后一公里”
开源·github
RWKV元始智能3 天前
RWKV7-G0a3 13.3B 发布:世界最强纯 RNN 大语言模型
人工智能·机器学习·开源
oioihoii3 天前
不止于Linux:百花齐放的开源世界与社区的力量
linux·运维·开源