//============================================================================ // Name : test_A.cpp // Author : Adarsh Kumar // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include #include #include // system() #include // sleep() #include // oflag like O_RDWR #include #include char Readbyte(char address); char file_I2C1_DEV[30] = "/dev/i2c-0"; int file; int addr_AD5933 = 0b00001101; // The I2C address of AD5933 int main() { printf("I2C Interface Project!\n"); if((file = open(file_I2C1_DEV,O_RDWR)) < 0) { perror("Failed to open the i2c bus"); exit(1); } if(ioctl(file,I2C_SLAVE,addr_AD5933) < 0) { printf("Failed to acquire bus access and/or talk to slave.\n"); exit(1); } char temp; temp = Readbyte(82); printf("%d\n",temp); sleep(1); } char Readbyte(char address) { char data; char buf[10] = {0}; buf[0] = address; if(write(file,buf,1) != 1) { /* ERROR HANDLING: i2c transaction failed */ printf("Failed to write to the i2c bus.\n"); } // Using I2C Read if (read(file,buf,1) != 1) { /* ERROR HANDLING: i2c transaction failed */ printf("Failed to read from the i2c bus.\n"); } else { data = buf[0]; } //BCD To Decimal Conversion if(address == 0) //remove MSB bit in case of second { data = (((data & 0x70) >> 4) * 10) + (data & 0x0F); } else { data = (((data & 0xF0) >> 4) * 10) + (data & 0x0F); } return data; }