![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3 axis accellerometer for pitch and roll on lcd and serial output
MMA7660.cpp@5:5779c52fa70e, 2020-12-05 (annotated)
- Committer:
- saltire78
- Date:
- Sat Dec 05 12:57:45 2020 +0000
- Revision:
- 5:5779c52fa70e
- Parent:
- 4:36a163511e34
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:7bc29a9ea016 | 1 | #include "MMA7660.h" |
Sissors | 0:7bc29a9ea016 | 2 | |
Sissors | 2:a8e20db7901e | 3 | MMA7660::MMA7660(PinName sda, PinName scl, bool active) : _i2c(sda, scl) |
Sissors | 0:7bc29a9ea016 | 4 | { |
Sissors | 2:a8e20db7901e | 5 | setActive(active); |
Sissors | 1:8997a1b348dd | 6 | samplerate = 64; |
Sissors | 0:7bc29a9ea016 | 7 | } |
Sissors | 0:7bc29a9ea016 | 8 | |
Sissors | 0:7bc29a9ea016 | 9 | //Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address |
Sissors | 0:7bc29a9ea016 | 10 | bool MMA7660::testConnection( void ) |
Sissors | 0:7bc29a9ea016 | 11 | { |
Sissors | 0:7bc29a9ea016 | 12 | if (_i2c.write(MMA7660_ADDRESS, NULL, 0) == 0 ) |
Sissors | 0:7bc29a9ea016 | 13 | return true; |
Sissors | 0:7bc29a9ea016 | 14 | else |
Sissors | 0:7bc29a9ea016 | 15 | return false; |
Sissors | 0:7bc29a9ea016 | 16 | } |
Sissors | 0:7bc29a9ea016 | 17 | |
Sissors | 0:7bc29a9ea016 | 18 | void MMA7660::setActive(bool state) |
Sissors | 0:7bc29a9ea016 | 19 | { |
Sissors | 3:89cb08cc663b | 20 | active = state; |
Sissors | 0:7bc29a9ea016 | 21 | char modereg = read(MMA7660_MODE_R); |
Sissors | 0:7bc29a9ea016 | 22 | modereg &= ~(1<<0); |
Sissors | 0:7bc29a9ea016 | 23 | |
Sissors | 0:7bc29a9ea016 | 24 | //If it somehow was in testmode, disable that |
Sissors | 0:7bc29a9ea016 | 25 | if (modereg && (1<<2)) { |
Sissors | 0:7bc29a9ea016 | 26 | modereg &= ~(1<<2); |
Sissors | 0:7bc29a9ea016 | 27 | write(MMA7660_MODE_R, modereg); |
Sissors | 0:7bc29a9ea016 | 28 | } |
Sissors | 0:7bc29a9ea016 | 29 | |
Sissors | 0:7bc29a9ea016 | 30 | modereg += state; |
Sissors | 0:7bc29a9ea016 | 31 | write(MMA7660_MODE_R, modereg); |
Sissors | 0:7bc29a9ea016 | 32 | } |
Sissors | 0:7bc29a9ea016 | 33 | |
Sissors | 0:7bc29a9ea016 | 34 | void MMA7660::readData(int *data) |
Sissors | 0:7bc29a9ea016 | 35 | { |
Sissors | 3:89cb08cc663b | 36 | bool active_old = active; |
Sissors | 0:7bc29a9ea016 | 37 | if (!active) { |
Sissors | 0:7bc29a9ea016 | 38 | setActive(true); |
Sissors | 1:8997a1b348dd | 39 | wait(0.012 + 1/samplerate); //Wait until new sample is ready, my experience is that 1/samplerate isnt needed, but datasheet says so |
Sissors | 0:7bc29a9ea016 | 40 | } |
Sissors | 0:7bc29a9ea016 | 41 | |
Sissors | 0:7bc29a9ea016 | 42 | char temp[3]; |
Sissors | 0:7bc29a9ea016 | 43 | bool alert; |
Sissors | 0:7bc29a9ea016 | 44 | |
Sissors | 0:7bc29a9ea016 | 45 | do { |
Sissors | 0:7bc29a9ea016 | 46 | alert = false; |
Sissors | 0:7bc29a9ea016 | 47 | read(MMA7660_XOUT_R, temp, 3); |
Sissors | 0:7bc29a9ea016 | 48 | for (int i = 0; i<3; i++) { |
Sissors | 0:7bc29a9ea016 | 49 | if (temp[i] > 63) |
Sissors | 0:7bc29a9ea016 | 50 | alert = true; |
Sissors | 0:7bc29a9ea016 | 51 | if (temp[i] > 31) |
Sissors | 0:7bc29a9ea016 | 52 | temp[i] += 128+64; |
Sissors | 0:7bc29a9ea016 | 53 | data[i] = (signed char)temp[i]; |
Sissors | 0:7bc29a9ea016 | 54 | } |
Sissors | 0:7bc29a9ea016 | 55 | } while (alert); |
Sissors | 2:a8e20db7901e | 56 | |
Sissors | 3:89cb08cc663b | 57 | if (!active_old) |
Sissors | 2:a8e20db7901e | 58 | setActive(false); |
Sissors | 0:7bc29a9ea016 | 59 | } |
Sissors | 0:7bc29a9ea016 | 60 | |
Sissors | 1:8997a1b348dd | 61 | |
Sissors | 0:7bc29a9ea016 | 62 | void MMA7660::readData(float *data) |
Sissors | 0:7bc29a9ea016 | 63 | { |
Sissors | 0:7bc29a9ea016 | 64 | int intdata[3]; |
Sissors | 0:7bc29a9ea016 | 65 | readData(intdata); |
Sissors | 0:7bc29a9ea016 | 66 | for (int i = 0; i<3; i++) |
Sissors | 0:7bc29a9ea016 | 67 | data[i] = intdata[i]/MMA7660_SENSITIVITY; |
Sissors | 0:7bc29a9ea016 | 68 | } |
Sissors | 0:7bc29a9ea016 | 69 | |
Sissors | 2:a8e20db7901e | 70 | float MMA7660::x( void ) |
Sissors | 1:8997a1b348dd | 71 | { |
Sissors | 0:7bc29a9ea016 | 72 | return getSingle(0); |
Sissors | 1:8997a1b348dd | 73 | } |
Sissors | 1:8997a1b348dd | 74 | |
Sissors | 2:a8e20db7901e | 75 | float MMA7660::y( void ) |
Sissors | 1:8997a1b348dd | 76 | { |
Sissors | 1:8997a1b348dd | 77 | return getSingle(1); |
Sissors | 1:8997a1b348dd | 78 | } |
Sissors | 1:8997a1b348dd | 79 | |
Sissors | 2:a8e20db7901e | 80 | float MMA7660::z( void ) |
Sissors | 1:8997a1b348dd | 81 | { |
Sissors | 1:8997a1b348dd | 82 | return getSingle(2); |
Sissors | 1:8997a1b348dd | 83 | } |
Sissors | 1:8997a1b348dd | 84 | |
Sissors | 1:8997a1b348dd | 85 | |
Sissors | 1:8997a1b348dd | 86 | void MMA7660::setSampleRate(int samplerate) |
Sissors | 1:8997a1b348dd | 87 | { |
Sissors | 3:89cb08cc663b | 88 | bool active_old = active; |
Sissors | 1:8997a1b348dd | 89 | setActive(false); //Not allowed to be active to change anything |
Sissors | 1:8997a1b348dd | 90 | int rates[] = {120, 64, 32, 16, 8, 4, 2, 1}; //Alowed samplerates (and their number in array is also number required for MMA) |
Sissors | 1:8997a1b348dd | 91 | int sampleLoc = 0, sampleError = 10000, temp; |
Sissors | 1:8997a1b348dd | 92 | for (int i = 0; i<8; i++) { |
Sissors | 1:8997a1b348dd | 93 | temp = abs( rates[i] - samplerate ); |
Sissors | 1:8997a1b348dd | 94 | if (temp<sampleError) { |
Sissors | 1:8997a1b348dd | 95 | sampleLoc = i; |
Sissors | 1:8997a1b348dd | 96 | sampleError=temp; |
Sissors | 1:8997a1b348dd | 97 | } |
Sissors | 0:7bc29a9ea016 | 98 | } |
Sissors | 0:7bc29a9ea016 | 99 | |
Sissors | 1:8997a1b348dd | 100 | //Update the samplerate reg |
Sissors | 1:8997a1b348dd | 101 | temp = read(MMA7660_SR_R); |
Sissors | 2:a8e20db7901e | 102 | temp &= ~0x07; //Awake sample rate are lowest 3 bit |
Sissors | 1:8997a1b348dd | 103 | temp |= sampleLoc; |
Sissors | 1:8997a1b348dd | 104 | write(MMA7660_SR_R, temp); |
Sissors | 1:8997a1b348dd | 105 | this->samplerate = rates[sampleLoc]; |
Sissors | 3:89cb08cc663b | 106 | setActive(active_old); //Restore previous active state |
Sissors | 1:8997a1b348dd | 107 | } |
Sissors | 1:8997a1b348dd | 108 | |
Sissors | 1:8997a1b348dd | 109 | |
Sissors | 1:8997a1b348dd | 110 | MMA7660::Orientation MMA7660::getSide( void ) |
Sissors | 1:8997a1b348dd | 111 | { |
Sissors | 1:8997a1b348dd | 112 | char tiltreg = read(MMA7660_TILT_R); |
Sissors | 1:8997a1b348dd | 113 | //We care about 2 LSBs |
Sissors | 1:8997a1b348dd | 114 | tiltreg &= 0x03; |
Sissors | 1:8997a1b348dd | 115 | if (tiltreg == 0x01) |
Sissors | 1:8997a1b348dd | 116 | return MMA7660::Front; |
Sissors | 1:8997a1b348dd | 117 | if (tiltreg == 0x02) |
Sissors | 1:8997a1b348dd | 118 | return MMA7660::Back; |
Sissors | 1:8997a1b348dd | 119 | return MMA7660::Unknown; |
Sissors | 1:8997a1b348dd | 120 | } |
Sissors | 1:8997a1b348dd | 121 | |
Sissors | 1:8997a1b348dd | 122 | MMA7660::Orientation MMA7660::getOrientation( void ) |
Sissors | 1:8997a1b348dd | 123 | { |
Sissors | 1:8997a1b348dd | 124 | char tiltreg = read(MMA7660_TILT_R); |
Sissors | 1:8997a1b348dd | 125 | |
Sissors | 1:8997a1b348dd | 126 | //We care about bit 2, 3 and 4 (counting from zero) |
Sissors | 1:8997a1b348dd | 127 | tiltreg &= 0x07<<2; |
Sissors | 1:8997a1b348dd | 128 | tiltreg >>= 2; |
Sissors | 1:8997a1b348dd | 129 | if (tiltreg == 0x01) |
Sissors | 1:8997a1b348dd | 130 | return MMA7660::Left; |
Sissors | 1:8997a1b348dd | 131 | if (tiltreg == 0x02) |
Sissors | 1:8997a1b348dd | 132 | return MMA7660::Right; |
Sissors | 1:8997a1b348dd | 133 | if (tiltreg == 0x05) |
Sissors | 1:8997a1b348dd | 134 | return MMA7660::Down; |
Sissors | 1:8997a1b348dd | 135 | if (tiltreg == 0x06) |
Sissors | 1:8997a1b348dd | 136 | return MMA7660::Up; |
Sissors | 1:8997a1b348dd | 137 | return MMA7660::Unknown; |
Sissors | 1:8997a1b348dd | 138 | } |
Sissors | 1:8997a1b348dd | 139 | |
Sissors | 2:a8e20db7901e | 140 | |
Sissors | 1:8997a1b348dd | 141 | |
Sissors | 1:8997a1b348dd | 142 | ////////////////////////////////////////////// |
Sissors | 1:8997a1b348dd | 143 | ///////////////PRIVATE//////////////////////// |
Sissors | 1:8997a1b348dd | 144 | ////////////////////////////////////////////// |
Sissors | 1:8997a1b348dd | 145 | |
Sissors | 0:7bc29a9ea016 | 146 | |
Sissors | 0:7bc29a9ea016 | 147 | void MMA7660::write(char address, char data) |
Sissors | 0:7bc29a9ea016 | 148 | { |
Sissors | 0:7bc29a9ea016 | 149 | char temp[2]; |
Sissors | 0:7bc29a9ea016 | 150 | temp[0]=address; |
Sissors | 0:7bc29a9ea016 | 151 | temp[1]=data; |
Sissors | 0:7bc29a9ea016 | 152 | |
Sissors | 0:7bc29a9ea016 | 153 | _i2c.write(MMA7660_ADDRESS, temp, 2); |
Sissors | 0:7bc29a9ea016 | 154 | } |
Sissors | 0:7bc29a9ea016 | 155 | |
Sissors | 0:7bc29a9ea016 | 156 | char MMA7660::read(char address) |
Sissors | 0:7bc29a9ea016 | 157 | { |
Sissors | 0:7bc29a9ea016 | 158 | char retval; |
Sissors | 0:7bc29a9ea016 | 159 | _i2c.write(MMA7660_ADDRESS, &address, 1, true); |
Sissors | 0:7bc29a9ea016 | 160 | _i2c.read(MMA7660_ADDRESS, &retval, 1); |
Sissors | 0:7bc29a9ea016 | 161 | return retval; |
Sissors | 0:7bc29a9ea016 | 162 | } |
Sissors | 0:7bc29a9ea016 | 163 | |
Sissors | 0:7bc29a9ea016 | 164 | void MMA7660::read(char address, char *data, int length) |
Sissors | 0:7bc29a9ea016 | 165 | { |
Sissors | 0:7bc29a9ea016 | 166 | _i2c.write(MMA7660_ADDRESS, &address, 1, true); |
Sissors | 0:7bc29a9ea016 | 167 | _i2c.read(MMA7660_ADDRESS, data, length); |
Sissors | 0:7bc29a9ea016 | 168 | } |
Sissors | 0:7bc29a9ea016 | 169 | |
Sissors | 0:7bc29a9ea016 | 170 | float MMA7660::getSingle( int number ) |
Sissors | 0:7bc29a9ea016 | 171 | { |
Sissors | 3:89cb08cc663b | 172 | bool active_old = active; |
Sissors | 0:7bc29a9ea016 | 173 | if (!active) { |
Sissors | 0:7bc29a9ea016 | 174 | setActive(true); |
Sissors | 1:8997a1b348dd | 175 | wait(0.012 + 1/samplerate); //Wait until new sample is ready |
Sissors | 0:7bc29a9ea016 | 176 | } |
Sissors | 0:7bc29a9ea016 | 177 | |
Sissors | 0:7bc29a9ea016 | 178 | signed char temp; |
Sissors | 0:7bc29a9ea016 | 179 | bool alert; |
Sissors | 0:7bc29a9ea016 | 180 | |
Sissors | 0:7bc29a9ea016 | 181 | do { |
Sissors | 0:7bc29a9ea016 | 182 | alert = false; |
Sissors | 0:7bc29a9ea016 | 183 | temp = read(MMA7660_XOUT_R + number); |
Sissors | 0:7bc29a9ea016 | 184 | if (temp > 63) |
Sissors | 0:7bc29a9ea016 | 185 | alert = true; |
Sissors | 0:7bc29a9ea016 | 186 | if (temp > 31) |
Sissors | 0:7bc29a9ea016 | 187 | temp += 128+64; |
Sissors | 2:a8e20db7901e | 188 | } while (alert); |
Sissors | 0:7bc29a9ea016 | 189 | |
Sissors | 3:89cb08cc663b | 190 | if (!active_old) |
Sissors | 2:a8e20db7901e | 191 | setActive(false); |
Sissors | 0:7bc29a9ea016 | 192 | |
Sissors | 0:7bc29a9ea016 | 193 | return temp / MMA7660_SENSITIVITY; |
Sissors | 0:7bc29a9ea016 | 194 | } |