mirror of
https://github.com/alexandrebobkov/ESP-Nodes.git
synced 2025-10-16 18:02:38 +00:00
.
This commit is contained in:
@@ -138,7 +138,6 @@ static esp_err_t i2c_driver_initialize(void)
|
|||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
// CONNECT TO BMP280
|
// CONNECT TO BMP280
|
||||||
//
|
|
||||||
ESP_ERROR_CHECK(i2c_driver_initialize());
|
ESP_ERROR_CHECK(i2c_driver_initialize());
|
||||||
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||||
uint8_t address = 0x76;
|
uint8_t address = 0x76;
|
||||||
@@ -154,22 +153,32 @@ void app_main(void)
|
|||||||
else
|
else
|
||||||
ESP_LOGI(TAG, "error %X", cmd_ret);
|
ESP_LOGI(TAG, "error %X", cmd_ret);
|
||||||
|
|
||||||
|
// REQUEST DEVICE ID
|
||||||
|
// The sequence of commands corresponds to the BME-280 I2C protocol
|
||||||
int len = 1; // 1 byte
|
int len = 1; // 1 byte
|
||||||
uint8_t *data = malloc(len);
|
uint8_t *data = malloc(len);
|
||||||
//uint8_t address = 0x76;
|
//uint8_t address = 0x76;
|
||||||
uint8_t register_address = 0xD0;
|
uint8_t register_address = 0xD0; // Register that stores device ID
|
||||||
ESP_ERROR_CHECK(i2c_driver_initialize());
|
ESP_ERROR_CHECK(i2c_driver_initialize());
|
||||||
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||||
command = i2c_cmd_link_create();
|
command = i2c_cmd_link_create();
|
||||||
|
// Step 1. START
|
||||||
i2c_master_start(command);
|
i2c_master_start(command);
|
||||||
|
// Step 2. Send device address, WRITE bit, and wait for acknowledgment
|
||||||
i2c_master_write_byte(command, address << 1 | I2C_MASTER_WRITE, 0x1);
|
i2c_master_write_byte(command, address << 1 | I2C_MASTER_WRITE, 0x1);
|
||||||
|
// Step 3. Specify (write) register address, and wait for acknowledgment
|
||||||
i2c_master_write_byte(command, register_address, 0x1);
|
i2c_master_write_byte(command, register_address, 0x1);
|
||||||
|
// Step 4. START
|
||||||
i2c_master_start(command);
|
i2c_master_start(command);
|
||||||
|
// Step 5. Send device address, READ bit, and wait for acknowledgment
|
||||||
i2c_master_write_byte(command, address << 1 | I2C_MASTER_READ, 0x1);
|
i2c_master_write_byte(command, address << 1 | I2C_MASTER_READ, 0x1);
|
||||||
if (len > 1)
|
if (len > 1)
|
||||||
i2c_master_read(command, data, len-1, 0x0);
|
i2c_master_read(command, data, len-1, 0x0);
|
||||||
|
// Step 6. Read one byte of data from register
|
||||||
i2c_master_read_byte(command, data+len-1, 0x1);
|
i2c_master_read_byte(command, data+len-1, 0x1);
|
||||||
|
// Step 8. STOP
|
||||||
i2c_master_stop(command);
|
i2c_master_stop(command);
|
||||||
|
// Step 7. Read one byte of data from register
|
||||||
cmd_ret = i2c_master_cmd_begin(I2C_NUM_0, command, 1000 / portTICK_PERIOD_MS);
|
cmd_ret = i2c_master_cmd_begin(I2C_NUM_0, command, 1000 / portTICK_PERIOD_MS);
|
||||||
i2c_cmd_link_delete(command);
|
i2c_cmd_link_delete(command);
|
||||||
if (cmd_ret == ESP_OK) {
|
if (cmd_ret == ESP_OK) {
|
||||||
|
@@ -97,7 +97,7 @@ BME-/BMP-280 can communicate via I<sup>2</sup>C. The two diagrams below summariz
|
|||||||
<img alt="BME-/BMP-280 I2C Read & Write" src="https://github.com/alexandrebobkov/ESP-Nodes/blob/main/assets/bme280_i2c_read-write.png" width="100%"/>
|
<img alt="BME-/BMP-280 I2C Read & Write" src="https://github.com/alexandrebobkov/ESP-Nodes/blob/main/assets/bme280_i2c_read-write.png" width="100%"/>
|
||||||
|
|
||||||
---
|
---
|
||||||
REFERENCES
|
## REFERENCES
|
||||||
|
|
||||||
[^2]: https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/
|
[^2]: https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/
|
||||||
[^3]: https://www.bosch-sensortec.com/products/environmental-sensors/pressure-sensors/bmp280/
|
[^3]: https://www.bosch-sensortec.com/products/environmental-sensors/pressure-sensors/bmp280/
|
||||||
|
Reference in New Issue
Block a user