Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
void main()
{
int file_i2c;
int length;
int i;
unsigned char buffer[35] = {0};
char *filename = (char*)"/dev/i2c-1";
if ((file_i2c = open(filename, O_RDONLY)) < 0)
{
printf("Failed to open the i2c bus");
return;
}
int addr = 0x54;
if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
{
printf("Failed to acquire bus access and/or talk to slave.\n");
//ERROR HANDLING; you can check errno to see what went wrong
return;
}
length = sizeof(buffer);
if (read(file_i2c, buffer, length) != length)
{
printf("Failed to read from the i2c bus.\n");
}
else
{ while(1)
{
for(i=0;i<=length;i++)
{printf("Data read: %c\n", buffer[i]);
delay(1000);
}
}
}