Goyoyo_bmc/main.c
2024-12-31 09:53:17 +05:30

47 lines
1.0 KiB
C

/* Autor : Siva Prakasam //
Date : 30/12/24 //
Title : I2C scan */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#define I2C_BUS "/dev/i2c-1" // Modify according to your bus
int main() {
int file;
int addr;
char *bus = I2C_BUS;
// Open the I2C bus
if ((file = open(bus, O_RDWR)) < 0) {
perror("Failed to open I2C bus");
exit(1);
}
printf("Scanning I2C bus %s...\n", bus);
// Loop through all possible I2C addresses (0x03 to 0x77)
for (addr = 0x03; addr < 0x78; addr++) {
// Try to communicate with the device at the address
if (ioctl(file, I2C_SLAVE, addr) < 0) {
// Device not found
continue;
}
// Device found, print the address
printf("I2C device found at address 0x%02x\n", addr);
}
// Close the I2C bus
close(file);
return 0;
}