Chandra Tjhai
/
ENGO333_Lab_tiltmeter
Simple tiltmeter using accelerometer
ENGO333_MMA7660.cpp@0:7d6134e052e0, 2019-11-23 (annotated)
- Committer:
- chtjhai
- Date:
- Sat Nov 23 04:58:09 2019 +0000
- Revision:
- 0:7d6134e052e0
Simple tiltmeter using accelerometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chtjhai | 0:7d6134e052e0 | 1 | #include "ENGO333_MMA7660.h" |
chtjhai | 0:7d6134e052e0 | 2 | |
chtjhai | 0:7d6134e052e0 | 3 | const float GRAVITY = 9.8086; |
chtjhai | 0:7d6134e052e0 | 4 | |
chtjhai | 0:7d6134e052e0 | 5 | ENGO333_MMA7660::ENGO333_MMA7660() : i2c(p28, p27) |
chtjhai | 0:7d6134e052e0 | 6 | { |
chtjhai | 0:7d6134e052e0 | 7 | SetActiveMode(); |
chtjhai | 0:7d6134e052e0 | 8 | SetSamplingRateAM(MMA7660_AMSR8); |
chtjhai | 0:7d6134e052e0 | 9 | measAccel[0] = measAccel[1] = measAccel[2] = 0; |
chtjhai | 0:7d6134e052e0 | 10 | } |
chtjhai | 0:7d6134e052e0 | 11 | |
chtjhai | 0:7d6134e052e0 | 12 | bool ENGO333_MMA7660::TestConnection() |
chtjhai | 0:7d6134e052e0 | 13 | { |
chtjhai | 0:7d6134e052e0 | 14 | char data[3] = {128, 128, 128}; |
chtjhai | 0:7d6134e052e0 | 15 | bool alert; |
chtjhai | 0:7d6134e052e0 | 16 | |
chtjhai | 0:7d6134e052e0 | 17 | // Check data validity |
chtjhai | 0:7d6134e052e0 | 18 | alert = false; |
chtjhai | 0:7d6134e052e0 | 19 | i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); |
chtjhai | 0:7d6134e052e0 | 20 | for (int i = 0; i < 3; ++i) { |
chtjhai | 0:7d6134e052e0 | 21 | if (data[i] > 63) |
chtjhai | 0:7d6134e052e0 | 22 | alert = true; |
chtjhai | 0:7d6134e052e0 | 23 | } |
chtjhai | 0:7d6134e052e0 | 24 | |
chtjhai | 0:7d6134e052e0 | 25 | if (alert == false) { |
chtjhai | 0:7d6134e052e0 | 26 | return true; |
chtjhai | 0:7d6134e052e0 | 27 | } else { |
chtjhai | 0:7d6134e052e0 | 28 | return false; |
chtjhai | 0:7d6134e052e0 | 29 | } |
chtjhai | 0:7d6134e052e0 | 30 | } |
chtjhai | 0:7d6134e052e0 | 31 | |
chtjhai | 0:7d6134e052e0 | 32 | void ENGO333_MMA7660::SetActiveMode() |
chtjhai | 0:7d6134e052e0 | 33 | { |
chtjhai | 0:7d6134e052e0 | 34 | i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_MODE_REG, 0x01); |
chtjhai | 0:7d6134e052e0 | 35 | } |
chtjhai | 0:7d6134e052e0 | 36 | |
chtjhai | 0:7d6134e052e0 | 37 | void ENGO333_MMA7660::SetSamplingRateAM(MMA7660_AMSR_t samplingRate) |
chtjhai | 0:7d6134e052e0 | 38 | { |
chtjhai | 0:7d6134e052e0 | 39 | char oldValue, newValue; |
chtjhai | 0:7d6134e052e0 | 40 | oldValue = i2c.readOneByte(MMA7660_ADDRESS, MMA7660_SR_REG); |
chtjhai | 0:7d6134e052e0 | 41 | oldValue = oldValue & 0x07; |
chtjhai | 0:7d6134e052e0 | 42 | newValue = oldValue | samplingRate; |
chtjhai | 0:7d6134e052e0 | 43 | i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_SR_REG, newValue); |
chtjhai | 0:7d6134e052e0 | 44 | } |
chtjhai | 0:7d6134e052e0 | 45 | |
chtjhai | 0:7d6134e052e0 | 46 | void ENGO333_MMA7660::ReadAccelerometers() |
chtjhai | 0:7d6134e052e0 | 47 | { |
chtjhai | 0:7d6134e052e0 | 48 | char data[3]; |
chtjhai | 0:7d6134e052e0 | 49 | bool alert; |
chtjhai | 0:7d6134e052e0 | 50 | |
chtjhai | 0:7d6134e052e0 | 51 | // Check data validity |
chtjhai | 0:7d6134e052e0 | 52 | do { |
chtjhai | 0:7d6134e052e0 | 53 | alert = false; |
chtjhai | 0:7d6134e052e0 | 54 | i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); |
chtjhai | 0:7d6134e052e0 | 55 | for (int i = 0; i < 3; ++i) { |
chtjhai | 0:7d6134e052e0 | 56 | if (data[i] > 63) |
chtjhai | 0:7d6134e052e0 | 57 | alert = true; |
chtjhai | 0:7d6134e052e0 | 58 | if (data[i] > 31) |
chtjhai | 0:7d6134e052e0 | 59 | data[i] += 128 + 64; |
chtjhai | 0:7d6134e052e0 | 60 | } |
chtjhai | 0:7d6134e052e0 | 61 | } while (alert); |
chtjhai | 0:7d6134e052e0 | 62 | |
chtjhai | 0:7d6134e052e0 | 63 | measAccel[0] = (signed char)(data[0]) / MMA7660_SCALE * GRAVITY; |
chtjhai | 0:7d6134e052e0 | 64 | measAccel[1] = (signed char)(data[1]) / MMA7660_SCALE * GRAVITY; |
chtjhai | 0:7d6134e052e0 | 65 | measAccel[2] = (signed char)(data[2]) / MMA7660_SCALE * GRAVITY; |
chtjhai | 0:7d6134e052e0 | 66 | } |
chtjhai | 0:7d6134e052e0 | 67 | |
chtjhai | 0:7d6134e052e0 | 68 | float ENGO333_MMA7660::GetAccelX() const |
chtjhai | 0:7d6134e052e0 | 69 | { |
chtjhai | 0:7d6134e052e0 | 70 | return measAccel[0]; |
chtjhai | 0:7d6134e052e0 | 71 | } |
chtjhai | 0:7d6134e052e0 | 72 | |
chtjhai | 0:7d6134e052e0 | 73 | float ENGO333_MMA7660::GetAccelY() const |
chtjhai | 0:7d6134e052e0 | 74 | { |
chtjhai | 0:7d6134e052e0 | 75 | return measAccel[1]; |
chtjhai | 0:7d6134e052e0 | 76 | } |
chtjhai | 0:7d6134e052e0 | 77 | |
chtjhai | 0:7d6134e052e0 | 78 | float ENGO333_MMA7660::GetAccelZ() const |
chtjhai | 0:7d6134e052e0 | 79 | { |
chtjhai | 0:7d6134e052e0 | 80 | return measAccel[2]; |
chtjhai | 0:7d6134e052e0 | 81 | } |
chtjhai | 0:7d6134e052e0 | 82 | |
chtjhai | 0:7d6134e052e0 | 83 |