PCF8575可以实现16口输入或者输出, 而且升级了esp32 IDF的新版本5.3.0,好多函数都和老版本的4.4.7不一样了
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/i2c_master.h"
#include "driver/gpio.h"
#define I2C_MASTER_SCL_IO 15
#define I2C_MASTER_SDA_IO 16
void app_main(void)
{
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = -1,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_io_num = I2C_MASTER_SDA_IO,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus_handle;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = 0x20, // 7位是0X20 实际是0100 000+读写位,可以理解是0x40和0x41
.scl_speed_hz = 400000,
};
i2c_master_dev_handle_t dev_handle;
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle));
while (1)
{
//输出检测
//uint8_t output_data[2] = {0x01,0x00}; // 设置P00为高电平,其它低电平
uint8_t output_data[2] = {0xFF,0x00}; // 设置低8位为高电平,高8位为低电平
i2c_master_transmit(dev_handle, output_data, 2, 100);
//输入检测
//说明书上说:在从PCF8575读取之前,所有需要的输入端口应设置为逻辑1。
uint8_t input_data[2];
i2c_master_receive(dev_handle, input_data, 2, 100);
printf("read data: %02X %02X \n", input_data[0], input_data[1]);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
不记录一下测试的情况,过几天又得忘记,哈哈