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的分享

相关推荐
说私域1 天前
电商价格战下的创新破局:定制开发开源AI智能名片S2B2C商城小程序的应用与价值
人工智能·小程序·开源
说私域1 天前
融合“开源链动2+1模式AI智能名片S2B2C商城小程序”:同城自媒体赋能商家私域流量增长的新路径
人工智能·小程序·开源
king王一帅1 天前
Incremark 0.3.0 发布:双引擎架构 + 完整插件生态,AI 流式渲染的终极方案
前端·人工智能·开源
秋天的一阵风1 天前
🎥解决前端 “复现难”:rrweb 录制回放从入门到精通
前端·javascript·开源
半壶清水1 天前
【开源免费】使用 Python + Whisper + PyDub 自动切割长音频文件
开发语言·python·语言模型·开源·whisper
编程武士2 天前
开源项目 osv-scanner:谷歌出品的依赖漏洞扫描工具
开源
IvorySQL2 天前
让源码安装不再困难:IvorySQL 一键安装脚本的实现细节解析
数据库·人工智能·postgresql·开源
CoderJia程序员甲2 天前
GitHub 热榜项目 - 日榜(2026-1-6)
ai·开源·大模型·github·ai教程
openFuyao2 天前
参与openFuyao嘉年华,体验开源开发流程,领视频年卡会员
人工智能·云原生·开源·开源软件·多样化算力
一念杂记2 天前
【免费网盘】1分钟快速搭建免费专属网盘,旧电脑&旧手机均可使用~
开源