Skip to content

Commit 92f6d45

Browse files
committed
Updated examples
1 parent 4fbe125 commit 92f6d45

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

examples/MPU9250BasicAHRS_I2C/MPU9250BasicAHRS_I2C.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);
4949
int intPin = 12; // These can be changed, 2 and 3 are the Arduinos ext int pins
5050
int myLed = 13; // Set up pin 13 led for toggling
5151

52-
#define I2C_clock 400000
53-
#define I2C_port Wire
52+
#define I2Cclock 400000
53+
#define I2Cport Wire
5454
#define MPU9250_ADDRESS MPU9250_ADDRESS_AD0 // Use either this line or the next to select which I2C address your device is using
5555
//#define MPU9250_ADDRESS MPU9250_ADDRESS_AD1
5656

57-
MPU9250 myIMU(MPU9250_ADDRESS, I2C_port, I2C_clock);
57+
MPU9250 myIMU(MPU9250_ADDRESS, I2Cport, I2Cclock);
5858

5959
void setup()
6060
{

examples/MPU9250BasicAHRS_SPI/MPU9250BasicAHRS_SPI.ino

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);
4242
#define AHRS true // Set to false for basic data read
4343
#define SERIAL_DEBUG true // Set to true to get Serial output for debugging
4444

45-
MPU9250 myIMU = MPU9250(2); // Using digital pin to for chip select in demo
45+
#define SPIport SPI
46+
#define CSpin 2
47+
#define SPIrate 1000000
48+
MPU9250 myIMU = MPU9250(CSpin, SPIport, SPIrate); // Using digital pin to for chip select in demo
49+
50+
51+
// Because of the way this library is written you still need to send SOME address -
52+
// but in SPI mode it is not used, so I chose over 9000 - By the way if you look closely
53+
// there will be a truncation warning during compilation because 9001 is too large for
54+
// the parameter's type.
55+
#define MPU9250_ADDRESS 9001
4656

4757
void setup()
4858
{

src/MPU9250.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//====== and temperature data
66
//==============================================================================
77

8-
MPU9250::MPU9250( int8_t csPin, SPIClass &spiInterface )
8+
MPU9250::MPU9250( int8_t csPin, SPIClass &spiInterface, uint32_t spi_freq )
99
{
1010
// Use hardware SPI communication
1111
// If used with sparkfun breakout board
@@ -17,6 +17,8 @@ MPU9250::MPU9250( int8_t csPin, SPIClass &spiInterface )
1717
_spi = &spiInterface;
1818
_wire = NULL;
1919

20+
_interfaceSpeed = spi_freq;
21+
2022
_spi->begin();
2123
pinMode(_csPin, OUTPUT);
2224
deselect();
@@ -27,10 +29,12 @@ MPU9250::MPU9250( uint8_t address, TwoWire &wirePort, uint32_t clock_frequency )
2729
_wire = &wirePort;
2830
_spi = NULL;
2931

32+
_interfaceSpeed = clock_frequency;
33+
3034
_csPin = NOT_SPI;// Used to tell the library that the sensor is using I2C
3135

3236
_wire->begin();
33-
_wire->setClock(clock_frequency);
37+
_wire->setClock(_interfaceSpeed);
3438
}
3539

3640
void MPU9250::getMres()
@@ -612,8 +616,8 @@ void MPU9250::magCalMPU9250(float * bias_dest, float * scale_dest)
612616
uint16_t ii = 0, sample_count = 0;
613617
int32_t mag_bias[3] = {0, 0, 0},
614618
mag_scale[3] = {0, 0, 0};
615-
int16_t mag_max[3] = {0x8000, 0x8000, 0x8000},
616-
mag_min[3] = {0x7FFF, 0x7FFF, 0x7FFF},
619+
int16_t mag_max[3] = {-32768, -32768, -32768},// Wrote out decimal (signed) values to remove a conversion warning
620+
mag_min[3] = {32767, 32767, 32767},
617621
mag_temp[3] = {0, 0, 0};
618622

619623
// Make sure resolution has been calculated
@@ -715,7 +719,7 @@ uint8_t MPU9250::writeByteSPI(uint8_t registerAddress, uint8_t writeData)
715719
{
716720
uint8_t returnVal;
717721

718-
_spi->beginTransaction(SPISettings(SPI_DATA_RATE, MSBFIRST, SPI_MODE));
722+
_spi->beginTransaction(SPISettings(_interfaceSpeed, MSBFIRST, SPI_MODE));
719723
select();
720724

721725
_spi->transfer(registerAddress);
@@ -733,12 +737,14 @@ uint8_t MPU9250::writeByteSPI(uint8_t registerAddress, uint8_t writeData)
733737
uint8_t MPU9250::writeByteWire(uint8_t deviceAddress, uint8_t registerAddress,
734738
uint8_t data)
735739
{
736-
_wire->beginTransmission(deviceAddress); // Initialize the Tx buffer
737-
_wire->write(registerAddress); // Put slave register address in Tx buffer
738-
_wire->write(data); // Put data in Tx buffer
739-
_wire->endTransmission(); // Send the Tx buffer
740-
// TODO: Fix this to return something meaningful
741-
return NULL;
740+
_wire->setClock(_interfaceSpeed);// Reset to the desired speed, in case other devices required a slowdown
741+
_wire->beginTransmission(deviceAddress); // Initialize the Tx buffer
742+
_wire->write(registerAddress); // Put slave register address in Tx buffer
743+
_wire->write(data); // Put data in Tx buffer
744+
_wire->endTransmission(); // Send the Tx buffer
745+
// TODO: Fix this to return something meaningful
746+
// return NULL; // In the meantime fix it to return the right type
747+
return 0;
742748
}
743749

744750
// Read a byte from given register on device. Calls necessary SPI or I2C

src/MPU9250.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@
180180
#define READ_FLAG 0x80
181181
#define NOT_SPI -1
182182
#define SPI_DATA_RATE 1000000 // 1MHz is the max speed of the MPU-9250
183-
//#define SPI_DATA_RATE 1000000 // 1MHz is the max speed of the MPU-9250
184183
#define SPI_MODE SPI_MODE3
185184

186185
class MPU9250
@@ -212,9 +211,14 @@ class MPU9250
212211
M_100HZ = 0x06 // 100 Hz continuous magnetometer
213212
};
214213

215-
uint8_t _I2Caddr = MPU9250_ADDRESS_AD0;// Use AD0 by default
216-
SPIClass * _spi;// Allows for use of different SPI ports
214+
217215
TwoWire * _wire;// Allows for use of various I2C ports
216+
uint8_t _I2Caddr = MPU9250_ADDRESS_AD0;// Use AD0 by default
217+
218+
SPIClass * _spi;// Allows for use of different SPI ports
219+
int8_t _csPin; // SPI chip select pin
220+
221+
uint32_t _interfaceSpeed;// Stores the desired I2C or SPi clock rate
218222

219223
// TODO: Add setter methods for this hard coded stuff
220224
// Specify sensor full scale
@@ -226,8 +230,8 @@ class MPU9250
226230
// 2 for 8 Hz, 6 for 100 Hz continuous magnetometer data read
227231
uint8_t Mmode = M_8HZ;
228232

229-
// SPI chip select pin
230-
int8_t _csPin;
233+
234+
231235

232236
uint8_t writeByteWire(uint8_t, uint8_t, uint8_t);
233237
uint8_t writeByteSPI(uint8_t, uint8_t);
@@ -270,7 +274,7 @@ class MPU9250
270274
int16_t accelCount[3];
271275

272276
// Public method declarations
273-
MPU9250( int8_t _csPin, SPIClass &spiInterface = SPI);
277+
MPU9250( int8_t _csPin, SPIClass &spiInterface = SPI, uint32_t spi_freq = SPI_DATA_RATE);
274278
MPU9250( uint8_t address = MPU9250_ADDRESS_AD0, TwoWire &wirePort = Wire, uint32_t clock_frequency = 100000 );
275279
void getMres();
276280
void getGres();

0 commit comments

Comments
 (0)