embeded/arduino(genuino)2016. 3. 31. 09:51

음.. 이제 좀 마음에 들게 되긴했는데..

각도를 어떻게 받고. 어떤 각도를 계산해야 하려나..


#include <avr/io.h>
#include <Wire.h>

#define I2C_ID  0x53 // ADXL-345

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Wire.begin();

  // adxl345 initialize
  // power setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x2d));
    Wire.write(byte(0x18));
  Wire.endTransmission();

  // range setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x31));
    Wire.write(byte(0x09));
  Wire.endTransmission();
}

void loop() {
  // put your main code here, to run repeatedly:
  char str[30];
  char data[6];
  unsigned char idx = 0;
  short acc_x;
  short acc_y;
  short acc_z;

  // data request
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x32));
  Wire.endTransmission();
  Wire.requestFrom(0x53, 6);

  idx = 0;
  while (Wire.available())
    data[idx++] = Wire.read();

  acc_x = (data[1] << 8) | data[0];
  acc_y = (data[3] << 8) | data[2];
  acc_z = (data[5] << 8) | data[4];

  sprintf(str,"x:%6d y:%6d z:%6d",acc_x, acc_y, acc_z);
  Serial.println(str);

  delay(100);
}



FULL_RES 끄고(10bit output / 0~1023) +-16g로 출력

#include <avr/io.h>
#include <math.h>
#include <Wire.h>

#define I2C_ID  0x53 // ADXL-345

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Wire.begin();

  // adxl345 initialize
  // power setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x2d));
    Wire.write(byte(0x18));
  Wire.endTransmission();

  // range setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x31));
    Wire.write(byte(0x03));
  Wire.endTransmission();
}

void loop() {
  // put your main code here, to run repeatedly:
  char str[30];
  char data[6];
  unsigned char idx = 0;
  short acc_x;
  short acc_y;
  short acc_z;
  double deg_x;
  double deg_y;

  // data request
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x32));
  Wire.endTransmission();
  Wire.requestFrom(0x53, 6);

  idx = 0;
  while (Wire.available())
    data[idx++] = Wire.read();

  acc_x = (data[1] << 8) | data[0];
  acc_y = (data[3] << 8) | data[2];
  acc_z = (data[5] << 8) | data[4];

  if(acc_z == 0)
  {
    deg_x = 0.0;
    deg_y = 0.0;
  }
  else
  {
    deg_x = atan((double)acc_x / (double)acc_z) * 57.2957;
    deg_y = atan((double)acc_y / (double)acc_z) * 57.2957;
  }
  
  sprintf(str,"x:%6d y:%6d z:%6d",acc_x, acc_y, acc_z);
  Serial.println(str);
  
  sprintf(str,"x:%d y:%d",(short)deg_x, (short)deg_y);
  Serial.println(str);

  delay(100);
}



#include <avr/io.h>
#include <math.h>
#include <Wire.h>

#define I2C_ID  0x53 // ADXL-345

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Wire.begin();

  // adxl345 initialize
  // power setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x2d));
    Wire.write(byte(0x18));
  Wire.endTransmission();

  // range setting
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x31));
    Wire.write(byte(0x02));
  Wire.endTransmission();
}

void loop() {
  // put your main code here, to run repeatedly:
  char str[30];
  char str_x[15];
  char str_y[15];
  char data[6];
  unsigned char idx = 0;
  short acc_x;
  short acc_y;
  short acc_z;
  double deg_x;
  double deg_y;

  // data request
  Wire.beginTransmission(I2C_ID);
    Wire.write(byte(0x32));
  Wire.endTransmission();
  Wire.requestFrom(0x53, 6);

  idx = 0;
  while (Wire.available())
    data[idx++] = Wire.read();

  acc_x = (data[1] << 8) | data[0];
  acc_y = (data[3] << 8) | data[2];
  acc_z = (data[5] << 8) | data[4];

  if(acc_z == 0)
  {
    deg_x = 0.0;
    deg_y = 0.0;
  }
  else
  {
    deg_x = atan((double)acc_x / (double)acc_z) * 57.2957;
    deg_y = atan((double)acc_y / (double)acc_z) * 57.2957;
  }
  
  sprintf(str,"x:%6d y:%6d z:%6d",acc_x, acc_y, acc_z);
  Serial.println(str);

  dtostrf(deg_x, 4,2, str_x);
  dtostrf(deg_y, 4,2, str_y);
  sprintf(str,"x:%s y:%s",str_x, str_y);
  Serial.println(str);

  delay(100);
}


Posted by 구차니