#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
void i2c_init()
{
i2c_config_t config;
config.mode=I2C_MODE_MASTER;
config.sda_io_num=GPIO_NUM_15;
config.scl_io_num=GPIO_NUM_16;
config.sda_pullup_en=GPIO_PULLUP_ENABLE;
config.scl_pullup_en=GPIO_PULLUP_ENABLE;
config.master.clk_speed=10000;
config.clk_flags=0;
i2c_param_config(I2C_NUM_0,&config);
esp_err_t insr=i2c_driver_install(I2C_NUM_0,I2C_MODE_MASTER,0,0,0);
}
void read_data()
{
uint8_t data[6];
uint8_t start_address[]={0x43};
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_read_device(I2C_NUM_0,0x68,start_address,1,data,6,1000/portTICK_PERIOD_MS);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0,cmd,1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
gpio_reset_pin(GPIO_NUM_5);
gpio_set_direction(GPIO_NUM_5,GPIO_MODE_OUTPUT);
gpio_reset_pin(GPIO_NUM_4);
gpio_set_direction(GPIO_NUM_4,GPIO_MODE_OUTPUT);
if(data[0]==0){
gpio_set_level(GPIO_NUM_4,1);
}
else{
gpio_set_level(GPIO_NUM_5,1);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_set_level(GPIO_NUM_4,0);
gpio_set_level(GPIO_NUM_5,0);
}
void rd(void *pvParameter){
while(1){
read_data();
}
vTaskDelete(NULL);
}
void app_main() {
printf("Hello, Wokwi!\n");
i2c_init();
xTaskCreate(
rd,
"read_data",
1024,
NULL,
1,
NULL);
}