使用Arduino MEGA ADK进行加速度传感器MMA7455L?

时间:2012-04-16 00:21:26

标签: microcontroller

我正在使用MMA7455L加速度计,但我无法从中读取或校准,我曾经使用模拟测试进行测试,这样更容易。但现在有了数字,我无法读取X,Y,Z。我在Arduino论坛上累了某人的代码,但它只给出0 0 0

#include <SPI.h>                   // include the SPI library

const byte mode = 0xAC;             // Write Mode Control Register "1"+0x16+"1bit"
const byte measure = 0x05;          // 2g sensitive + Measure Mode "00000101"
const byte xout = 0x0C;             // Read XOUT register "0"+0x06+"0"
const byte yout = 0x0E;             // Read YOUT register "0"+0x07+"0"
const byte zout = 0x10;             // Read ZOUT register "0"+0x08+"0"

const byte i2c_add = 0x9A;          // Write I2C address "1"+0x0D+"0"
const byte i2cdis = 0x9D;           // Disable I2C 0b10011101, bit 6:0 is read only

const int pinReady = 7;
const int pinCS = 5;

void setup() {
  Serial.begin(9600);                    // open serial port  
  SPI.begin();                           // start the SPI library
  SPI.setClockDivider(SPI_CLOCK_DIV128); // tune down the speed of SPI
  pinMode(pinReady, INPUT);              // initalize the pins
  pinMode(pinCS, OUTPUT);                // initalize the pins

  digitalWrite(pinCS, LOW);              // low make the chip in SPI mode
  delay(1000);                           // wait for the chip to setup

  // Disable I2C, without this code the DataRady pin never go HIGH
  digitalWrite(pinCS, HIGH);             // turn from HIGH to LOW to give the signal
  delay(10);                             // without this delay, DataReady pin never go HIGH
  digitalWrite(pinCS, LOW);              // select the sensor
  SPI.transfer(i2c_add);                 // I2C address write command
  SPI.transfer(i2cdis);                  // disable the I2C
  digitalWrite(pinCS, HIGH);             // de-select the sensor


  // Setup the measure mode
  digitalWrite(pinCS, HIGH);             // turn from HIGH to LOW to give the signal
  digitalWrite(pinCS, LOW);              // select the sensor
  SPI.transfer(mode);                    // Write to mode address
  SPI.transfer(measure);                 // Send the mode setting value
  digitalWrite(pinCS, HIGH);             // de-select the sensor
  delay(10);                             // give the sensor time to set up
}

void loop() {
  byte valueX, valueY, valueZ;
  if (digitalRead(pinReady) == HIGH) {   // data ready
    digitalWrite(pinCS, LOW);            // select the sensor
    (void) SPI.transfer(xout);           // read value X
    valueX = SPI.transfer(0);            // '0' is don't care value
    digitalWrite(pinCS, HIGH);           // de-select the sensor

    digitalWrite(pinCS, LOW);            // select the sensor
    (void) SPI.transfer(yout);           // read value Y
    valueY = SPI.transfer(0);            // '0' is don't care value
    digitalWrite(pinCS, HIGH);           // de-select the sensor

    digitalWrite(pinCS, LOW);            // select the sensor
    (void) SPI.transfer(zout);                  // read value Z
    valueZ = SPI.transfer(0);            // '0' is don't care value
    digitalWrite(pinCS, HIGH);           // de-select the sensor

    // print out the value
    Serial.print("X:  ");
    Serial.print((int) valueX);
    Serial.print("  Y:  ");
    Serial.print((int) valueY);
    Serial.print("  Z:  ");
    Serial.println((int) valueZ);
  }
  delay(1000);
}

0 个答案:

没有答案