Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <linux/i2c-dev.h>
- int main() {
- int file;
- int adapter_nr = 1; // 1 for rPi rev 2, 0 for rev 1
- char filename[20];
- snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
- file = open(filename, O_RDWR);
- if (file < 0) {
- /* ERROR HANDLING; you can check errno to see what went wrong */
- exit(1);
- }
- int addr = 0x1e;
- if (ioctl(file, I2C_SLAVE, addr) < 0) {
- /* ERROR HANDLING; you can check errno to see what went wrong */
- exit(1);
- }
- i2c_smbus_write_word_data(file, 0x02, 0x00);
- unsigned char values[6];
- values[0] = i2c_smbus_read_word_data(file, 0x03);
- values[1] = i2c_smbus_read_word_data(file, 0x04);
- values[2] = i2c_smbus_read_word_data(file, 0x05);
- values[3] = i2c_smbus_read_word_data(file, 0x06);
- values[4] = i2c_smbus_read_word_data(file, 0x07);
- values[5] = i2c_smbus_read_word_data(file, 0x08);
- // print out result
- printf("Values: X MSB: %d, X LSB: %d, Y MSB: %d, Y LSB: %d, Z MSB: %d, Z LSB: %d\n",
- values[0],values[1],values[2],values[3],values[4],values[5]);
- unsigned char status;
- status = i2c_smbus_read_word_data(file, 0x09);
- printf("Status: %d",status);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement