-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathaccel_sensor.c
234 lines (193 loc) · 6.09 KB
/
accel_sensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* See license.txt for license information. */
#include "mywisp.h"
#if (ACTIVE_SENSOR == SENSOR_ACCEL)
#include "dlwisp41.h"
#include "accel_sensor.h"
#if DEBUG_BAD_SAMPLES
short lastx = 0xffff, lasty = 0xffff, lastz = 0xffff;
short x = 0xffff, y = 0xffff, z = 0xffff;
short diff = 0;
#endif
void init_sensor()
{
return;
}
#if 1
void read_sensor(unsigned char volatile *target)
{
unsigned short enough_power;
if(!is_power_good())
sleep();
P1OUT &= ~RX_EN_PIN; // turn off comparator
// slow down clock
BCSCTL1 = XT2OFF + RSEL3;
DCOCTL = DCO2 + DCO1 + DCO0;
// Power sensor, enable analog in
ADC10AE0 |= (X_INCH + Y_INCH + Z_INCH);
SET_ACCEL_ENABLE_DIR;
TURN_ON_ACCEL_ENABLE;
// set up watchdog interval timer to sleep during settle time
WDTCTL = WDT_MDLY_0_5;
IE1 |= WDTIE;
P1IE = 0;
P2IE = 0;
// a little time for regulator to stabilize active mode current AND
// filter caps to settle. for WDT_MDLY_0_5 * 46, this is slightly less than 10 ms
for ( int k = 0 ; k < 46 ; k++ )
{
#if CHECK_FOR_GOOD_VOLTAGE
//DEBUG_PIN5_HIGH;
if ( k == 1 )
{
//DEBUG_PIN5_HIGH;
enough_power = is_power_good();
//DEBUG_PIN5_LOW;
}
#endif
_BIS_SR(LPM1_bits+GIE);
//DEBUG_PIN5_LOW;
}
IE1 &= ~WDTIE;
#if CHECK_FOR_GOOD_VOLTAGE
// make sure there's enough voltage to generate good samples. samples
// get seriously wacky at ~1.8V, especially the already-noisy Z channel.
// see the accel data sheet for details.
if ( ! enough_power )
{
// low voltage -- don't sample
//DEBUG_PIN5_HIGH;
CLEAR_ACCEL_ENABLE_DIR;
TURN_OFF_ACCEL_ENABLE;
ADC10AE0 &= ~(X_INCH + Y_INCH + Z_INCH);
ADC10CTL0 &= ~ENC;
ADC10CTL1 = 0; // turn adc off
ADC10CTL0 = 0; // turn adc off
//DEBUG_PIN5_LOW;
//return 0;
}
#endif
// grab data
for (int i = (DATA_LENGTH_IN_WORDS-1); i >= 0; i--)
{
ADC10CTL0 &= ~ENC; // make sure this is off otherwise settings are locked.
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE;
if (i == 2)
// sample Z channel first, because it is the most noisy
ADC10CTL1 = ADC10DIV_4 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + Z_INCH;
else if (i == 1)
ADC10CTL1 = ADC10DIV_4 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + Y_INCH;
else
ADC10CTL1 = ADC10DIV_4 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + X_INCH;
ADC10CTL0 |= ENC;
ADC10CTL0 |= ADC10SC;
LPM4;
unsigned int k;
if ( i == 0 ) k = 0;
else if ( i == 1 ) k = 2;
else k = 4;
// grab sample and write it to ram
*(target + k + 1 ) = (ADC10MEM & 0xff);
*(target + k) = (ADC10MEM & 0x0300) >> 8;
#if DEBUG_BAD_SAMPLES
if ( i == 0 ) x = ADC10MEM;
else if ( i == 1 ) y = ADC10MEM;
else z = ADC10MEM;
#endif
k += 2;
}
#if DEBUG_BAD_SAMPLES
//#define THRES 50 // about 5% ... never fires
//#define THRES 30 // about 3% ... fires very rarely
//#define THRES 20
#define THRES 10
unsigned short fired = 0;
diff = ((x - lastx) > 0) ? (x - lastx) : (lastx -x);
if ( lastx != 0xffff && diff > THRES )
{
fired = 1;
}
diff = ((y - lasty) > 0) ? (y - lasty) : (lasty -y);
if ( lasty != 0xffff && diff > THRES )
{
fired = 1;
}
diff = ((z - lastz) > 0) ? (z- lastz) : (lastz -z);
if ( lastz != 0xffff && diff > THRES )
{
fired = 1;
}
lastx = x; lasty = y ; lastz = z;
if ( fired ) { DEBUG_PIN5_HIGH; DEBUG_PIN5_LOW; }
#endif
// Power off sensor and adc
CLEAR_ACCEL_ENABLE_DIR;
TURN_OFF_ACCEL_ENABLE;
ADC10AE0 &= ~(X_INCH + Y_INCH + Z_INCH);
ADC10CTL0 &= ~ENC;
ADC10CTL1 = 0; // turn adc off
ADC10CTL0 = 0; // turn adc off
//return 1;
}
#endif
#if 0
void read_sensor(unsigned char volatile *target)
{
static short cntr = 0;
if(!is_power_good())
sleep();
P1OUT &= ~RX_EN_PIN; // turn off comparator
// set up watchdog interval timer to sleep during settle time
WDTCTL = WDT_MDLY_0_5;
IE1 |= WDTIE;
// a little time for regulator to stabilize active mode current AND
// filter caps to settle. for WDT_MDLY_0_5 * 48, this is 10 ms
DEBUG_PIN5_HIGH;
for ( int k = 0 ; k < 48 ; k++ )
{
_BIS_SR(LPM1_bits+GIE);
}
DEBUG_PIN5_LOW;
IE1 &= ~WDTIE;
if ( !is_power_good() )
{
DEBUG_PIN5_HIGH;
DEBUG_PIN5_LOW;
//return 0;
}
//#define DELTA 0x05
#define DELTA 0x0B
//#define DELTA 0x14
#define HIBYTE 0x01
//#define HIBYTE 0x02
*(target) = HIBYTE;
*(target + 1 ) = 0xF8;
*(target + 2) = HIBYTE;
*(target + 3 ) = 0xF3;
*(target + 4) = HIBYTE;
*(target + 5 ) = 0xCA;
if ( cntr++ == 2 )
{
*(target + 1 ) = 0xF8 - DELTA;
*(target + 3 ) = 0xF3 - DELTA;
*(target + 5 ) = 0xCA + DELTA;
cntr = 0;
}
//return 1;
}
#endif
// used to sleep while taking adc, this wakes us
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
ADC10CTL0 &= ~ENC; // make sure this is off otherwise settings are locked.
ADC10CTL0 &= ~(ADC10IFG | ADC10IE);
LPM4_EXIT;
return;
}
#pragma vector=WDT_VECTOR
__interrupt void wdt_ISR( void )
{
LPM4_EXIT;
return;
}
#endif // (ACTIVE_SENSOR == SENSOR_ACCEL)