MPU6500

From OpenMBD
Jump to: navigation, search


General description

The MPU6500 is a 3 axis gyro and 3 axis accelerometer making it a 6DOF sensor. It has a programmable scale for both the gyroscope and the accelerometer and can measure angular rates up to 2000°/sec and accelerations up to 16G. The sensor can be configured and read using the SPI bus or I2C.

Connecting the MPU6500

First the MPU6500 must be soldered on the prototype PCB. It is wise to solder some female headers onto the PCB and male headers onto the MPU6500 so it can be replaced when broken. Make sure it is mounted rigidly to make sure the sensor won't measure movements of the sensor relative to the board due to vibrations. Next the pins must be connected to the E407 pins, see the connection diagram below.

File:ConnectionDiagram.png

Where to buy

SPI control in Simulink

As mentioned before the communication with the sensor is done via SPI. First the SPI Master Init block is used to initialize the SPI system. Channel 1 is chosen with a communication speed of 656.25kHz because the datasheet states that the maximum SPI clock frequency is 1Mhz. The polarity is chosen to be 'Idle low' and phase 'Sample bit on 1st clock edge', these settings also come from the datasheet of the MPU6500.
MPU6500 SPIsettings.png

Configuring the MPU6500

The MPU6500 needs to be configured, because the chip needs some time to process the configurations some delay has to be added between sending the bytes. This is done with the use of a free running counter. The counter increases every 10ms so by checking the output of the counter a delay can be generated. See the Simulink model screenshot below:
MPU6500 SPIstartupOverview.png

First, when the counter value is 0, register 0x6A and 0x6B are written. The next register, 0x68, is written when the counter value is 11 which is a delay of 110ms. After all the configuration subsystems (green blocks) are executed, reading the sensor signals begins in the orange subsystem. Let's look inside one of the green configuration subsystems:
MPU6500 SPIconfigSubsystem.png

Here two registers are written using 4 transfers of a byte. The first bit of the first byte indicates if it is a read (1) or a write (0) action. The following 7 bits are the address bits of the register. After this the value can be send to the MPU6500. A value of 0x9F is send, this means: I2C disable, FIFO disable, FIFO reset, etc. Immediately after the next register is written. SPI works with a slave select pin, this pin is pulled low before the transfer so the slave knows the data is for him. The slave select pin is kept low until the data to the second byte has been successfully send. The screenshot below is the mask with the settings for the transfer of the first byte. It can be seen that the slave select pin, pin 'CON3 - D7' is enabled 50 delay cycles before transfer. (this corresponds to 1s/656250Hz * 50cycles = 76us)
MPU6500 SPIconfigTransferSettings.png

After the second byte has been transferred the slave select pin is disabled. The same procedure is followed while transferring the address and data of register 0x6B, the 3rd and 4th byte transfer in the subsystem. In Simulink the position of the blocks in the model doesn't ensure the execution order of the blocks, it could be that the byte transfer which is on the top of the model is executed as last. To ensure the sequence of byte transfers is correct you can use priorities. Right-click the (HANcoder) block and choose 'Properties', in this screen the priority can be entered. Priority 1 will be executed before a block with priority 2.
MPU6500 SPIconfigTransferPrio.png

Reading the data from the MPU6500

When the configuration is done it is time to read the data from the sensor. Unlike during the configuration it is not necessary to disable the slave select pin after each readout. To start reading the first data address is sent with the first bit high to indicate a read command. The address is 59 or 0x3B, setting the first bit to 1 gives 0xBB. The answer to this command can be neglected and thus the output of the transfer block is led to a terminator. At the next transfer block the input is irrelevant as long as the slave select pin is kept enabled but the output is the most significant byte (MSB) of the acceleration in x-direction. Every time a register is read the MPU6500 automatically jumps to the next registry so with the next byte transfer the next data registry is read. Priorities are used again to ensure the correct sequence.
MPU6500 SPIreadout.png

After all registries are read the slave select can be disabled again by checking the 'Disable slave select after transfer' checkbox.
MPU6500 SPIdisableSlaveSelect.png

There are two ways of combining the bytes into 16-bit integers, you can multiply the MSB with 256 and then add the LSB to get the bits in the right order. You can also use the bitshift block from Simulink and use the bitwise OR operator to join the two bytes. Pay attention that the data type is changed to 16-bit before bit shifting, else the bits will be lost.
MPU6500 SPIbitShifting.png

Vehicle Logger

This is part of the Vehicle Logger example, the next step for this example is building the OBD Reader.

See Also

SPI master transfer | SPI master init