MPU6000 library to get accelerometer, gyroscope and temperature readings via SPI.
MPU6000.cpp@0:2edbd561b520, 2014-03-08 (annotated)
- Committer:
- brunoalfano
- Date:
- Sat Mar 08 12:38:14 2014 +0000
- Revision:
- 0:2edbd561b520
First version of a simple MPU6000 SPI library to read sensors' data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunoalfano | 0:2edbd561b520 | 1 | /*CODED by Bruno Alfano on 07/03/2014 |
brunoalfano | 0:2edbd561b520 | 2 | www.xene.it |
brunoalfano | 0:2edbd561b520 | 3 | */ |
brunoalfano | 0:2edbd561b520 | 4 | |
brunoalfano | 0:2edbd561b520 | 5 | #include <mbed.h> |
brunoalfano | 0:2edbd561b520 | 6 | #include "MPU6000.h" |
brunoalfano | 0:2edbd561b520 | 7 | |
brunoalfano | 0:2edbd561b520 | 8 | mpu6000_spi::mpu6000_spi(SPI& _spi, PinName _cs) : spi(_spi), cs(_cs) {} |
brunoalfano | 0:2edbd561b520 | 9 | |
brunoalfano | 0:2edbd561b520 | 10 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 11 | INITIALIZATION |
brunoalfano | 0:2edbd561b520 | 12 | usage: call this function at startup, giving the sample rate divider (raging from 0 to 255) and |
brunoalfano | 0:2edbd561b520 | 13 | low pass filter value; suitable values are: |
brunoalfano | 0:2edbd561b520 | 14 | BITS_DLPF_CFG_256HZ_NOLPF2 |
brunoalfano | 0:2edbd561b520 | 15 | BITS_DLPF_CFG_188HZ |
brunoalfano | 0:2edbd561b520 | 16 | BITS_DLPF_CFG_98HZ |
brunoalfano | 0:2edbd561b520 | 17 | BITS_DLPF_CFG_42HZ |
brunoalfano | 0:2edbd561b520 | 18 | BITS_DLPF_CFG_20HZ |
brunoalfano | 0:2edbd561b520 | 19 | BITS_DLPF_CFG_10HZ |
brunoalfano | 0:2edbd561b520 | 20 | BITS_DLPF_CFG_5HZ |
brunoalfano | 0:2edbd561b520 | 21 | BITS_DLPF_CFG_2100HZ_NOLPF |
brunoalfano | 0:2edbd561b520 | 22 | returns 1 if an error occurred |
brunoalfano | 0:2edbd561b520 | 23 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 24 | bool mpu6000_spi::init(int sample_rate_div,int low_pass_filter){ |
brunoalfano | 0:2edbd561b520 | 25 | unsigned int response; |
brunoalfano | 0:2edbd561b520 | 26 | spi.format(8,0); |
brunoalfano | 0:2edbd561b520 | 27 | spi.frequency(1000000); |
brunoalfano | 0:2edbd561b520 | 28 | //FIRST OF ALL DISABLE I2C |
brunoalfano | 0:2edbd561b520 | 29 | select(); |
brunoalfano | 0:2edbd561b520 | 30 | response=spi.write(MPUREG_USER_CTRL); |
brunoalfano | 0:2edbd561b520 | 31 | response=spi.write(BIT_I2C_IF_DIS); |
brunoalfano | 0:2edbd561b520 | 32 | deselect(); |
brunoalfano | 0:2edbd561b520 | 33 | //RESET CHIP |
brunoalfano | 0:2edbd561b520 | 34 | select(); |
brunoalfano | 0:2edbd561b520 | 35 | response=spi.write(MPUREG_PWR_MGMT_1); |
brunoalfano | 0:2edbd561b520 | 36 | response=spi.write(BIT_H_RESET); |
brunoalfano | 0:2edbd561b520 | 37 | deselect(); |
brunoalfano | 0:2edbd561b520 | 38 | wait(0.15); |
brunoalfano | 0:2edbd561b520 | 39 | //WAKE UP AND SET GYROZ CLOCK |
brunoalfano | 0:2edbd561b520 | 40 | select(); |
brunoalfano | 0:2edbd561b520 | 41 | response=spi.write(MPUREG_PWR_MGMT_1); |
brunoalfano | 0:2edbd561b520 | 42 | response=spi.write(MPU_CLK_SEL_PLLGYROZ); |
brunoalfano | 0:2edbd561b520 | 43 | deselect(); |
brunoalfano | 0:2edbd561b520 | 44 | //DISABLE I2C |
brunoalfano | 0:2edbd561b520 | 45 | select(); |
brunoalfano | 0:2edbd561b520 | 46 | response=spi.write(MPUREG_USER_CTRL); |
brunoalfano | 0:2edbd561b520 | 47 | response=spi.write(BIT_I2C_IF_DIS); |
brunoalfano | 0:2edbd561b520 | 48 | deselect(); |
brunoalfano | 0:2edbd561b520 | 49 | //WHO AM I? |
brunoalfano | 0:2edbd561b520 | 50 | select(); |
brunoalfano | 0:2edbd561b520 | 51 | response=spi.write(MPUREG_WHOAMI|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 52 | response=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 53 | deselect(); |
brunoalfano | 0:2edbd561b520 | 54 | if(response<100){return 0;}//COULDN'T RECEIVE WHOAMI |
brunoalfano | 0:2edbd561b520 | 55 | //SET SAMPLE RATE |
brunoalfano | 0:2edbd561b520 | 56 | select(); |
brunoalfano | 0:2edbd561b520 | 57 | response=spi.write(MPUREG_SMPLRT_DIV); |
brunoalfano | 0:2edbd561b520 | 58 | response=spi.write(sample_rate_div); |
brunoalfano | 0:2edbd561b520 | 59 | deselect(); |
brunoalfano | 0:2edbd561b520 | 60 | // FS & DLPF |
brunoalfano | 0:2edbd561b520 | 61 | select(); |
brunoalfano | 0:2edbd561b520 | 62 | response=spi.write(MPUREG_CONFIG); |
brunoalfano | 0:2edbd561b520 | 63 | response=spi.write(low_pass_filter); |
brunoalfano | 0:2edbd561b520 | 64 | deselect(); |
brunoalfano | 0:2edbd561b520 | 65 | //DISABLE INTERRUPTS |
brunoalfano | 0:2edbd561b520 | 66 | select(); |
brunoalfano | 0:2edbd561b520 | 67 | response=spi.write(MPUREG_INT_ENABLE); |
brunoalfano | 0:2edbd561b520 | 68 | response=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 69 | deselect(); |
brunoalfano | 0:2edbd561b520 | 70 | return 0; |
brunoalfano | 0:2edbd561b520 | 71 | } |
brunoalfano | 0:2edbd561b520 | 72 | |
brunoalfano | 0:2edbd561b520 | 73 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 74 | ACCELEROMETER SCALE |
brunoalfano | 0:2edbd561b520 | 75 | usage: call this function at startup, after initialization, to set the right range for the |
brunoalfano | 0:2edbd561b520 | 76 | accelerometers. Suitable ranges are: |
brunoalfano | 0:2edbd561b520 | 77 | BITS_FS_2G |
brunoalfano | 0:2edbd561b520 | 78 | BITS_FS_4G |
brunoalfano | 0:2edbd561b520 | 79 | BITS_FS_8G |
brunoalfano | 0:2edbd561b520 | 80 | BITS_FS_16G |
brunoalfano | 0:2edbd561b520 | 81 | returns the range set (2,4,8 or 16) |
brunoalfano | 0:2edbd561b520 | 82 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 83 | unsigned int mpu6000_spi::set_acc_scale(int scale){ |
brunoalfano | 0:2edbd561b520 | 84 | unsigned int temp_scale; |
brunoalfano | 0:2edbd561b520 | 85 | select(); |
brunoalfano | 0:2edbd561b520 | 86 | spi.write(MPUREG_ACCEL_CONFIG); |
brunoalfano | 0:2edbd561b520 | 87 | spi.write(scale); |
brunoalfano | 0:2edbd561b520 | 88 | deselect(); |
brunoalfano | 0:2edbd561b520 | 89 | switch (scale){ |
brunoalfano | 0:2edbd561b520 | 90 | case BITS_FS_2G: |
brunoalfano | 0:2edbd561b520 | 91 | acc_divider=16384; |
brunoalfano | 0:2edbd561b520 | 92 | break; |
brunoalfano | 0:2edbd561b520 | 93 | case BITS_FS_4G: |
brunoalfano | 0:2edbd561b520 | 94 | acc_divider=8192; |
brunoalfano | 0:2edbd561b520 | 95 | break; |
brunoalfano | 0:2edbd561b520 | 96 | case BITS_FS_8G: |
brunoalfano | 0:2edbd561b520 | 97 | acc_divider=4096; |
brunoalfano | 0:2edbd561b520 | 98 | break; |
brunoalfano | 0:2edbd561b520 | 99 | case BITS_FS_16G: |
brunoalfano | 0:2edbd561b520 | 100 | acc_divider=2048; |
brunoalfano | 0:2edbd561b520 | 101 | break; |
brunoalfano | 0:2edbd561b520 | 102 | } |
brunoalfano | 0:2edbd561b520 | 103 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 104 | select(); |
brunoalfano | 0:2edbd561b520 | 105 | temp_scale=spi.write(MPUREG_ACCEL_CONFIG|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 106 | temp_scale=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 107 | deselect(); |
brunoalfano | 0:2edbd561b520 | 108 | switch (temp_scale){ |
brunoalfano | 0:2edbd561b520 | 109 | case BITS_FS_2G: |
brunoalfano | 0:2edbd561b520 | 110 | temp_scale=2; |
brunoalfano | 0:2edbd561b520 | 111 | break; |
brunoalfano | 0:2edbd561b520 | 112 | case BITS_FS_4G: |
brunoalfano | 0:2edbd561b520 | 113 | temp_scale=4; |
brunoalfano | 0:2edbd561b520 | 114 | break; |
brunoalfano | 0:2edbd561b520 | 115 | case BITS_FS_8G: |
brunoalfano | 0:2edbd561b520 | 116 | temp_scale=8; |
brunoalfano | 0:2edbd561b520 | 117 | break; |
brunoalfano | 0:2edbd561b520 | 118 | case BITS_FS_16G: |
brunoalfano | 0:2edbd561b520 | 119 | temp_scale=16; |
brunoalfano | 0:2edbd561b520 | 120 | break; |
brunoalfano | 0:2edbd561b520 | 121 | } |
brunoalfano | 0:2edbd561b520 | 122 | return temp_scale; |
brunoalfano | 0:2edbd561b520 | 123 | } |
brunoalfano | 0:2edbd561b520 | 124 | |
brunoalfano | 0:2edbd561b520 | 125 | |
brunoalfano | 0:2edbd561b520 | 126 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 127 | GYROSCOPE SCALE |
brunoalfano | 0:2edbd561b520 | 128 | usage: call this function at startup, after initialization, to set the right range for the |
brunoalfano | 0:2edbd561b520 | 129 | gyroscopes. Suitable ranges are: |
brunoalfano | 0:2edbd561b520 | 130 | BITS_FS_250DPS |
brunoalfano | 0:2edbd561b520 | 131 | BITS_FS_500DPS |
brunoalfano | 0:2edbd561b520 | 132 | BITS_FS_1000DPS |
brunoalfano | 0:2edbd561b520 | 133 | BITS_FS_2000DPS |
brunoalfano | 0:2edbd561b520 | 134 | returns the range set (250,500,1000 or 2000) |
brunoalfano | 0:2edbd561b520 | 135 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 136 | unsigned int mpu6000_spi::set_gyro_scale(int scale){ |
brunoalfano | 0:2edbd561b520 | 137 | unsigned int temp_scale; |
brunoalfano | 0:2edbd561b520 | 138 | select(); |
brunoalfano | 0:2edbd561b520 | 139 | spi.write(MPUREG_GYRO_CONFIG); |
brunoalfano | 0:2edbd561b520 | 140 | spi.write(scale); |
brunoalfano | 0:2edbd561b520 | 141 | deselect(); |
brunoalfano | 0:2edbd561b520 | 142 | switch (scale){ |
brunoalfano | 0:2edbd561b520 | 143 | case BITS_FS_250DPS: |
brunoalfano | 0:2edbd561b520 | 144 | gyro_divider=131; |
brunoalfano | 0:2edbd561b520 | 145 | break; |
brunoalfano | 0:2edbd561b520 | 146 | case BITS_FS_500DPS: |
brunoalfano | 0:2edbd561b520 | 147 | gyro_divider=65.5; |
brunoalfano | 0:2edbd561b520 | 148 | break; |
brunoalfano | 0:2edbd561b520 | 149 | case BITS_FS_1000DPS: |
brunoalfano | 0:2edbd561b520 | 150 | gyro_divider=32.8; |
brunoalfano | 0:2edbd561b520 | 151 | break; |
brunoalfano | 0:2edbd561b520 | 152 | case BITS_FS_2000DPS: |
brunoalfano | 0:2edbd561b520 | 153 | gyro_divider=16.4; |
brunoalfano | 0:2edbd561b520 | 154 | break; |
brunoalfano | 0:2edbd561b520 | 155 | } |
brunoalfano | 0:2edbd561b520 | 156 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 157 | select(); |
brunoalfano | 0:2edbd561b520 | 158 | temp_scale=spi.write(MPUREG_GYRO_CONFIG|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 159 | temp_scale=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 160 | deselect(); |
brunoalfano | 0:2edbd561b520 | 161 | switch (temp_scale){ |
brunoalfano | 0:2edbd561b520 | 162 | case BITS_FS_250DPS: |
brunoalfano | 0:2edbd561b520 | 163 | temp_scale=250; |
brunoalfano | 0:2edbd561b520 | 164 | break; |
brunoalfano | 0:2edbd561b520 | 165 | case BITS_FS_500DPS: |
brunoalfano | 0:2edbd561b520 | 166 | temp_scale=500; |
brunoalfano | 0:2edbd561b520 | 167 | break; |
brunoalfano | 0:2edbd561b520 | 168 | case BITS_FS_1000DPS: |
brunoalfano | 0:2edbd561b520 | 169 | temp_scale=1000; |
brunoalfano | 0:2edbd561b520 | 170 | break; |
brunoalfano | 0:2edbd561b520 | 171 | case BITS_FS_2000DPS: |
brunoalfano | 0:2edbd561b520 | 172 | temp_scale=2000; |
brunoalfano | 0:2edbd561b520 | 173 | break; |
brunoalfano | 0:2edbd561b520 | 174 | } |
brunoalfano | 0:2edbd561b520 | 175 | return temp_scale; |
brunoalfano | 0:2edbd561b520 | 176 | } |
brunoalfano | 0:2edbd561b520 | 177 | |
brunoalfano | 0:2edbd561b520 | 178 | |
brunoalfano | 0:2edbd561b520 | 179 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 180 | WHO AM I? |
brunoalfano | 0:2edbd561b520 | 181 | usage: call this function to know if SPI is working correctly. It checks the I2C address of the |
brunoalfano | 0:2edbd561b520 | 182 | mpu6000 which should be 104 when in SPI mode. |
brunoalfano | 0:2edbd561b520 | 183 | returns the I2C address (104) |
brunoalfano | 0:2edbd561b520 | 184 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 185 | unsigned int mpu6000_spi::whoami(){ |
brunoalfano | 0:2edbd561b520 | 186 | unsigned int response; |
brunoalfano | 0:2edbd561b520 | 187 | select(); |
brunoalfano | 0:2edbd561b520 | 188 | response=spi.write(MPUREG_WHOAMI|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 189 | response=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 190 | deselect(); |
brunoalfano | 0:2edbd561b520 | 191 | return response; |
brunoalfano | 0:2edbd561b520 | 192 | } |
brunoalfano | 0:2edbd561b520 | 193 | |
brunoalfano | 0:2edbd561b520 | 194 | |
brunoalfano | 0:2edbd561b520 | 195 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 196 | READ ACCELEROMETER |
brunoalfano | 0:2edbd561b520 | 197 | usage: call this function to read accelerometer data. Axis represents selected axis: |
brunoalfano | 0:2edbd561b520 | 198 | 0 -> X axis |
brunoalfano | 0:2edbd561b520 | 199 | 1 -> Y axis |
brunoalfano | 0:2edbd561b520 | 200 | 2 -> Z axis |
brunoalfano | 0:2edbd561b520 | 201 | returns the value in Gs |
brunoalfano | 0:2edbd561b520 | 202 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 203 | float mpu6000_spi::read_acc(int axis){ |
brunoalfano | 0:2edbd561b520 | 204 | uint8_t responseH,responseL; |
brunoalfano | 0:2edbd561b520 | 205 | int16_t bit_data; |
brunoalfano | 0:2edbd561b520 | 206 | float data; |
brunoalfano | 0:2edbd561b520 | 207 | select(); |
brunoalfano | 0:2edbd561b520 | 208 | switch (axis){ |
brunoalfano | 0:2edbd561b520 | 209 | case 0: |
brunoalfano | 0:2edbd561b520 | 210 | responseH=spi.write(MPUREG_ACCEL_XOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 211 | break; |
brunoalfano | 0:2edbd561b520 | 212 | case 1: |
brunoalfano | 0:2edbd561b520 | 213 | responseH=spi.write(MPUREG_ACCEL_YOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 214 | break; |
brunoalfano | 0:2edbd561b520 | 215 | case 2: |
brunoalfano | 0:2edbd561b520 | 216 | responseH=spi.write(MPUREG_ACCEL_ZOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 217 | break; |
brunoalfano | 0:2edbd561b520 | 218 | } |
brunoalfano | 0:2edbd561b520 | 219 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 220 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 221 | bit_data=((int16_t)responseH<<8)|responseL; |
brunoalfano | 0:2edbd561b520 | 222 | data=(float)bit_data; |
brunoalfano | 0:2edbd561b520 | 223 | data=data/acc_divider; |
brunoalfano | 0:2edbd561b520 | 224 | deselect(); |
brunoalfano | 0:2edbd561b520 | 225 | return data; |
brunoalfano | 0:2edbd561b520 | 226 | } |
brunoalfano | 0:2edbd561b520 | 227 | |
brunoalfano | 0:2edbd561b520 | 228 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 229 | READ GYROSCOPE |
brunoalfano | 0:2edbd561b520 | 230 | usage: call this function to read gyroscope data. Axis represents selected axis: |
brunoalfano | 0:2edbd561b520 | 231 | 0 -> X axis |
brunoalfano | 0:2edbd561b520 | 232 | 1 -> Y axis |
brunoalfano | 0:2edbd561b520 | 233 | 2 -> Z axis |
brunoalfano | 0:2edbd561b520 | 234 | returns the value in Degrees per second |
brunoalfano | 0:2edbd561b520 | 235 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 236 | float mpu6000_spi::read_rot(int axis){ |
brunoalfano | 0:2edbd561b520 | 237 | uint8_t responseH,responseL; |
brunoalfano | 0:2edbd561b520 | 238 | int16_t bit_data; |
brunoalfano | 0:2edbd561b520 | 239 | float data; |
brunoalfano | 0:2edbd561b520 | 240 | select(); |
brunoalfano | 0:2edbd561b520 | 241 | switch (axis){ |
brunoalfano | 0:2edbd561b520 | 242 | case 0: |
brunoalfano | 0:2edbd561b520 | 243 | responseH=spi.write(MPUREG_GYRO_XOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 244 | break; |
brunoalfano | 0:2edbd561b520 | 245 | case 1: |
brunoalfano | 0:2edbd561b520 | 246 | responseH=spi.write(MPUREG_GYRO_YOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 247 | break; |
brunoalfano | 0:2edbd561b520 | 248 | case 2: |
brunoalfano | 0:2edbd561b520 | 249 | responseH=spi.write(MPUREG_GYRO_ZOUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 250 | break; |
brunoalfano | 0:2edbd561b520 | 251 | } |
brunoalfano | 0:2edbd561b520 | 252 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 253 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 254 | bit_data=((int16_t)responseH<<8)|responseL; |
brunoalfano | 0:2edbd561b520 | 255 | data=(float)bit_data; |
brunoalfano | 0:2edbd561b520 | 256 | data=data/gyro_divider; |
brunoalfano | 0:2edbd561b520 | 257 | deselect(); |
brunoalfano | 0:2edbd561b520 | 258 | return data; |
brunoalfano | 0:2edbd561b520 | 259 | } |
brunoalfano | 0:2edbd561b520 | 260 | |
brunoalfano | 0:2edbd561b520 | 261 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 262 | READ TEMPERATURE |
brunoalfano | 0:2edbd561b520 | 263 | usage: call this function to read temperature data. |
brunoalfano | 0:2edbd561b520 | 264 | returns the value in °C |
brunoalfano | 0:2edbd561b520 | 265 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 266 | float mpu6000_spi::read_temp(){ |
brunoalfano | 0:2edbd561b520 | 267 | uint8_t responseH,responseL; |
brunoalfano | 0:2edbd561b520 | 268 | int16_t bit_data; |
brunoalfano | 0:2edbd561b520 | 269 | float data; |
brunoalfano | 0:2edbd561b520 | 270 | select(); |
brunoalfano | 0:2edbd561b520 | 271 | responseH=spi.write(MPUREG_TEMP_OUT_H | READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 272 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 273 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 274 | bit_data=((int16_t)responseH<<8)|responseL; |
brunoalfano | 0:2edbd561b520 | 275 | data=(float)bit_data; |
brunoalfano | 0:2edbd561b520 | 276 | data=(data/340)+36.53; |
brunoalfano | 0:2edbd561b520 | 277 | deselect(); |
brunoalfano | 0:2edbd561b520 | 278 | return data; |
brunoalfano | 0:2edbd561b520 | 279 | } |
brunoalfano | 0:2edbd561b520 | 280 | |
brunoalfano | 0:2edbd561b520 | 281 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 282 | READ ACCELEROMETER CALIBRATION |
brunoalfano | 0:2edbd561b520 | 283 | usage: call this function to read accelerometer data. Axis represents selected axis: |
brunoalfano | 0:2edbd561b520 | 284 | 0 -> X axis |
brunoalfano | 0:2edbd561b520 | 285 | 1 -> Y axis |
brunoalfano | 0:2edbd561b520 | 286 | 2 -> Z axis |
brunoalfano | 0:2edbd561b520 | 287 | returns Factory Trim value |
brunoalfano | 0:2edbd561b520 | 288 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 289 | int mpu6000_spi::calib_acc(int axis){ |
brunoalfano | 0:2edbd561b520 | 290 | uint8_t responseH,responseL,calib_data; |
brunoalfano | 0:2edbd561b520 | 291 | int temp_scale; |
brunoalfano | 0:2edbd561b520 | 292 | //READ CURRENT ACC SCALE |
brunoalfano | 0:2edbd561b520 | 293 | select(); |
brunoalfano | 0:2edbd561b520 | 294 | responseH=spi.write(MPUREG_ACCEL_CONFIG|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 295 | temp_scale=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 296 | deselect(); |
brunoalfano | 0:2edbd561b520 | 297 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 298 | set_acc_scale(BITS_FS_8G); |
brunoalfano | 0:2edbd561b520 | 299 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 300 | //ENABLE SELF TEST |
brunoalfano | 0:2edbd561b520 | 301 | select(); |
brunoalfano | 0:2edbd561b520 | 302 | responseH=spi.write(MPUREG_ACCEL_CONFIG); |
brunoalfano | 0:2edbd561b520 | 303 | temp_scale=spi.write(0x80>>axis); |
brunoalfano | 0:2edbd561b520 | 304 | deselect(); |
brunoalfano | 0:2edbd561b520 | 305 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 306 | select(); |
brunoalfano | 0:2edbd561b520 | 307 | responseH=spi.write(MPUREG_SELF_TEST_X|READ_FLAG); |
brunoalfano | 0:2edbd561b520 | 308 | switch(axis){ |
brunoalfano | 0:2edbd561b520 | 309 | case 0: |
brunoalfano | 0:2edbd561b520 | 310 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 311 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 312 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 313 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 314 | calib_data=((responseH&11100000)>>3)|((responseL&00110000)>>4); |
brunoalfano | 0:2edbd561b520 | 315 | break; |
brunoalfano | 0:2edbd561b520 | 316 | case 1: |
brunoalfano | 0:2edbd561b520 | 317 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 318 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 319 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 320 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 321 | calib_data=((responseH&11100000)>>3)|((responseL&00001100)>>2); |
brunoalfano | 0:2edbd561b520 | 322 | break; |
brunoalfano | 0:2edbd561b520 | 323 | case 2: |
brunoalfano | 0:2edbd561b520 | 324 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 325 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 326 | responseH=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 327 | responseL=spi.write(0x00); |
brunoalfano | 0:2edbd561b520 | 328 | calib_data=((responseH&11100000)>>3)|((responseL&00000011)); |
brunoalfano | 0:2edbd561b520 | 329 | break; |
brunoalfano | 0:2edbd561b520 | 330 | } |
brunoalfano | 0:2edbd561b520 | 331 | deselect(); |
brunoalfano | 0:2edbd561b520 | 332 | wait(0.01); |
brunoalfano | 0:2edbd561b520 | 333 | set_acc_scale(temp_scale); |
brunoalfano | 0:2edbd561b520 | 334 | return calib_data; |
brunoalfano | 0:2edbd561b520 | 335 | } |
brunoalfano | 0:2edbd561b520 | 336 | |
brunoalfano | 0:2edbd561b520 | 337 | /*----------------------------------------------------------------------------------------------- |
brunoalfano | 0:2edbd561b520 | 338 | SPI SELECT AND DESELECT |
brunoalfano | 0:2edbd561b520 | 339 | usage: enable and disable mpu6000 communication bus |
brunoalfano | 0:2edbd561b520 | 340 | -----------------------------------------------------------------------------------------------*/ |
brunoalfano | 0:2edbd561b520 | 341 | void mpu6000_spi::select() { |
brunoalfano | 0:2edbd561b520 | 342 | //Set CS low to start transmission (interrupts conversion) |
brunoalfano | 0:2edbd561b520 | 343 | cs = 0; |
brunoalfano | 0:2edbd561b520 | 344 | } |
brunoalfano | 0:2edbd561b520 | 345 | void mpu6000_spi::deselect() { |
brunoalfano | 0:2edbd561b520 | 346 | //Set CS high to stop transmission (restarts conversion) |
brunoalfano | 0:2edbd561b520 | 347 | cs = 1; |
brunoalfano | 0:2edbd561b520 | 348 | } |