SHARE
TWEET

ADXL345.c

a guest May 17th, 2011 191 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fcntl.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <linux/i2c-dev.h>
  7.  
  8. #include "ADXL345.h"
  9.  
  10. //Write only an address
  11. int write_address(const unsigned char reg){
  12.         if (write(i2c_bus, &reg, 1) != 1) {
  13.                 //printf("Failed to write to the i2c bus.\n");
  14.                 //printf("%s\n\n",strerror(errno));
  15.                 return 0;
  16.         }
  17.         return 1;
  18. }
  19.  
  20. //Write a byte to an address
  21. int write_byte(unsigned char reg, unsigned char data){
  22.         buf[0] = reg;
  23.         buf[1] = data;
  24.  
  25.         if (write(i2c_bus, buf, 2) != 2) {
  26.                 //printf("Failed to write to the i2c bus.\n");
  27.                 //printf("%s\n\n",strerror(errno));
  28.                 return 0;
  29.         }
  30.  
  31.         return 1;
  32. }
  33.  
  34. //Read the current register at an address, then change only the masked bytes based on data
  35. int write_masked_byte(unsigned char reg, unsigned char data, char mask){
  36.         unsigned char current_data;
  37.        
  38.     //Write desired register
  39.         if(write_address(reg) == 0)
  40.                 return 1;
  41.        
  42.         //Read current value of register
  43.         if(read_current_byte(&current_data) == 0)
  44.                 return 1;
  45.        
  46.         //Write masked data
  47.         data = (current_data & ~mask) | (data & mask);
  48.    
  49.         return write_byte(reg, data);
  50. }
  51.  
  52. //Read a byte from the current address
  53. int read_current_byte(unsigned char * data){
  54.         if (read(i2c_bus,buf,1) != 1) {
  55.                 //printf("Failed to read from the i2c bus.\n");
  56.                 //printf("%s\n\n",strerror(errno));
  57.                 return 0;
  58.         }
  59.  
  60.         *data = buf[0];
  61.         return 1;
  62. }
  63.  
  64. //Read a byte from the passed register
  65. int read_byte(unsigned char reg, unsigned char * data){
  66.         //Write the register's address
  67.         if(write_address(reg) == 0)
  68.                 return 0;
  69.  
  70.         //Read from that address
  71.         return read_current_byte(data);
  72. }
  73.  
  74. //Go from standby to measurement mode
  75. int measure_mode(){
  76.         //printf("Go to measure mode... ");
  77.         if(write_masked_byte(REG_POWER_CTL,0x08,0x08) == 0)
  78.                 return 0;
  79.         //printf("Set to measure mode okay.\n");
  80.         return 1;
  81. }
  82.  
  83. //Go to standby mode
  84. int standby_mode(){
  85.         //printf("Go to standby mode... ");
  86.         if(write_masked_byte(REG_POWER_CTL,0x00,0x08) == 0)
  87.                 return 0;
  88.         //printf("Set to standby mode okay.\n");
  89.         return 1;
  90. }
  91.  
  92. //Pass 1 to set power mode, 0 to turn off
  93. //0 by default
  94. int set_low_power(unsigned char power){
  95.         return write_masked_byte(REG_BW_RATE, power<<3,0x10);
  96. }
  97.  
  98. //Pass a value to set sensed range
  99. //Potential values are 2,4,8,16g
  100. int set_range(char range_set){
  101.         unsigned char rate = 0xF;
  102.         switch(range_set){
  103.                 case 2: rate = 0x0; break;
  104.                 case 4: rate = 0x1; break;
  105.                 case 8: rate = 0x2; break;
  106.                 case 16: rate = 0x3; break;
  107.                 default: printf("Not a valid range.\n"); return 0;
  108.         }
  109.         if(write_masked_byte(REG_DATA_FORMAT,rate,0x3) == 0)
  110.                 return 0;
  111.        
  112.         range = range_set;
  113.    
  114.         return 1;
  115. }
  116.  
  117. int get_range(){
  118.         unsigned char data;
  119.         if(read_byte(REG_DATA_FORMAT,&data) == 0)
  120.                 return 0;
  121.         //Mask off non-range bits
  122.         data = data & 0x3;
  123.         switch(data){
  124.                 case 0x0: range = 2; break;
  125.                 case 0x1: range = 4; break;
  126.                 case 0x2: range = 8; break;
  127.                 case 0x3: range = 16; break;
  128.                 default: printf("Not a valid range.\n"); return 0;
  129.         }
  130.         return 1;
  131. }
  132.  
  133. //Read a multiple bytes starting with register reg and proceeding for length len
  134. //Data should be at least len long, otherwise an overflow will occur
  135. int read_multibyte(unsigned char reg, unsigned char * data, unsigned char len){
  136.     unsigned char ret = 0;
  137.    
  138.         ret = write(i2c_bus, &reg, 1);
  139.     ret = read(i2c_bus, data, len);
  140.    
  141.     if (ret != len) {
  142.         printf("MultiByte read error\n");
  143.     }
  144.    
  145.     return ret;
  146. }
  147.  
  148. float convert_to_g(unsigned short raw){
  149.         char negative = 0;
  150.         float result;
  151.         //Convert from twos complement
  152.         if((raw >> 15) == 1){
  153.                 raw = ~raw + 1;
  154.                 negative = 1;
  155.         }
  156.         result = (float)raw;
  157.         if(negative)
  158.                 result *= -1;
  159.  
  160.         //1FF is the maximum value of a 10-bit signed register
  161.         result = (float)4 * (result/(0x1FF));
  162. }
  163.  
  164. vec get_orientation() {
  165.     char xyz[6];
  166.     vec orientation;
  167.    
  168.     read_multibyte(REG_DATAX0, xyz, 6);
  169.     orientation.x = convert_to_g(xyz[1] << 8 | xyz[0]);
  170.     orientation.y = convert_to_g(xyz[3] << 8 | xyz[2]);
  171.     orientation.z = convert_to_g(xyz[5] << 8 | xyz[4]);
  172.    
  173.     return orientation;
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top