unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress) { unsigned char result; I2C_Start(); I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */ I2C_Wait(); I2C0_D = u8RegisterAddress; /* Send register address */ I2C_Wait(); I2C_RepeatedStart(); I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */ I2C_Wait(); I2C_EnterRxMode(); I2C_DisableAck(); result = I2C0_D; I2C_Wait(); I2C_Stop(); result = I2C0_D; Pause(50); return result; }
/** * @brief * @param * @param * @param * @retval */ uint8_t ADXL345_ReadReg(uint8_t ADXL345_Reg){ //Send request for register contents. I2C_Start(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Transmitter); I2C_Write(ADXL345_Reg); I2C_RepeatedStart(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Receiver); uint8_t readvalue = I2C_Read_NAck(); return readvalue; }
//============================================================================== //Gets information of RTC. //byte 0: Sets the "Word Address" //byte1: Return the information. //============================================================================== unsigned char DS3231_GetInfo(unsigned char byte0){ unsigned char byte1 = 0; I2C_Start(); I2C_WriteByte(0xD0); I2C_WriteByte(byte0); I2C_RepeatedStart(); I2C_WriteByte(0xD1); byte1 = I2C_ReadByte(0); I2C_Stop(); return(byte1); }
/** * @brief * @param * @param * @param * @retval */ int16_t ADXL345_ReadZ(){ /** Do a multi byte read. */ uint8_t buffer[2]; uint16_t bytes_read; I2C_Start(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Transmitter); I2C_Write(ADXL345_DATAZ0_REG_ADDR); I2C_RepeatedStart(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Receiver); bytes_read = I2C_Read_Buf(buffer,2); I2C_Stop(); while(bytes_read != 2){} /** assemble data */ int16_t data = ( buffer[1] <<8 | buffer[0] ); return data; }
void ADXL345_ReadXYZ(int16_t data[]){ /** Do a multi byte read. */ uint8_t buffer[6]; uint16_t bytes_read; I2C_Start(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Transmitter); I2C_Write(ADXL345_DATAX0_REG_ADDR); //X0 thru to Z1 are sequential. I2C_RepeatedStart(ADXL345_I2C_7BIT_ADDRESS<<1, I2C_Direction_Receiver); bytes_read = I2C_Read_Buf(buffer,6); I2C_Stop(); while(bytes_read != 6){} /** assemble data */ int16_t xdata = ( buffer[1] <<8 | buffer[0] ); int16_t ydata = ( buffer[3] <<8 | buffer[2] ); int16_t zdata = ( buffer[5] <<8 | buffer[4] ); data[0] = xdata; data[1] = ydata; data[2] = zdata; }
void I2C_ReadMultiRegisters(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress, unsigned char n, unsigned char *r) { char i; I2C_Start(); I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */ I2C_Wait(); I2C0_D = u8RegisterAddress; /* Send register address */ I2C_Wait(); I2C_RepeatedStart(); I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */ I2C_Wait(); I2C_EnterRxMode(); I2C_EnableAck(); i = I2C0_D; I2C_Wait(); for(i=0; i<n-2; i++) { *r = I2C0_D; r++; I2C_Wait(); } I2C_DisableAck(); *r = I2C0_D; r++; I2C_Wait(); I2C_Stop(); *r = I2C0_D; Pause(50); }