uint8_t mti_tcn75a_get_temperature(int32_t *result) { uint16_t tmp; uint8_t rc; uint8_t tmp_lsb = 4; uint8_t tmp_msb = 2; /* start */ StartI2C1(); /* send slave address (write) */ rc = WriteI2C1(0b10010000); if (rc != 0x00) { rc = CH_ERROR_I2C_SLAVE_ADDRESS; goto out; } /* set ambient temperature pointer */ rc = WriteI2C1(0b00000000); /* reset the bus */ RestartI2C1(); /* send slave address (read) */ WriteI2C1(0b10010001); /* read the temperature */ tmp_msb = ReadI2C1(); AckI2C1(); tmp_lsb = ReadI2C1(); NotAckI2C1(); /* stop */ StopI2C1(); /* format result */ *result = (((int32_t) tmp_msb) << 16) + (((int32_t) tmp_lsb) << 8); out: return rc; }
unsigned char I2CReadByte(unsigned char add,unsigned char subadd) { unsigned char _temp=0; StartI2C1(); while(WriteI2C1(add<<1)!=0); while(WriteI2C1(subadd)!=0); RestartI2C1(); while(WriteI2C1(1+(add<<1))!=0); _temp=ReadI2C1(); NotAckI2C1(); StopI2C1(); return _temp; }
int8_t I2C1dev_readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) { count_accel = 0; // S IdleI2C1(); StartI2C1(); // Device write address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x00); // Register address IdleI2C1(); WriteI2C1(regAddr); // R IdleI2C1(); RestartI2C1(); // Device read address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x01); for (count_accel = 0; count_accel < length; count_accel++) { // Data byte IdleI2C1(); data[count_accel] = ReadI2C1(); if (count_accel == length - 1) { // NACK IdleI2C1(); NotAckI2C1(); } else { // ACK IdleI2C1(); AckI2C1(); } } // P IdleI2C1(); StopI2C1(); return count_accel; }
bool HTS221init(void) { // Check WHO_AM_I register of HTS221 ReadI2C1(HTS221_WHO_AM_I); if (I2C1_val1 == 0xBC) { writeI2C1reg(HTS221_AV_CONF, avgT << 3 | avgH); // set temperature and humidity averaging // power on HTS221 (bit 7) and enable block data update (bit 2) writeI2C1reg(HTS221_CTRL_REG1, 0x80 | 0x04 | HTODR); // set temperature and humidity sample ODR // Read calibrations data H0_rH_x2 = ReadI2C0(HTS221_CALIB_0); H1_rH_x2 = ReadI2C0(HTS221_CALIB_1); T0_degC_x8 = ReadI2C0(HTS221_CALIB_2); T1_degC_x8 = ReadI2C0(HTS221_CALIB_3); T0_msb = ReadI2C0(HTS221_CALIB_5) & 0x03; T1_msb = (ReadI2C0(HTS221_CALIB_5) & 0x0C) >> 2; H0_T0_OUT = (int16_t) ((int16_t) ReadI2C0(HTS221_CALIB_7) << 8 | ReadI2C0(HTS221_CALIB_6)); H1_T0_OUT = (int16_t) ((int16_t) ReadI2C0(HTS221_CALIB_B) << 8 | ReadI2C0(HTS221_CALIB_A)); T0_OUT = (int16_t) ((int16_t) ReadI2C0(HTS221_CALIB_D) << 8 | ReadI2C0(HTS221_CALIB_C)); T1_OUT = (int16_t) ((int16_t) ReadI2C0(HTS221_CALIB_F) << 8 | ReadI2C0(HTS221_CALIB_E)); return true; }