|
本帖最後由 undead1515 於 2011-7-7 20:47 編輯
如題
此sensor的datasheet為http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/HitachiH48C3AxisAccelerometer.pdf
有寫了程式碼 基本上都會動 只是在靜止的時候
為什麼Z軸(垂直於sensor的那軸)都一直是大約在0.05G
其他的X Y 軸 都晃動的很小
以下是程式碼:
// Constants:
#define xAxis 0
#define yAxis 1
#define zAxis 2
int Cnt2Mv = (short) 52813; // 0.80586*65536
int GfCnv = 14418; //0.22*65536
// Pins:
#define clkPin 8
#define dioPin 9
#define csPin 10
void setup()
{
Serial.begin( 9600 );
pinMode( dioPin, OUTPUT );
pinMode( clkPin, OUTPUT );
pinMode( csPin, OUTPUT );
digitalWrite( csPin, HIGH ); // disable device
digitalWrite( clkPin, LOW );
digitalWrite( dioPin, LOW );
}
void loop()
{
int a;
int value = 0;
// a =readValue( 3 );
//3 is reference voltage
value = gForce( xAxis );
Serial.print( "x = " );
Serial.print( value );
//a =readValue( 3 );
value = gForce( yAxis );
Serial.print( "\ty = " );
Serial.print( value );
//a =readValue( 3 );
value = gForce( zAxis );
Serial.print( "\tz = " );
Serial.println( value );
delay( 200 ); // wait 1sec
}
int gForce( int adcValue, int refValue )
{
if( adcValue >= refValue )
return umulf( adcValue - refValue, GfCnv );
else
return -umulf( refValue - adcValue, GfCnv );
}
int gForce( int axis )
{
int refValue = readValue( 3 );
//Serial.println(refValue);
//delay( 1 ); // wait 1msec
int adcValue = readValue( axis );
//Serial.println(adcValue);
return gForce( adcValue, refValue );
}
int readValue( int channel )
{
int data = 0;
int sig = ( ( 0x0018 + channel ) << 11 );
digitalWrite(csPin, LOW);
myShiftOut(dioPin, clkPin, 5, 1, sig );
//delayMicroseconds( 200 );
data = myShiftIn( dioPin, clkPin, 13 );
digitalWrite( csPin, HIGH );
//Serial.println(data);
return data;
}
void myShiftOut(int dataPin, int clockPin, int bitCount, int bitOrder, int val ) {
pinMode( dataPin, OUTPUT );
for( int i = 0; i < bitCount; i++) {
if (bitOrder < 0) { // if shiftout from LSB
digitalWrite( dataPin, ( val & 1) );
delayMicroseconds( 50 );
digitalWrite( clockPin, HIGH );
val = val >> 1;
}
else { // else shiftout from MSB
digitalWrite( dataPin, ((val >> 15)&1) );
delayMicroseconds( 50 );
digitalWrite( clockPin, HIGH );
val = val << 1;
}
digitalWrite( clockPin, LOW );
}
}
int myShiftIn(int dataPin, int clockPin, int bitCount) {
int pinState = 0;
int dataIn = 0;
pinMode( clockPin, OUTPUT );
pinMode( dataPin, INPUT );
for(int i = 0; i < bitCount; i++) {
int temp = -1;
digitalWrite(clockPin, HIGH) ;
//delayMicroseconds( 20 );
temp = digitalRead( dataPin );
if (temp > 0){
pinState = 1;
}
else if(temp == 0){
pinState = 0;
}
dataIn = (dataIn << 1) | pinState;
digitalWrite(clockPin, LOW);
}
//Serial.println(dataIn);
dataIn = dataIn & 0x0fff;
//Serial.println(dataIn);
return dataIn;
}
int umulf( int readvalue, int fraction )
{
int result = 0;
int rest = 0;
int temp, temp2;
for ( int i = 1; i < 17; i ++ )
{
if ( fraction < 0 ) //b15 set
{
result += ( ( readvalue >> i ) & 0x7FFF ); //accumulate integer result
temp = ( ( readvalue & ( ( 1 << i ) - 1) ) << ( 16 - i ) ); //accumulate rest R/65536
temp2 = rest ^ temp; // never overflow if sign(rest) != sign(temp)
rest += temp; //accumulate rest R/65536
//if (CPU.carry()) result++; //update result if rest overflows
if ( temp2 >= 0 ) //b15 clear, signs equal -> test for overflow
{
if ( ( rest ^ temp ) < 0) result ++; //new rest has different sign than temp
}
}
fraction <<= 1; //next bit
}
if ( rest < 0 ) result ++; //roundoff
return result;
}
還請各位神手幫忙 |
|