Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1993 | - | 1 | |
2 | #include <avr/pgmspace.h> |
||
3 | #include <wiring.h> |
||
4 | |||
5 | #include "bma180.h" |
||
6 | |||
7 | BMA180::BMA180() |
||
8 | { |
||
9 | gSense=G2; |
||
10 | } |
||
11 | |||
12 | void BMA180::setAddress(int adr) |
||
13 | { |
||
14 | address=(unsigned char) adr; |
||
15 | } |
||
16 | |||
17 | int temp; |
||
18 | |||
19 | void BMA180::readAccel(int * x, int * y, int * z) |
||
20 | { |
||
21 | unsigned int result; |
||
22 | |||
23 | Wire.beginTransmission(address); |
||
24 | Wire.send(0x02); |
||
25 | Wire.endTransmission(); |
||
26 | Wire.requestFrom((int)address, 7); |
||
27 | if(Wire.available()==7) |
||
28 | { |
||
29 | int lsb = Wire.receive()>>2; |
||
30 | int msb = Wire.receive(); |
||
31 | *x=(msb<<6)+lsb; |
||
32 | if ((*x)&0x2000) (*x)|=0xc000; // set full 2 complement for neg values |
||
33 | lsb = Wire.receive()>>2; |
||
34 | msb = Wire.receive(); |
||
35 | *y=(msb<<6)+lsb; |
||
36 | if ((*y)&0x2000) (*y)|=0xc000; |
||
37 | lsb = Wire.receive()>>2; |
||
38 | msb = Wire.receive(); |
||
39 | *z=(msb<<6)+lsb; |
||
40 | if ((*z)&0x2000) (*z)|=0xc000; |
||
41 | temp = Wire.receive(); |
||
42 | if (temp&0x80) temp|=0xff00; |
||
43 | } |
||
44 | result = Wire.endTransmission(); |
||
45 | } |
||
46 | |||
47 | float BMA180::getGSense() |
||
48 | { |
||
49 | switch(gSense) |
||
50 | { |
||
51 | case G1: return 1.0; |
||
52 | case G15: return 1.5; |
||
53 | case G2: return 2.0; |
||
54 | case G3: return 3.0; |
||
55 | case G4: return 4.0; |
||
56 | case G8: return 8.0; |
||
57 | case G16: return 16.0; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | float BMA180::getXValFloat() |
||
62 | { |
||
63 | // normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
||
64 | return (float)x/8191.0*getGSense(); |
||
65 | } |
||
66 | float BMA180::getYValFloat() |
||
67 | { |
||
68 | // normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
||
69 | return (float)y/8191.0*getGSense(); |
||
70 | } |
||
71 | float BMA180::getZValFloat() |
||
72 | { |
||
73 | // normalize (if x is maximum (8191) and GSENSE=1.0 then 1.0 |
||
74 | return (float)z/8191.0*getGSense(); |
||
75 | } |
||
76 | |||
77 | int BMA180::getRegValue(int adr) |
||
78 | { |
||
79 | int val; |
||
80 | Wire.beginTransmission(address); |
||
81 | Wire.send(adr); |
||
82 | Wire.endTransmission(); |
||
83 | Wire.requestFrom((int)address, 1); |
||
84 | if (Wire.available()==1) |
||
85 | { |
||
86 | val = Wire.receive(); |
||
87 | } |
||
88 | else val=-1; |
||
89 | int result = Wire.endTransmission(); |
||
90 | checkResult(result); |
||
91 | return val; |
||
92 | } |
||
93 | |||
94 | void BMA180::setRegValue(int regAdr, int val, int maskPreserve) |
||
95 | { |
||
96 | int preserve=getRegValue(regAdr); |
||
97 | int orgval=preserve & maskPreserve; |
||
98 | Wire.beginTransmission(address); |
||
99 | Wire.send(regAdr); |
||
100 | Wire.send(orgval|val); |
||
101 | int result = Wire.endTransmission(); |
||
102 | checkResult(result); |
||
103 | |||
104 | } |
||
105 | |||
106 | void BMA180::setGSensitivty(GSENSITIVITY maxg) //1, 1.5 2 3 4 8 16 |
||
107 | { |
||
108 | setRegValue(0x35,maxg<<1,0xF1); |
||
109 | } |
||
110 | |||
111 | void BMA180::SetFilter(FILTER f) // 10,20,40,75,150,300,600,1200, HP 1HZ,BP 0.2-300, higher values not authorized |
||
112 | { |
||
113 | setRegValue(0x20,f<<4,0x0F); |
||
114 | } |
||
115 | |||
116 | void BMA180::SetISRMode() // you must provide a ISR function on the pin selected (pin 2 or 3,. so INT0 or INT1) |
||
117 | { |
||
118 | setRegValue(0x21,2,0xFD); |
||
119 | } |
||
120 | |||
121 | void BMA180::SoftReset() // all values will be default |
||
122 | { |
||
123 | setRegValue(0x10,0xB6,0); |
||
124 | delay(100); |
||
125 | } |
||
126 | |||
127 | void BMA180::SetSMPSkip() |
||
128 | { |
||
129 | setRegValue(0x35, 1, 0xFE); |
||
130 | } |
||
131 | |||
132 | int BMA180::getIDs(int *id, int *version) |
||
133 | { |
||
134 | Wire.beginTransmission(address); |
||
135 | Wire.send(0x0); |
||
136 | Wire.endTransmission(); |
||
137 | Wire.requestFrom((int)address, 2); |
||
138 | if (Wire.available()==2) |
||
139 | { |
||
140 | *id = Wire.receive(); |
||
141 | *version= Wire.receive(); |
||
142 | } |
||
143 | else *id=-1; |
||
144 | int result = Wire.endTransmission(); |
||
145 | checkResult(result); |
||
146 | return *id!=-1; |
||
147 | } |
||
148 | |||
149 | |||
150 | void BMA180::enableWrite() |
||
151 | { |
||
152 | //ctrl_reg1 register set ee_w bit to enable writing to regs. |
||
153 | setRegValue(0x0D,0x10,~0x10); |
||
154 | delay(10); |
||
155 | } |
||
156 | |||
157 | |||
158 | void BMA180::disableWrite() |
||
159 | { |
||
160 | setRegValue(0x0D,0x0,~0x10); |
||
161 | delay(10); |
||
162 | } |
||
163 | |||
164 | bool BMA180::checkResult(int result) |
||
165 | { |
||
166 | if(result >= 1) |
||
167 | return false; |
||
168 | return true; |
||
169 | } |
||
170 | |||
171 |