Advertisement
Guest User

Untitled

a guest
May 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. #include <linux/i2c-dev.h>
  10.  
  11. int main() {
  12. int file;
  13. int adapter_nr = 1; // 1 for rPi rev 2, 0 for rev 1
  14. char filename[20];
  15.  
  16. snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
  17. file = open(filename, O_RDWR);
  18. if (file < 0) {
  19. /* ERROR HANDLING; you can check errno to see what went wrong */
  20. exit(1);
  21. }
  22.  
  23. int addr = 0x1e;
  24. if (ioctl(file, I2C_SLAVE, addr) < 0) {
  25. /* ERROR HANDLING; you can check errno to see what went wrong */
  26. exit(1);
  27. }
  28. i2c_smbus_write_word_data(file, 0x02, 0x00);
  29.  
  30. unsigned char values[6];
  31. values[0] = i2c_smbus_read_word_data(file, 0x03);
  32. values[1] = i2c_smbus_read_word_data(file, 0x04);
  33. values[2] = i2c_smbus_read_word_data(file, 0x05);
  34. values[3] = i2c_smbus_read_word_data(file, 0x06);
  35. values[4] = i2c_smbus_read_word_data(file, 0x07);
  36. values[5] = i2c_smbus_read_word_data(file, 0x08);
  37.  
  38. // print out result
  39. printf("Values: X MSB: %d, X LSB: %d, Y MSB: %d, Y LSB: %d, Z MSB: %d, Z LSB: %d\n",
  40. values[0],values[1],values[2],values[3],values[4],values[5]);
  41. unsigned char status;
  42. status = i2c_smbus_read_word_data(file, 0x09);
  43. printf("Status: %d",status);
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement