/branches/V0.82b-Arthur-P/bma180/bma180.cpp |
---|
0,0 → 1,171 |
#include <avr/pgmspace.h> |
#include <wiring.h> |
#include "bma180.h" |
BMA180::BMA180() |
{ |
gSense=G2; |
} |
void BMA180::setAddress(int adr) |
{ |
address=(unsigned char) adr; |
} |
int temp; |
void BMA180::readAccel(int * x, int * y, int * z) |
{ |
unsigned int result; |
Wire.beginTransmission(address); |
Wire.send(0x02); |
Wire.endTransmission(); |
Wire.requestFrom((int)address, 7); |
if(Wire.available()==7) |
{ |
int lsb = Wire.receive()>>2; |
int msb = Wire.receive(); |
*x=(msb<<6)+lsb; |
if ((*x)&0x2000) (*x)|=0xc000; // set full 2 complement for neg values |
lsb = Wire.receive()>>2; |
msb = Wire.receive(); |
*y=(msb<<6)+lsb; |
if ((*y)&0x2000) (*y)|=0xc000; |
lsb = Wire.receive()>>2; |
msb = Wire.receive(); |
*z=(msb<<6)+lsb; |
if ((*z)&0x2000) (*z)|=0xc000; |
temp = Wire.receive(); |
if (temp&0x80) temp|=0xff00; |
} |
result = Wire.endTransmission(); |
} |
float BMA180::getGSense() |
{ |
switch(gSense) |
{ |
case G1: return 1.0; |
case G15: return 1.5; |
case G2: return 2.0; |
case G3: return 3.0; |
case G4: return 4.0; |
case G8: return 8.0; |
case G16: return 16.0; |
} |
} |
float BMA180::getXValFloat() |
{ |
// normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
return (float)x/8191.0*getGSense(); |
} |
float BMA180::getYValFloat() |
{ |
// normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
return (float)y/8191.0*getGSense(); |
} |
float BMA180::getZValFloat() |
{ |
// normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
return (float)z/8191.0*getGSense(); |
} |
int BMA180::getRegValue(int adr) |
{ |
int val; |
Wire.beginTransmission(address); |
Wire.send(adr); |
Wire.endTransmission(); |
Wire.requestFrom((int)address, 1); |
if (Wire.available()==1) |
{ |
val = Wire.receive(); |
} |
else val=-1; |
int result = Wire.endTransmission(); |
checkResult(result); |
return val; |
} |
void BMA180::setRegValue(int regAdr, int val, int maskPreserve) |
{ |
int preserve=getRegValue(regAdr); |
int orgval=preserve & maskPreserve; |
Wire.beginTransmission(address); |
Wire.send(regAdr); |
Wire.send(orgval|val); |
int result = Wire.endTransmission(); |
checkResult(result); |
} |
void BMA180::setGSensitivty(GSENSITIVITY maxg) //1, 1.5 2 3 4 8 16 |
{ |
setRegValue(0x35,maxg<<1,0xF1); |
} |
void BMA180::SetFilter(FILTER f) // 10,20,40,75,150,300,600,1200, HP 1HZ,BP 0.2-300, higher values not authorized |
{ |
setRegValue(0x20,f<<4,0x0F); |
} |
void BMA180::SetISRMode() // you must provide a ISR function on the pin selected (pin 2 or 3,. so INT0 or INT1) |
{ |
setRegValue(0x21,2,0xFD); |
} |
void BMA180::SoftReset() // all values will be default |
{ |
setRegValue(0x10,0xB6,0); |
delay(100); |
} |
void BMA180::SetSMPSkip() |
{ |
setRegValue(0x35, 1, 0xFE); |
} |
int BMA180::getIDs(int *id, int *version) |
{ |
Wire.beginTransmission(address); |
Wire.send(0x0); |
Wire.endTransmission(); |
Wire.requestFrom((int)address, 2); |
if (Wire.available()==2) |
{ |
*id = Wire.receive(); |
*version= Wire.receive(); |
} |
else *id=-1; |
int result = Wire.endTransmission(); |
checkResult(result); |
return *id!=-1; |
} |
void BMA180::enableWrite() |
{ |
//ctrl_reg1 register set ee_w bit to enable writing to regs. |
setRegValue(0x0D,0x10,~0x10); |
delay(10); |
} |
void BMA180::disableWrite() |
{ |
setRegValue(0x0D,0x0,~0x10); |
delay(10); |
} |
bool BMA180::checkResult(int result) |
{ |
if(result >= 1) |
return false; |
return true; |
} |
/branches/V0.82b-Arthur-P/bma180/bma180.h |
---|
0,0 → 1,52 |
#ifndef _BMA180_h |
#define _BMA180_h |
#include <Wire.h> |
/* |
extern "C" { |
#include <../Wire/Wire.h> |
} |
*/ |
#define BMA180_ADDRESS_SDO_LOW 0x40 |
#define BMA180_ADDRESS_SDO_HIGH 0x41 |
class BMA180 |
{ |
public: |
typedef enum {F10HZ=0,F20HZ=1,F40HZ, F75HZ,F15HZ0,F300HZ,F600HZ,F1200HZ,HIGHPASS,BANDPASS} FILTER; |
typedef enum {G1=0,G15=1,G2,G3,G4,G8,G16}GSENSITIVITY; |
private: |
unsigned char address; |
GSENSITIVITY gSense; |
public: |
int x,y,z; // yes, public, what the heck |
int temp; |
BMA180(); |
void setAddress(int val); |
void readAccel(int * x, int * y, int * z); |
float getGSense(); |
float getXValFloat(); |
float getYValFloat(); |
float getZValFloat(); |
void setRegValue(int regAdr, int val, int maskPreserve); |
int getRegValue(int adr); |
void setGSensitivty(GSENSITIVITY maxg); |
void SetFilter(FILTER f); |
void SetISRMode(); |
void SoftReset(); |
void SetSMPSkip(); |
int getIDs(int *id, int *version); |
void enableWrite(); |
void disableWrite(); |
virtual bool checkResult(int result); |
}; |
#endif //_BMA180_h |
/branches/V0.82b-Arthur-P/bma180/examples/BMA180_simple/BMA180_simple.pde |
---|
0,0 → 1,32 |
#include <Wire.h> |
#include <bma180.h> |
BMA180 bma180 = BMA180(BMA180_ADDRESS_SDO_LOW); |
void setup() |
{ |
Wire.begin(); |
Serial.begin(115200); |
bma180.SoftReset(); |
bma180.enableWrite(); |
bma180.SetFilter(bma180.F10HZ); |
bma180.setGSensitivty(bma180.G15); |
bma180.SetSMPSkip(); |
bma180.SetISRMode(); |
bma180.disableWrite(); |
delay(100); |
} |
void loop() |
{ |
bma180.readAccel(); |
Serial.print(bma180.x,DEC); |
Serial.print(","); |
Serial.print(bma180.y,DEC); |
Serial.print(","); |
Serial.println(bma180.z,DEC); |
delay(20); |
} |
/branches/V0.82b-Arthur-P/bma180/examples/isr_read/isr_read.pde |
---|
0,0 → 1,99 |
#include <Wire.h> |
#include <bma180.h> |
#define DEBUGOFF |
BMA180 bma180 = BMA180(BMA180_ADDRESS_SDO_LOW); |
volatile byte newData=0; |
volatile unsigned int countISR=0; |
unsigned int served=0; |
void BMAISR(void) |
{ |
countISR++; |
//bma180.readAccel(); |
newData=1; |
} |
void setup() |
{ |
Wire.begin(); |
Serial.begin(115200); |
bma180.SoftReset(); |
bma180.enableWrite(); |
int sversion; |
int id; |
bma180.getIDs(&id,&sversion); |
Serial.print("Id = "); |
Serial.print(id,DEC); |
Serial.print(" v."); |
Serial.println(sversion,HEX); |
bma180.SetFilter(bma180.F10HZ); |
bma180.setGSensitivty(bma180.G15); |
attachInterrupt(0, BMAISR, RISING); |
bma180.SetSMPSkip(); |
bma180.SetISRMode(); |
bma180.disableWrite(); |
delay(2000); |
} |
void loop() |
{ |
if (newData) |
{ |
newData=0; |
bma180.readAccel(); |
Serial.print("["); |
Serial.print(bma180.x,DEC); |
Serial.print(" "); |
Serial.print(bma180.y,DEC); |
Serial.print(" "); |
Serial.print(bma180.z,DEC); |
Serial.println("]"); |
} |
#ifdef DEBUGON |
if (millis() > tAlive) |
{ |
bma180.readAccel(); |
Serial.print("ISR's="); |
Serial.print(countISR); |
Serial.print(" served="); |
Serial.println(served); |
Serial.print("t="); |
Serial.print(bma180.temp); |
Serial.print("["); |
Serial.print(bma180.x,DEC); |
Serial.print(" "); |
Serial.print(bma180.y,DEC); |
Serial.print(" "); |
Serial.print(bma180.z,DEC); |
Serial.println("]"); |
tAlive=millis()+5000; |
Serial.print("filter reg="); |
Serial.println(bma180.getRegValue(0x20),BIN); |
Serial.print("status_reg1="); |
Serial.println(bma180.getRegValue(0x09),BIN); |
Serial.print("status_reg2="); |
Serial.println(bma180.getRegValue(0x0A),BIN); |
Serial.print("status_reg3="); |
Serial.println(bma180.getRegValue(0x0B),BIN); |
Serial.print("status_reg4="); |
Serial.println(bma180.getRegValue(0x0C),BIN); |
Serial.print("ctrl_reg0="); |
Serial.println(bma180.getRegValue(0x0d),BIN); |
Serial.print("ctrl_reg1="); |
Serial.println(bma180.getRegValue(0x0e),BIN); |
Serial.print("ctrl_reg2="); |
Serial.println(bma180.getRegValue(0x0f),BIN); |
Serial.print("ctrl_reg3="); |
Serial.println(bma180.getRegValue(0x21),BIN); |
Serial.print("ctrl_reg4="); |
Serial.println(bma180.getRegValue(0x22),BIN); |
} |
#endif |
} |
/branches/V0.82b-Arthur-P/bma180/keywords.txt |
---|
0,0 → 1,50 |
####################################### |
# Syntax Coloring Map for BMA180 |
####################################### |
####################################### |
# Datatypes (KEYWORD1) |
####################################### |
BMA180 KEYWORD1 |
GSENSITIVITY KEYWORD1 |
FILTER KEYWORD1 |
####################################### |
# Methods and Functions (KEYWORD2) |
####################################### |
setAddress KEYWORD2 |
readAccel KEYWORD2 |
getGSense KEYWORD2 |
getXValFloat KEYWORD2 |
getYValFloat KEYWORD2 |
getZValFloat KEYWORD2 |
setRegValue KEYWORD2 |
getRegValue KEYWORD2 |
setGSensitivity KEYWORD2 |
setFilter KEYWORD2 |
setISRMode KEYWORD2 |
softReset KEYWORD2 |
setSMPSkip KEYWORD2 |
getIDs KEYWORD2 |
enableWrite KEYWORD2 |
disableWrite KEYWORD2 |
####################################### |
# Constants (LITERAL1) |
####################################### |
checkResult KEYWORD3 |
F10HZ KEYWORD3 |
F20HZ KEYWORD3 |
F75HZ KEYWORD3 |
F15HZ0 KEYWORD3 |
F300HZ KEYWORD3 |
F600HZ KEYWORD3 |
F1200HZ KEYWORD3 |
G1 KEYWORD3 |
G15 KEYWORD3 |
G2 KEYWORD3 |
G3 KEYWORD3 |
G4 KEYWORD3 |
G8 KEYWORD3 |
G16 KEYWORD3 |