embeded/raspberry pi2015. 8. 24. 15:47

아두이노랑은 이렇게 하라는데

일단.. I2C로 할거니까.. 저렇게 위에 4개 선을 연결하면 어찌어찌 라즈베리에서도 사용가능 할 듯



장치 ID는 0xE5 고정

ADXL345

REGISTER DEFINITIONS

Register 0x00—DEVID (Read Only)

The DEVID register holds a fixed device ID code of 0xE5 (345 octal). 



데이터는 X/Y/Z축 LSB / MSB 순으로 들어 있다.


Register 0x32 to Register 0x37—DATAX0, DATAX1, DATAY0, DATAY1, DATAZ0, DATAZ1 (Read Only)

 These six bytes (Register 0x32 to Register 0x37) are eight bits each and hold the output data for each axis. Register 0x32 and Register 0x33 hold the output data for the x-axis, Register 0x34 and Register 0x35 hold the output data for the y-axis, and Register 0x36 and Register 0x37 hold the output data for the z-axis. The output data is twos complement, with DATAx0 as the least significant byte and DATAx1 as the most significant byte, where x represent X, Y, or Z. The DATA_FORMAT register (Address 0x31) controls the format of the data. It is recommended that a multiple-byte read of all registers be performed to prevent a change in data between reads of sequential registers



I 2 C

 With CS tied high to VDD I/O, the ADXL345 is in I2 C mode, requiring a simple 2-wire connection, as shown in Figure 40. The ADXL345 conforms to the UM10204 I 2 C-Bus Specification and User Manual, Rev. 03—19 June 2007, available from NXP Semiconductors. It supports standard (100 kHz) and fast (400 kHz) data transfer modes if the bus parameters given in Table 11 and Table 12 are met. Single- or multiple-byte reads/writes are supported, as shown in Figure 41. With the ALT ADDRESS pin high, the 7-bit I 2 C address for the device is 0x1D, followed by the R/W bit. This translates to 0x3A for a write and 0x3B for a read. An alternate I2 C address of 0x53 (followed by the R/W bit) can be chosen by grounding the ALT ADDRESS pin (Pin 12). This translates to 0xA6 for a write and 0xA7 for a read. There are no internal pull-up or pull-down resistors for any unused pins; therefore, there is no known state or default state for the CS or ALT ADDRESS pin if left floating or unconnected. It is required that the CS pin be connected to VDD I/O and that the ALT ADDRESS pin be connected to either VDD I/O or GND when using I2 C


[링크 : http://eleparts.co.kr/data/design/product_file/SENSOR/G_SENSOR/data/ADXL345.pdf]


---





---

pi@raspberrypi /boot/overlays $ dmesg | grep i2c

[    3.862054] bcm2708_i2c_init_pinmode(1,2)

[    3.863939] bcm2708_i2c_init_pinmode(1,3)

[    3.866020] bcm2708_i2c 3f804000.i2c: BSC1 Controller at 0x3f804000 (irq 79) (baudrate 100000)


pi@raspberrypi /boot/overlays $ lsmod | grep i2c

i2c_bcm2708             4990  0


pi@raspberrypi /boot/overlays $ insmod i2c-dev

Error: could not load module i2c-dev: No such file or directory


pi@raspberrypi /boot/overlays $ sudo vi /etc/modules

i2c-dev

i2c-bcm2708


pi@raspberrypi ~ $ lsmod | grep i2c

i2c_dev                 6027  0

i2c_bcm2708             4990  0


pi@raspberrypi ~ $ i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:          -- -- -- -- -- -- -- -- -- -- -- -- --

10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- --

60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

70: -- -- -- -- -- -- -- --

pi@raspberrypi ~ $ i2cdetect -y 0

Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory


pi@raspberrypi ~ $ i2cdetect -l

i2c-1   i2c             3f804000.i2c                            I2C adapter


pi@raspberrypi ~ $ i2cdetect -F 1

Functionalities implemented by /dev/i2c-1:

I2C                              yes

SMBus Quick Command              yes

SMBus Send Byte                  yes

SMBus Receive Byte               yes

SMBus Write Byte                 yes

SMBus Read Byte                  yes

SMBus Write Word                 yes

SMBus Read Word                  yes

SMBus Process Call               yes

SMBus Block Write                yes

SMBus Block Read                 no

SMBus Block Process Call         no

SMBus PEC                        yes

I2C Block Write                  yes

I2C Block Read                   yes


pi@raspberrypi ~ $ lsmod | grep -i i2c

i2c_dev                 6027  0

i2c_bcm2708             4990  0



insmod가 아니라 modprobe ...


pi@raspberrypi ~ $ sudo modprobe i2c_dev

pi@raspberrypi ~ $ lsmod | grep i2c

i2c_dev                 6027  0

i2c_bcm2708             4990  0


[링크 : http://raspberrypi.stackexchange.com/.../how-to-make-raspbian-load-the-i2c-dev-module-on-boot-up]



---





#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>

int main(int argc, char **argv)
{
   printf("**** ADXL435 example program ****\n");
   
   int fd;                                          // File descrition
   char *fileName = "/dev/i2c-1";                        // Name of the port we will be using
   int  address = 0x53;                              // Address of the SRF02 shifted right one bit
   char buf[6];                              // Buffer for data being read/ written on the i2c bus
   short x,y,z;

   if ((fd = open(fileName, O_RDWR)) < 0) {               // Open port for reading and writing
      printf("Failed to open i2c port\n");
      exit(1);
   }
   
   if (ioctl(fd, I2C_SLAVE, address) < 0) {               // Set the port options and set the address of the device we wish to speak to
      printf("Unable to get bus access to talk to slave\n");
      exit(1);
   }

   buf[0] = 0x2d;                                       // Commands for performing a ranging
   buf[1] = 0x18;
   
   if ((write(fd, buf, 2)) != 2) {                        // Write commands to the i2c port
      printf("Error writing to i2c slave\n");
      exit(1);
   }

   buf[0] = 0x31;                                       // Commands for performing a ranging
   buf[1] = 0x09;
   
   if ((write(fd, buf, 2)) != 2) {                        // Write commands to the i2c port
      printf("Error writing to i2c slave\n");
      exit(1);
   }

                                       // This sleep waits for the ping to come back
while(true){   

   buf[0] = 0x32;                                       // This is the register we wish to read from
   if ((write(fd, buf, 1)) != 1) {                        // Send the register to read from
      printf("Error writing to i2c slave\n");
      exit(1);
   }
   

usleep(1000);
  memset(&buf,0,sizeof(buf));

   if (read(fd, buf, 6) != 6) {                        // Read back data into buf[]
      printf("Unable to read from slave\n");
      exit(1);
   }
   else { x=y=z=0;
//               memset(&buf,0,sizeof(buf));

      x = ((short)buf[1]<<8) | (short) buf[0]; 
       y = ((short)buf[3]<<8) | (short) buf[2];
       z = ((short)buf[5]<<8) | (short) buf[4];
       std::cout<<"x:"<<x<<"\ty:"<<y<<"\tz:"<<z<<std::endl;
   }
}
   return 0;

} 



Posted by 구차니