Hello,
It is normal for accelerometers to have some noise on measurements. The accelerometer has an embedded high-pass filter, but we would need a low-pass one. My suggestion is to read accel values and place them on a software FIFO Buffer (not the internal accelerometer FIFO, which is used for special functions). Then you may average values in a moving window. For example, you read 10 samples and place them in a vector (positions 0-9), and average them. When you read next value, you place result on vector's position 0, and average the 10 values again, so now you actually have averages of samples 1-10. At next sample, you replace vector's position 1 with the new value, and average the 10 values again (samples 2-11), and so on. Averaging multiple samples should reduce noise. Here is a possible C code for that, but keep in mind that I have not tested it.
At the beginning of main:
unsigned short raw;
short converted, samples[10], averaged;
int sum = 0;
char k, p = 0;
In the main function:
for(k = 0; k < 10; k++) { // Fills in the first 10 samples
raw = MMA1_MeasureGetRawX();
converted = ((short) raw) / 4; // Typecast raw value to signed 16-bit, and shifts 2 bite to the right without losing signal
samples[k] = converted;
sum += converted;
}
averaged = sum / 10; // Average of 10 first samples
// Do whatever you want to do with the averaged sample
for(;;) { // Supposing it is an infinite loop
raw = MMA1_MeasureGetRawX();
converted = ((short) raw) / 4;
samples[p] = converted;
p++;
sum = 0;
for(k = 0; k < 10; k++) {
sum += converted;
}
averaged = sum / 10;
// Do whatever you want to do with the averaged sample
}
This is the basic idea. Actually you may optimize the averaging process after the first averaged sample. Instead of summing all values again, you just subtract the oldest sample from "sum", and add the newest one to it, and finally divide it by 10 to "average":
for(;;) { // Supposing it is an infinite loop
raw = MMA1_MeasureGetRawX();
converted = ((short) raw) / 4;
sum -= samples[p];
sum += converted;
samples[p] = converted;
p++;
averaged = sum / 10;
// Do whatever you want to do with the averaged sample
}
Good luck!
Antonio