[請教] Arduino MMA7455L 3-Axis Accelometer
本帖最後由 modbook 於 2011-6-12 18:13 編輯
Arduino 新手, 剛買了Ardunio UNO, 淘寶入手了塊 MMA7455L sensor "龙丘 MMA7455 模块V1.0"
Pin 的接駁:
CLK --> Pin 13 (SPI library 話的)
MOSI --> Pin11 (SPI library 話的)
MISO --> +R2.2K --> Pin 12 (說明書話的)
Data ready --> Pin 7 (隨意)
CS --> Pin 10 (SPI library 話的)
無法讀取到 MMA7455L 的數據, 以下是我寫的code:
#include <SPI.h> // include the SPI library
const byte mode = 0x16; // Mode Control Register is $16
const byte measure = 0b00000101; // 1st 01 set to 2g, 2nd 01 set to measure mode
const byte xout = 0x06; // X-Axis value register is $06
const byte yout = 0x07; // Y-Axis value register is $07
const byte zout = 0x08; // Z-Axis value register is $08
const int pinReady = 7; // Data Ready Pin
const int pinCS = 10; // CS pin of MMA board
void setup() {
Serial.begin(9600); // open serial port
SPI.begin(); // start the SPI library
pinMode(pinReady, INPUT); // initalize the pins
pinMode(pinCS, OUTPUT); // initalize the pins
// Configure MMA7455 into 2g sensitivies and measure mode
digitalWrite(pinCS, LOW); // select the sensor
SPI.transfer(mode); // Send register location
SPI.transfer(measure); // Send value into that register
digitalWrite(pinCS, HIGH); // de-select the sensor
delay(100); // give the sensor time to set up
}
void loop() {
if (digitalRead(pinReady == HIGH)) {
digitalWrite(pinCS, LOW); // select the sensor
byte valueX = SPI.transfer(xout);
byte valueY = SPI.transfer(yout);
byte valueZ = SPI.transfer(zout);
digitalWrite(pinCS, HIGH); // de-select the sensor
Serial.print("X: ");
Serial.print((int) valueX);
Serial.print(" Y: ");
Serial.print((int) valueY);
Serial.print(" Z: ");
Serial.println((int) valueZ);
}
delay(1000);
}
開了個serial monitor之後, 所有data output 都是 0 :
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
......
我的問題:
const byte measure = 0b00000101; 那一句, 其實我只須send 最尾的0101. 我試過改佢為 0x05 都係唔唔得
說明晝有提到CS pin 接高電平為IIC, 低電平為SPI. 我有試過長期將 CS Pin 落地, 省去所有output CS pin to LOW (to select) 及 HIGH (de-select) 的code. output依然係 0,0,0.
MMA7455L 的 datasheet 亦有提過:
A SPI read transfer consists of a 1-bit Read/Write signal, a 6-bit address, and 1-bit don't care bit. (1-bit R/W=0 + 6-bits address + 1-bit don't care). The data to read is sent by the SPI interface during the next transfer.
In order to write to one of the 8-bit registers, an 8-bit write command must be sent to the MMA7455L. The write command consists of an MSB (0=read, 1=write) to indicate writing to the MMA7455L register, followed by a 6-bit address and 1 don't care bit.
我唔識理解以上所講的要點改我段code.
希望大家可以幫到我. 萬謝 |
|
|