Created
April 23, 2024 12:38
-
-
Save esmarr58/fb7ddbef8911974240c66c68ed45f811 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "driver/gpio.h" | |
#include "esp_log.h" | |
#include "sdkconfig.h" | |
#include "driver/i2c.h" | |
static const char *TAG = "i2c-simple-example"; | |
#define I2C_MASTER_SCL_IO GPIO_NUM_9 /*!< GPIO number used for I2C master clock */ | |
#define I2C_MASTER_SDA_IO GPIO_NUM_8 /*!< 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 100000 /*!< 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 DIRECCION_ESCLAVO 0x68 | |
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); | |
} | |
static esp_err_t leer_byte(uint8_t slv_address, uint8_t reg_addr, uint8_t *data, size_t len) | |
{ | |
return i2c_master_write_read_device(I2C_MASTER_NUM, slv_address, ®_addr, 1, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); | |
} | |
static esp_err_t escribir_byte(uint8_t slv_address, uint8_t reg_addr, uint8_t data) | |
{ | |
int ret; | |
uint8_t write_buf[2] = {reg_addr, data}; | |
ret = i2c_master_write_to_device(I2C_MASTER_NUM, slv_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); | |
return ret; | |
} | |
void programa1(void * pvParameters){ | |
for(;;){ | |
} | |
} | |
void programa2(void * pvParameters){ | |
for(;;){ | |
} | |
} | |
void app_main(void){ | |
ESP_ERROR_CHECK(i2c_master_init()); | |
ESP_LOGI(TAG, "I2C initialized successfully"); | |
uint8_t data[2]; | |
/* Read the MPU9250 WHO_AM_I register, on power up the register should have the value 0x71 */ | |
for(;;){ | |
ESP_ERROR_CHECK(leer_byte(0x68, 0x00, data, 1)); | |
ESP_LOGI(TAG, "Segundos = %X", data[0]); | |
sleep(1); | |
} | |
//xTaskCreate(programa1, "Programa1", 4048, NULL, tskIDLE_PRIORITY, NULL); | |
//xTaskCreate(programa2, "Programa2", 4048, NULL, tskIDLE_PRIORITY, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment