31 write(MPU_address,CONFIG, 0x03);
32 write(MPU_address,SMPLRT_DIV, 0x00);
33 write(MPU_address,GYRO_CONFIG, 0x01);
34 write(MPU_address,INT_PIN_CFG, 0x02);
35 write(MAG_address, CNTL, 0x16);
38 mx_sensitivity = (((read(MAG_address, ASAX)-128)*0.5)/128)+1;
39 my_sensitivity = (((read(MAG_address, ASAY)-128)*0.5)/128)+1;
40 mz_sensitivity = (((read(MAG_address, ASAZ)-128)*0.5)/128)+1;
43 GX_offset = ((int16_t)read(MPU_address, XG_OFFSET_H) << 8) | read(MPU_address, XG_OFFSET_L);
44 GY_offset = ((int16_t)read(MPU_address, YG_OFFSET_H) << 8) | read(MPU_address, YG_OFFSET_L);
45 GZ_offset = ((int16_t)read(MPU_address, XG_OFFSET_H) << 8) | read(MPU_address, ZG_OFFSET_L);
51 AX_offset = (read(MPU_address,XA_OFFSET_H)<<8) | (read(MPU_address,XA_OFFSET_L));
52 AY_offset = (read(MPU_address,YA_OFFSET_H)<<8) | (read(MPU_address,YA_OFFSET_L));
53 AZ_offset = (read(MPU_address,ZA_OFFSET_H)<<8) | (read(MPU_address,ZA_OFFSET_L));
56 AX_offset = AX_offset>>1;
57 AY_offset = AY_offset>>1;
58 AZ_offset = AZ_offset>>1;
73 offset = offset + GX_offset;
74 write(MPU_address,XG_OFFSET_L,(offset & 0xFF));
75 write(MPU_address,XG_OFFSET_H,(offset>>8));
79 offset = offset + GY_offset;
80 write(MPU_address,YG_OFFSET_L,(offset & 0xFF));
81 write(MPU_address,YG_OFFSET_H,(offset>>8));
85 offset = offset + GZ_offset;
86 write(MPU_address,ZG_OFFSET_L,(offset & 0xFF));
87 write(MPU_address,ZG_OFFSET_H,(offset>>8));
100 switch(selected_axis)
103 offset = offset + AX_offset;
104 write(MPU_address,XA_OFFSET_L,(offset & 0xFF)<<1);
105 write(MPU_address,XA_OFFSET_H,(offset>>7));
109 offset = offset + AY_offset;
110 write(MPU_address,YA_OFFSET_L,(offset & 0xFF)<<1);
111 write(MPU_address,YA_OFFSET_H,(offset>>7));
115 offset = offset + AZ_offset;
116 write(MPU_address,ZA_OFFSET_L,(offset & 0xFF)<<1);
117 write(MPU_address,ZA_OFFSET_H,(offset>>7));
128 switch(selected_bandwidth)
131 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<3));
136 write_AND(MPU_address,ACCEL_CONFIG_2,~((1<<3)|(1<<2)|(1<<1)|(1<<0)));
140 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
142 write_AND(MPU_address,ACCEL_CONFIG_2,~((1<<1)|(1<<2)));
143 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<0));
147 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
149 write_AND(MPU_address,ACCEL_CONFIG_2,~((1<<0)|(1<<2)));
150 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<1));
154 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
156 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<2));
157 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<0)|(1<<1));
161 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
163 write_AND(MPU_address,ACCEL_CONFIG_2,~((1<<0)|(1<<1)));
164 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<2));
168 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
170 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<1));
171 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<0)|(1<<2));
175 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<3));
177 write_AND(MPU_address,ACCEL_CONFIG_2,~(1<<0));
178 write_OR(MPU_address,ACCEL_CONFIG_2,(1<<1)|(1<<2));
189 switch(selected_bandwidth)
192 write_OR(MPU_address,GYRO_CONFIG,(1<<0));
196 write_AND(MPU_address,GYRO_CONFIG,~(1<<0));
197 write_OR(MPU_address,GYRO_CONFIG,(1<<1));
201 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
203 write_AND(MPU_address,CONFIG,~((1<<0)|(1<<1)|(1<<2)));
207 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
209 write_AND(MPU_address,CONFIG,~((1<<1)|(1<<2)));
210 write_OR(MPU_address,CONFIG,(1<<0));
214 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
216 write_AND(MPU_address,CONFIG,~((1<<2)|(1<<0)));
217 write_OR(MPU_address,CONFIG,(1<<1));
221 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
223 write_AND(MPU_address,CONFIG,~(1<<2));
224 write_OR(MPU_address,CONFIG,(1<<0)|(1<<1));
228 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
230 write_AND(MPU_address,CONFIG,~((1<<1)|(1<<0)));
231 write_OR(MPU_address,CONFIG,(1<<2));
235 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
237 write_AND(MPU_address,CONFIG,~(1<<1));
238 write_OR(MPU_address,CONFIG,(1<<2)|(1<<0));
242 write_AND(MPU_address,GYRO_CONFIG,~((1<<0)|(1<<1)));
244 write_AND(MPU_address,CONFIG,~(1<<0));
245 write_OR(MPU_address,CONFIG,(1<<1)|(1<<2));
258 uint8_t MPU9255::getScale(uint8_t current_state, scales selected_scale)
260 if(selected_scale == scale_2g || selected_scale == scale_250dps)
262 current_state &= ~((1<<3)|(1<<4));
265 if(selected_scale == scale_4g || selected_scale == scale_500dps)
267 current_state &= ~(1<<4);
268 current_state |= (1<<3);
271 if(selected_scale == scale_8g || selected_scale == scale_1000dps)
273 current_state &= ~(1<<3);
274 current_state |= (1<<4);
277 if(selected_scale == scale_16g || selected_scale == scale_2000dps)
279 current_state |= (1<<4)|(1<<3);
282 return current_state;
291 uint8_t val = read(MPU_address,ACCEL_CONFIG);
292 val = getScale(val,selected_scale);
293 write(MPU_address,ACCEL_CONFIG,val);
302 uint8_t val=read(MPU_address,GYRO_CONFIG);
303 val = getScale(val,selected_scale);
304 write(MPU_address,GYRO_CONFIG,val);
313 if(read(MPU_address,WHO_AM_I)==0xFF)
326 if(read(MAG_address,MAG_ID)==0xFF)
void set_acc_scale(scales selected_scale)
Set accelerometer scale.
void set_gyro_bandwidth(bandwidth selected_bandwidth)
Set gyroscope bandwidth.
uint8_t testMag()
Test if magnetometer is working.
uint8_t init()
Initialise MPU9255 module.
void set_acc_bandwidth(bandwidth selected_bandwidth)
Set accelerometer bandwidth.
void set_acc_offset(axis selected_axis, int16_t offset)
Set accelerometer offset.
Main header of the library.
void set_gyro_scale(scales selected_scale)
Set gyroscope scale.
uint8_t testIMU()
Test if IMU (gyroscope and accelerometer) is working.
void set_gyro_offset(axis selected_axis, int16_t offset)
Set gyroscope offset.