Trouble understanding I2C examples
Posted: Tue Jul 20, 2021 4:32 am
Hi,
I am trying to use an accelerometer via I2c however I'm finding it difficult to understand what I need to do, I am using this guide to get the accelerometer started (I have done this successfully previously using Arduino Wire() function for standard I2C stuff) and this is the code I've got so far:
Init Function (Based off this example)
Read function:
I just hope that I'm heading in the right direction but I'm still not sure how to finish up the read function so that I actually can print out the data?
I'm posting this as unfinished code as it's been hurting my head for a while now and I'd just be grateful for any pointers in the correct direction.
Cheers
I am trying to use an accelerometer via I2c however I'm finding it difficult to understand what I need to do, I am using this guide to get the accelerometer started (I have done this successfully previously using Arduino Wire() function for standard I2C stuff) and this is the code I've got so far:
Init Function (Based off this example)
Code: Select all
static esp_err_t i2c_master_sensor_init(i2c_port_t i2c_num, uint8_t *data_h, uint8_t *data_l)
{
int ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
// i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
i2c_master_stop(cmd);
i2c_master_start(cmd);
// i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0xC0, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
return ret;
}
// vTaskDelay(30 / portTICK_RATE_MS);
// cmd = i2c_cmd_link_create();
// i2c_master_start(cmd);
// i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
// i2c_master_read_byte(cmd, data_h, ACK_VAL);
// i2c_master_read_byte(cmd, data_l, NACK_VAL);
// i2c_master_stop(cmd);
// ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
// i2c_cmd_link_delete(cmd);
// return ret;
}
Code: Select all
static esp_err_t i2c_master_sensor_read(i2c_port_t i2c_num, uint8_t *data_h, uint8_t *data_l)
{
int ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
// i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0x06, ACK_CHECK_EN);
//i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
i2c_master_stop(cmd);
// i2c_master_start(cmd);
// // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
// i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
// i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
// i2c_master_write_byte(cmd, 0xC0, ACK_CHECK_EN);
// i2c_master_stop(cmd);
// ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
// i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
return ret;
}
// vTaskDelay(30 / portTICK_RATE_MS);
// cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
// i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
i2c_master_read_byte(cmd, 0x0f, ACK_VAL);
// i2c_master_read_byte(cmd, data_l, NACK_VAL);
// i2c_master_stop(cmd);
// ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
// i2c_cmd_link_delete(cmd);
// return ret;
}
I'm posting this as unfinished code as it's been hurting my head for a while now and I'd just be grateful for any pointers in the correct direction.
Cheers