Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. /*
  2. * C code to read pressure and temperature from the
  3. * Raspberry Pi Sense HAT add-on board (LPS25H sensor)
  4. *
  5. * sudo raspi-config --> advanced options --> enable i2c
  6. *
  7. * sudo apt-get install libi2c-dev i2c-tools
  8. *
  9. * Then build with:
  10. *
  11. * gcc -Wall accelerometer.c -o accelerometer
  12. *
  13. */
  14. #include <unistd.h>
  15.  
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <stdint.h>
  23. #include <linux/i2c-dev.h>
  24.  
  25.  
  26.  
  27. #define DEV_ID 0x6A
  28. #define DEV_PATH "/dev/i2c-1"
  29. #define WHO_AM_I 0x0F
  30. #define CTRL_REG8 0x22
  31. #define CTRL_REG2 0x18
  32.  
  33. #define CTRL_REG5_XL 0x1F
  34. #define CTRL_REG6_XL 0x20
  35.  
  36.  
  37. #define LSM9DS1_CTRL_REG4 0x1E
  38. #define LSM9DS1_CTRL_REG1_G 0x10
  39. #define LSM9DS1_ORIENT_CFG_G 0x13
  40.  
  41.  
  42. #define MPI 3.141592653
  43. #define RAD2DEG 57.2978
  44.  
  45. int fd = 0;
  46.  
  47. void delay(int);
  48.  
  49. void writeReg(uint8_t reg, uint8_t value){
  50. int res = i2c_smbus_write_byte_data(fd, reg, value);
  51. printf("res: 0%x \n", res);
  52. }
  53.  
  54. void readBlock(uint8_t command, uint8_t size, uint8_t *data){
  55. int result = i2c_smbus_read_i2c_block_data(fd, command, size, data);
  56. printf("block: 0%x \n", result);
  57. if(result != size ){
  58. printf("Fail to read i2c");
  59. exit(1);
  60. }
  61. }
  62.  
  63.  
  64. void readGyro(int *a){
  65. uint8_t block[6];
  66. readBlock(0x80 | CTRL_REG2, sizeof(block), block);
  67. *a = (int16_t)(block[0] | block[1] << 8);
  68. *(a + 1) = (int16_t)(block[2] | block[3] << 8);
  69. *(a + 2) = (int16_t)(block[4] | block[5] << 8);
  70. }
  71.  
  72.  
  73. int main(void)
  74. {
  75.  
  76. //uint8_t status = 0;
  77.  
  78.  
  79. /* open i2c comms */
  80. if ((fd = open(DEV_PATH, O_RDWR)) < 0) {
  81. perror("Unable to open i2c device");
  82. exit(1);
  83. }
  84.  
  85. /* configure i2c slave */
  86. if (ioctl(fd, I2C_SLAVE, DEV_ID) < 0) {
  87. perror("Unable to configure i2c slave device");
  88. close(fd);
  89. exit(1);
  90. }
  91.  
  92.  
  93. /* check we are who we should be */
  94. int who = i2c_smbus_read_byte_data(fd, WHO_AM_I);
  95. printf(" who: 0%x \n", who);
  96. if (who != 0x68) {
  97. printf("%s\n", "who_am_i error");
  98. close(fd);
  99. exit(1);
  100. }
  101.  
  102. printf(" Enabled \n");
  103. writeReg(LSM9DS1_CTRL_REG4, 0b00111000);
  104. writeReg(LSM9DS1_CTRL_REG1_G, 0b10111000);
  105. writeReg(LSM9DS1_ORIENT_CFG_G, 0b10111000);
  106. int gyroRaw[3];
  107. float accXAngle = 0.0;
  108. float accYAngle = 0.0;
  109.  
  110. while(1){
  111. readGyro(gyroRaw);
  112. printf(" x: %d %d %d \n", gyroRaw[0], gyroRaw[1], gyroRaw[2]);
  113.  
  114.  
  115. }
  116. close(fd);
  117.  
  118. return (0);
  119. }
  120.  
  121.  
  122. void delay(int t)
  123. {
  124. usleep(t * 1000);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement