Dear friend ask to help me.
I try to read mpu6000 use SPI.
Please look my programm. What is wrong?
Help me give answer to qustion "Who Am I" (read 0x75 registr).
аbout mpu6000:
register map and discriptions:
v053_MPU6000_test1.ino (12.1 KB)
Dear friend ask to help me.
I try to read mpu6000 use SPI.
Please look my programm. What is wrong?
Help me give answer to qustion "Who Am I" (read 0x75 registr).
аbout mpu6000:
register map and discriptions:
v053_MPU6000_test1.ino (12.1 KB)
Hello and welcome,
Maybe you should explain your problem?
First set spi
const int ChipSelPin = 53; // set D53 as CS
SPI.begin(); // start the SPI library
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI_CLOCK_DIV16 = 1 MHz // 1 MHz is the maximum SPI clock frequency according to the MPU-6000
SPI.setBitOrder(MSBFIRST); // data delivered MSB first as in MPU-6000 Product Specification
SPI.setDataMode(SPI_MODE0); // latched on rising edge, transitioned on falling edge, active low
second try wright to registers:
SPIwriteBit(0x6B, 7, true, ChipSelPin); // it must RESET mpu6000
delay(200);
SPIwriteBit(0x6B, 6, false, ChipSelPin); // Disabling sleep mode... wakeup mpu6000
delay(200);
third try to read register 0x75 mpu 6000:
Serial.println(SPIread(0x75, ChipSelPin));
Use function spi.h and:
// --- Function for SPI reading one bit from sensor
// reg : MPU-6000 register number to read from
// bitNum : bit number in the register to read - 7 (MSB) to 0 (LSB)
// ChipSelPin : MPU-6000 chip select pin number (in this sketch defined by ChipSelPin)
// return > byte 0x00 if bit is 0, otherwise byte with a 1 at bitNum (rest 0's)
byte SPIreadBit(byte reg, byte bitNum, int ChipSelPin)
{
byte byte_value = SPIread(reg, ChipSelPin);
byte bit_value = byte_value & (1 << bitNum); // AND result from register byte value and byte with only one "1" at place of bit to return (rest "0"'s)
#ifdef DEBUG
Serial.print(" bit_"); Serial.print(bitNum); Serial.print(" = ");
if (bit_value == 0x00)
{
Serial.println("0");
}
else
{
Serial.println("1");
}
#endif
return bit_value;
}
//--- Function for SPI writing one bit to sensor
// reg : MPU-6000 register number to write to
// bitNum : bit number in the register to write to - 7 (MSB) to 0 (LSB)
// databit : bit value to be written into reg - false or 0 | true or non-zero (1 will be logical)
// ChipSelPin : MPU-6000 chip select pin number (in this sketch defined by ChipSelPin)
// return > nothing
//
// first read byte, then insert bit value, then write byte:
// otherwise all other bits will be written 0, this may trigger unexpected behaviour
void SPIwriteBit(byte reg, byte bitNum, byte databit, int ChipSelPin)
{
byte byte_value = SPIread(reg, ChipSelPin);
if (databit == 0)
{
byte_value = byte_value & ~(1 << bitNum); // AND result from register byte value and byte with only one "0" at place of bit to write (rest "1"'s)
}
else // databit is intended to be a "1"
{
byte_value = byte_value | (1 << bitNum); // OR result from register byte value and byte with only one "1" at place of bit to write (rest "0"'s)
}
SPIwrite(reg, byte_value, ChipSelPin);
}
// --- Function for SPI reading one byte from sensor
// reg : MPU-6000 register number to read from
// ChipSelPin : MPU-6000 chip select pin number (in this sketch defined by ChipSelPin)
// return > register contents
byte SPIread(byte reg, int ChipSelPin)
{
DEBUG_PRINT("SPI (/CS"); DEBUG_PRINT(ChipSelPin); DEBUG_PRINT(") ");
DEBUG_PRINT("reading 1 byte from register 0x");
if (reg < 0x10) DEBUG_PRINT("0"); // add leading zero - this is an Arduino bug
DEBUG_PRINTF(reg, HEX); DEBUG_PRINT("... ");
digitalWrite(ChipSelPin, LOW); // select MPU-6000 for SPI transfer (low active)
SPI.transfer(reg | 0x80); // reg | 0x80 causes a "1" added as MSB to reg to denote reading from reg i.s.o. writing to it
byte read_value = SPI.transfer(0x00); // write 8-bits zero to MPU-6000, read the 8-bits coming back from reg at the same time
digitalWrite(ChipSelPin, HIGH); // deselect MPU-6000 for SPI transfer
DEBUG_PRINT("0x");
if (read_value < 0x10) DEBUG_PRINT("0"); // add leading zero - this is an Arduino bug
DEBUG_PRINTF(read_value, HEX); DEBUG_PRINTLN(" (done)");
return read_value;
}
First:
Is it setting right ? for:
1.Data is delivered MSB first and LSB last
2.Data is latched on the rising edge of SCLK
3.Data should be transitioned on the falling edge of SCLK
4.The maximum frequency of SCLK is 1MHz
third try to read register 0x75 mpu 6000:
Serial.println(SPIread(0x75, ChipSelPin));
What is printed in the serial monitor ?
guix:
What is printed in the serial monitor ?
SPIread(reg,CS) -read register #reg, cs-number pin use us CS
Serial.println - send it to computer
serial monitor printed 255