// Sweep through all 7-bit I2C addresses, to see if any slaves are present on
// the I2C bus. Print out a table that looks like this:
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#define PICO_I2C_SDA_PIN 20
#define PICO_I2C_SCL_PIN 21
#define i2c_type_default 0
// I2C reserves some addresses for special purposes. We exclude these from
// the scan.
// These are any addresses of the form 000 0xxx or 111 1xxx
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
int main() {
stdio_init_all();
// This example will use I2C0 on the default SDA and SCL pins.
i2c_init(i2c_type_default, 100 * 1000);
gpio_set_function(PICO_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_I2C_SDA_PIN);
gpio_pull_up(PICO_I2C_SCL_PIN);
printf("I2C Bus Scan\n");
for (int addr = 0; addr < (1 << 7); ++addr) {
// Skip over any reserved addresses.
int ret;
uint8_t rxdata;
if (reserved_addr(addr))
ret = PICO_ERROR_GENERIC;
else
ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
if (ret > 0) {
printf("Dispositivo encontrado 0x%x\n", addr);
}
}
}