void MPU6050_Initialize() //初始化过程 ,其实就是写 5个寄存器 { MPU6050_I2C_ByteWrite(0xd0,0x00,MPU6050_RA_PWR_MGMT_1); // reg107, 唤醒,8M内部时钟源 MPU6050_I2C_ByteWrite(0xd0,0x07,MPU6050_RA_SMPLRT_DIV); //采用频率 1000 MPU6050_I2C_ByteWrite(0xd0,0x06,MPU6050_RA_CONFIG); MPU6050_I2C_ByteWrite(0xd0,0x01,MPU6050_RA_ACCEL_CONFIG); //加速度量程 2g MPU6050_I2C_ByteWrite(0xd0,0x18,MPU6050_RA_GYRO_CONFIG); //角速度量程 2000度/s }
/** write a single bit in an 8-bit device register. * @param slaveAddr I2C slave device address * @param regAddr Register regAddr to write to * @param bitNum Bit position to write (0-7) * @param value New bit value to write */ void MPU6050_WriteBit(uint8_t slaveAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) { uint8_t tmp; MPU6050_I2C_BufferRead(slaveAddr, &tmp, regAddr, 1); tmp = (data != 0) ? (tmp | (1 << bitNum)) : (tmp & ~(1 << bitNum)); MPU6050_I2C_ByteWrite(slaveAddr,&tmp,regAddr); }
/** Write multiple bits in an 8-bit device register. * @param slaveAddr I2C slave device address * @param regAddr Register regAddr to write to * @param bitStart First bit position to write (0-7) * @param length Number of bits to write (not more than 8) * @param data Right-aligned value to write */ void MPU6050_WriteBits(uint8_t slaveAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) { // 010 value to write // 76543210 bit numbers // xxx args: bitStart=4, length=3 // 00011100 mask byte // 10101111 original value (sample) // 10100011 original & ~mask // 10101011 masked | value uint8_t tmp; MPU6050_I2C_BufferRead(slaveAddr, &tmp, regAddr, 1); uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); data <<= (bitStart - length + 1); // shift data into correct position data &= mask; // zero all non-important bits in data tmp &= ~(mask); // zero all important bits in existing byte tmp |= data; // combine data with existing byte MPU6050_I2C_ByteWrite(slaveAddr,&tmp,regAddr); }
/** Write multiple bits in an 8-bit device register. * @param slaveAddr I2C slave device address * @param regAddr Register regAddr to write to * @param bitStart First bit position to write (0-7) * @param length Number of bits to write (not more than 8) * @param data Right-aligned value to write */ void MPU6050_WriteBits( uint8_t slaveAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data ) { /* * 010 value to write * 76543210 bit numbers * xxx args: bitStart=4, length=3 * 00011100 mask byte * 10101111 original value (sample) * 10100011 original & ~mask * 10101011 masked | value */ uint8_t tmp, mask; MPU6050_I2C_BufferRead( slaveAddr, &tmp, regAddr, 1 ); mask = ( (1 << length) - 1) << (bitStart - length + 1); data <<= (bitStart - length + 1); /* shift data into correct position */ data &= mask; /* zero all non-important bits in data */ tmp &= ~(mask); /* zero all important bits in existing byte */ tmp |= data; /* combine data with existing byte */ MPU6050_I2C_ByteWrite( slaveAddr, &tmp, regAddr ); }
void MPU6050_WriteByte(uint8_t slaveAddr, uint8_t regAddr, uint8_t data) { uint8_t tmp; tmp = data; MPU6050_I2C_ByteWrite(slaveAddr,&tmp,regAddr); }