Advertisement
MarShar

V4Pressure

Mar 26th, 2020
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. /*
  2.  * C function code to read pressure and temperature from the
  3.  *  Raspberry Pi Sense HAT add-on board (LPS25H sensor)
  4.  *
  5.  *  sudo raspi-config --> interfacing options --> enable i2c
  6.  *
  7.  *  sudo apt install libi2c-dev
  8.  *
  9.  *  Copyright for this function - dave.bird@dsl.pipex.com (2016)
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <linux/i2c-dev.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <i2c/smbus.h>
  20. #include <sys/ioctl.h>
  21.  
  22. #define DEV_ID 0x5c
  23. #define DEV_PATH "/dev/i2c-1"
  24. #define WHO_AM_I 0x0F
  25. #define CTRL_REG1 0x20
  26. #define CTRL_REG2 0x21
  27. #define PRESS_OUT_XL 0x28
  28. #define PRESS_OUT_L 0x29
  29. #define PRESS_OUT_H 0x2A
  30. #define TEMP_OUT_L 0x2B
  31. #define TEMP_OUT_H 0x2C
  32.  
  33. void delay(int);
  34.  
  35. int sense_pressure(double *pressure, double *t_c)
  36. {
  37.     int fd = 0;
  38.     uint8_t temp_out_l = 0, temp_out_h = 0;
  39.     int16_t temp_out = 0;
  40.    
  41.     uint8_t press_out_xl = 0;
  42.     uint8_t press_out_l = 0;
  43.     uint8_t press_out_h = 0;
  44.  
  45.     int32_t press_out = 0;
  46.    
  47.     uint8_t status = 0;
  48.  
  49.     /* open i2c comms */
  50.     if((fd = open(DEV_PATH, O_RDWR)) < 0) {
  51.         perror("Unable to open i2c device");
  52.         exit(1);
  53.     }
  54.  
  55.     /* configure i2c slave */
  56.     if(ioctl(fd, I2C_SLAVE, DEV_ID) < 0) {
  57.         perror("Unable to configure i2c slave device");
  58.         close(fd);
  59.         exit(1);
  60.     }
  61.  
  62.     /* check we are who we should be */
  63.     if(i2c_smbus_read_byte_data(fd, WHO_AM_I) != 0xBD) {
  64.         printf("%s\n", "who_am_i error");
  65.         close(fd);
  66.         exit(1);
  67.     }
  68.  
  69.     /* Power down the device (clean start) */
  70.     i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
  71.  
  72.     /* Turn on the pressure sensor analog front end in single shot mode  */
  73.     i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x84);
  74.  
  75.     /* Run one-shot measurement (temperature and pressure), the set bit will be reset by the
  76.      * sensor itself after execution (self-clearing bit)
  77.      */
  78.     i2c_smbus_write_byte_data(fd, CTRL_REG2, 0x01);
  79.  
  80.     /* Wait until the measurement is complete */
  81.     do {
  82.         delay(25);      /* 25 milliseconds */
  83.         status = i2c_smbus_read_byte_data(fd, CTRL_REG2);
  84.     } while(status != 0);
  85.  
  86.     /* Read the temperature measurement (2 bytes to read) */
  87.     temp_out_l = i2c_smbus_read_byte_data(fd, TEMP_OUT_L);
  88.     temp_out_h = i2c_smbus_read_byte_data(fd, TEMP_OUT_H);
  89.  
  90.     /* Read the pressure measurement (3 bytes to read) */
  91.     press_out_xl = i2c_smbus_read_byte_data(fd, PRESS_OUT_XL);
  92.     press_out_l = i2c_smbus_read_byte_data(fd, PRESS_OUT_L);
  93.     press_out_h = i2c_smbus_read_byte_data(fd, PRESS_OUT_H);
  94.  
  95.     /* make 16 and 24 bit values (using bit shift) */
  96.     temp_out = temp_out_h << 8 | temp_out_l;
  97.     press_out = press_out_h << 16 | press_out_l << 8 | press_out_xl;
  98.  
  99.     /* calculate output values */
  100.     *t_c = 42.5 + (temp_out / 480.0);
  101.     *pressure = press_out / 4096.0;
  102.  
  103.    
  104.     /* Power down the device */
  105.     i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
  106.  
  107.     close(fd);
  108.  
  109.     return (0);
  110. }
  111.  
  112. void delay(int t)
  113. {
  114.     usleep(t * 1000);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement