Skip to content

Instantly share code, notes, and snippets.

@esmarr58
Created April 23, 2024 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esmarr58/311b059ff05eab3dd2cb7abed9bbaa66 to your computer and use it in GitHub Desktop.
Save esmarr58/311b059ff05eab3dd2cb7abed9bbaa66 to your computer and use it in GitHub Desktop.
#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 = "Ejemplo-i2c2";
#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, &reg_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(;;){
}
}
uint8_t cambiarValor(uint8_t entrada){
uint8_t salida;
uint8_t unidades = entrada&0x0F;
uint8_t decenas = (entrada&0xF0)>>4;
salida = decenas*10+unidades;
return salida;
}
void programa2(void * pvParameters){
uint8_t datos[10];
char diaString[7][10] = {"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"};
char mesString[12][11] = {"Enero", "Febrero", "Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"};
for(;;){
ESP_ERROR_CHECK(leer_byte(0x68, 0x00, datos, 7));
uint8_t segundos = cambiarValor(datos[0]);
uint8_t minutos = cambiarValor(datos[1]);
uint8_t hora = cambiarValor(datos[2]);
uint8_t diaSem = cambiarValor(datos[3]);
uint8_t dia = cambiarValor(datos[4]);
uint8_t mes = cambiarValor(datos[5]);
uint8_t anio = cambiarValor(datos[6]);
ESP_LOGI(TAG, "%s %i de %s del 20%i %i:%i:%i ", &diaString[diaSem][0], dia, &mesString[mes-1][0], anio, hora, minutos, segundos);
sleep(1);
}
}
void app_main(void){
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
/*
ESP_ERROR_CHECK(escribir_byte(0x68,0x00,0x00)); //segundos
ESP_ERROR_CHECK(escribir_byte(0x68,0x01,0x12)); //minutos
ESP_ERROR_CHECK(escribir_byte(0x68,0x02,0x13)); //horas
ESP_ERROR_CHECK(escribir_byte(0x68,0x03,0x03)); //dia de la semana
ESP_ERROR_CHECK(escribir_byte(0x68,0x04,0x23)); //dia
ESP_ERROR_CHECK(escribir_byte(0x68,0x05,0x04)); //mes
ESP_ERROR_CHECK(escribir_byte(0x68,0x06,0x24)); //anio
*/
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