Example #1
0
unsigned int GetData(unsigned char REG_Address)
{
    char H, L;
    H = I2C_ByteRead(REG_Address);
    L = I2C_ByteRead(REG_Address + 1);
    return (H << 8) + L; //合成数据
}
//有符号数
int16_t GetData_GYRO(unsigned char REG_Address)
{
	uint8_t H,L;
	H=I2C_ByteRead(MPU6050_ADDRESS,REG_Address);
	L=I2C_ByteRead(MPU6050_ADDRESS,REG_Address+1);
	return (H<<8)+L;   //合成数据

}
//是否是带符号数,待确认
int16_t GetData_BMP280(unsigned char REG_Address)
{
	uint8_t H,L;
	I2C_ByteWrite(BMP280_ADDRESS,0xF4,0x25);//设置forced mode,每次测量完都要回到sleep mode
	delayms(5);
	H=I2C_ByteRead(BMP280_ADDRESS,REG_Address);
	L=I2C_ByteRead(BMP280_ADDRESS,REG_Address+1);
	return (H<<8)+L;   //合成数据

}
Example #4
0
int I2C_Read(I2C_TypeDef* I2Cx, int address, char* data, int length, int stop){
	uint16_t timeout;
	int count, value;
	
	I2C_Start(I2Cx);
	
	// Wait until SB flag is set
  timeout = FLAG_TIMEOUT;
	while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_SB) == RESET){
		timeout--;
		if(timeout == 0){
			return -1;
		}
	}
	
	I2Cx->DR = I2C_7BIT_ADD_READ(address);
	
	// Wait address is acknowledged
  timeout = FLAG_TIMEOUT;
	while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_ADDR) == RESET){
		timeout--;
		if(timeout == 0){
			return -1;
		}
	}
	
	__I2C_CLEAR_ADDRFLAG(I2Cx);
	
	// Read all bytes except last one
  for (count = 0; count < (length - 1); count++) {
		value = I2C_ByteRead(I2Cx, 0);
    data[count] = (char)value;
	}

  // If not repeated start, send stop.
  // Warning: must be done BEFORE the data is read.
  if (stop) {
		I2C_Stop(I2Cx);
  }

  // Read the last byte
  value = I2C_ByteRead(I2Cx, 1);
  data[count] = (char)value;
	
  return length;
}
//有符号数 ,是否可设置为连续读取模式
int16_t GetData_MAG(unsigned char REG_Address)
{
	uint8_t H,L;
	uint8_t ST1;
	I2C_ByteWrite(MAG_ADDRESS,0x0A,0x11);//根据手册知单次测量模什结束会回到powerdown模式,需要重置一下
	delayms(1);
	
  do// Read register Status 1 and wait for the DRDY: Data Ready
  {
    ST1=I2C_ByteRead(MAG_ADDRESS,0x02);
  }
  while(!(ST1&0x01));
	 
	L=I2C_ByteRead(MAG_ADDRESS,REG_Address);
	H=I2C_ByteRead(MAG_ADDRESS,REG_Address+1);

	return (H<<8)+L;   //合成数据

}
Example #6
0
status_t LSM303C::ACC_ReadReg(ACC_REG_t reg, uint8_t& data)
{
  // printf("Reading address 0x");
  // printf("0x%d\n", reg);
  status_t ret;
    
  if (interfaceMode == MODE_I2C)
  {
    ret = I2C_ByteRead(ACC_I2C_ADDR, reg, data);
  }
  else
  {
    ret = IMU_HW_ERROR;
  }

  return ret;
}
Example #7
0
status_t LSM303C::MAG_ReadReg(MAG_REG_t reg, uint8_t& data)
{
  // printf("Reading register 0x");
  // printf("0x%d\n", reg);
  status_t ret = IMU_GENERIC_ERROR;
    
  if (interfaceMode == MODE_I2C)
  {
    ret = I2C_ByteRead(MAG_I2C_ADDR, reg, data);
  }
  else
  {
    ret = IMU_GENERIC_ERROR; // Shouldn't get here
  }

  return ret;
}
Example #8
0
int I2C_DataRead(int address, char *data, int length, int stop)
{  
    int timeout;
    int count;
    int value;
    /* update CR2 register */
    I2C1->CR2 = (I2C1->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
               | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SoftEnd_Mode | (uint32_t)I2C_Generate_Start_Read);

    // Read all bytes
    for (count = 0; count < length; count++) {
        value = I2C_ByteRead(0);
        data[count] = (char)value;
    }
    // Wait transfer complete
    timeout = LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2C1, I2C_FLAG_TC) == RESET) {
        timeout--;
        if (timeout == 0) {
            return -1;
        }
    }
    I2C_ClearFlag(I2C1, I2C_FLAG_TC);
    // If not repeated start, send stop.
    if (stop) {
        I2C1->CR2 |= I2C_CR2_STOP;
        /* Wait until STOPF flag is set */
        timeout = FLAG_TIMEOUT;
        while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET) {
            timeout--;
            if (timeout == 0) {
                return -1;
            }
        }
        /* Clear STOP Flag */
       I2C_ClearFlag(I2C1, I2C_FLAG_STOPF);
    }
    return length;
}
status_t LSM303C::ACC_ReadReg(ACC_REG_t reg, uint8_t& data)
{
  debug_print("Reading address 0x");
  debug_printlns(reg, HEX);
  status_t ret;
    
  if (interfaceMode == MODE_I2C)
  {
    ret = I2C_ByteRead(ACC_I2C_ADDR, reg, data);
  }
  else if (interfaceMode == MODE_SPI)
  {
    data = SPI_ReadByte(ACC, reg);
    ret = IMU_SUCCESS;
  }
  else
  {
    ret = IMU_HW_ERROR;
  }

  return ret;
}
status_t LSM303C::MAG_ReadReg(MAG_REG_t reg, uint8_t& data)
{
  debug_print("Reading register 0x");
  debug_printlns(reg, HEX);
  status_t ret = IMU_GENERIC_ERROR;
    
  if (interfaceMode == MODE_I2C)
  {
    ret = I2C_ByteRead(MAG_I2C_ADDR, reg, data);
  }
  else if (interfaceMode == MODE_SPI)
  {
    data = SPI_ReadByte(MAG, reg);
    ret = IMU_SUCCESS;
  }
  else
  {
    ret = IMU_GENERIC_ERROR; // Shouldn't get here
  }

  return ret;
}