/** 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); }
void KumpasOku(s16* AccelGyro) { int i; u8 tmpBuffer[14]; MPU6050_I2C_BufferRead(MPU6050_DEFAULT_ADDRESS, tmpBuffer, 0x3B, 0); /** * @brief Reads a block of data from the MPU6050. * @param slaveAddr : slave address MPU6050_DEFAULT_ADDRESS * @param pBuffer : pointer to the buffer that receives the data read from the MPU6050. * @param readAddr : MPU6050's internal address to read from. * @param NumByteToRead : number of bytes to read from the MPU6050 ( NumByteToRead >1 only for the Mgnetometer readinf). * @return None */ /* Get acceleration */ for(i=0; i<3; i++) AccelGyro[i]=((s16)((u16)tmpBuffer[2*i] << 8) + tmpBuffer[2*i+1]); /* Get Angular rate */ for(i=4; i<7; i++) AccelGyro[i-1]=((s16)((u16)tmpBuffer[2*i] << 8) + tmpBuffer[2*i+1]); }
/** Get raw 6-axis motion sensor readings (accel/gyro). * Retrieves all currently available motion sensor values. * @param AccelGyro 16-bit signed integer array of length 6 * @see MPU6050_RA_ACCEL_XOUT_H */ void MPU6050_GetRawAccelGyro(s16* AccelGyro) { u8 tmpBuffer[14]; MPU6050_I2C_BufferRead(MPU6050_DEFAULT_ADDRESS, tmpBuffer, MPU6050_RA_ACCEL_XOUT_H, 14); /* Get acceleration */ for(int i=0; i<3; i++) AccelGyro[i]=((s16)((u16)tmpBuffer[2*i] << 8) + tmpBuffer[2*i+1]); /* Get Angular rate */ for(int i=4; i<7; i++) AccelGyro[i-1]=((s16)((u16)tmpBuffer[2*i] << 8) + tmpBuffer[2*i+1]); }
/** Read multiple bits from an 8-bit device register. * @param slaveAddr I2C slave device address * @param regAddr Register regAddr to read from * @param bitStart First bit position to read (0-7) * @param length Number of bits to read (not more than 8) * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in readTimeout) */ void MPU6050_ReadBits(uint8_t slaveAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data) { // 01101001 read byte // 76543210 bit numbers // xxx args: bitStart=4, length=3 // 010 masked // -> 010 shifted uint8_t tmp; MPU6050_I2C_BufferRead(slaveAddr, &tmp, regAddr, 1); uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); tmp &= mask; tmp >>= (bitStart - length + 1); *data = tmp; }
/** 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 ); }
/** Get raw 6-axis motion sensor readings (accel/gyro). * Retrieves all currently available motion sensor values. * @param AccelGyro 16-bit signed integer array of length 6 * @see MPU6050_RA_ACCEL_XOUT_H */ void MPU9150_GetRawAccelGyro(s16* AccelGyro,s16* Magne) { u8 tmpBuffer_6050[14]; //u8 tmpBuffer_8975[6]; u8 ReadData = 0x00; MPU6050_I2C_BufferRead(MPU6050_DEFAULT_ADDRESS, tmpBuffer_6050, MPU6050_RA_ACCEL_XOUT_H, 14); //MPU6050_ReadBit(AK8975_I2C_ADDR, AK8975_ST1, 0, &ReadData); /* if (ReadData == 1) { MPU6050_I2C_BufferRead(AK8975_I2C_ADDR, tmpBuffer_8975,AK8975_HXL, 6); MPU6050_WriteBits(AK8975_I2C_ADDR,AK8975_CNTL, 3,4, 0x01); // Set Single Measurement Mode } /* Get acceleration */ for(int i=0; i<3; i++) AccelGyro[i]=((s16)((u16)tmpBuffer_6050[2*i] << 8) + tmpBuffer_6050[2*i+1]); /* Get Angular rate */ for(int i=4; i<7; i++) AccelGyro[i-1]=((s16)((u16)tmpBuffer_6050[2*i] << 8) + tmpBuffer_6050[2*i+1]); /* Get magne rate */ //for(int i=0; i<3; i++) //Magne[i]=((s16)((u16)tmpBuffer_8975[2*i+1] << 8) + tmpBuffer_8975[2*i]); }
/** Read a single bit from an 8-bit device register. * @param slaveAddr I2C slave device address * @param regAddr Register regAddr to read from * @param bitNum Bit position to read (0-7) * @param data Container for single bit value * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in readTimeout) */ void MPU6050_ReadBit(uint8_t slaveAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data) { uint8_t tmp; MPU6050_I2C_BufferRead(slaveAddr, &tmp, regAddr, 1); *data = tmp & (1 << bitNum); }