add main file and Makefile
This commit is contained in:
parent
7659c89e72
commit
120860328f
12
Makefile
Normal file
12
Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
SOURCE = main.c
|
||||
CC = arm-linux-gnueabi-gcc
|
||||
FLAG = --static -o
|
||||
OUT = I2C_scan.elf
|
||||
main:${SOURCE}
|
||||
${CC} ${SOURCE} ${FLAG} ${OUT}
|
||||
clean:
|
||||
rm -r ${OUT}
|
||||
.PHONY:clean
|
||||
|
||||
|
||||
|
46
main.c
Normal file
46
main.c
Normal file
@ -0,0 +1,46 @@
|
||||
/* 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user