uint8_t twi_read_byte(void) { //uint8_t databyte; twi_start(); if (twi_get_status() != 0x08) return ERROR; //select devise and send A2 A1 A0 address bits twi_write(0xD0); if (twi_get_status() != 0x18) return ERROR; twi_write(0x01); if (twi_get_status() != 0x18) return ERROR; twi_read_ack(); twi_write(0xD1); if (twi_get_status() != 0x18) return ERROR; twi_start(); twi_read_nack(); if (twi_get_status() != 0x58) return ERROR; twi_stop(); return SUCCESS; }
/* Read num bytes starting from regAddrStart. Each subsequent byte will be from the register above (newRegPtr = oldRegPtr + 1). * * Input: regAddrStart The register address to start reading bytes from * data Location to store read data * num The number of registers to be read * Return: 0 Succeeded * >0 Failed, see handle_error() for relavant error codes */ uint8_t twi_read_bytes(uint8_t regAddrStart, uint8_t *data, uint8_t num) { uint8_t i, error; /* Transmit start condition. */ if((error = twi_start())) return error; /* Transmit slave address and write bit. */ if((error = twi_sla_w(IMU_ADDR))) return error; /* Transmit to slave the register to be read. */ if((error = twi_write(regAddrStart))) return error; /* Transmit repeated start condition. */ if((error = twi_start_r())) return error; /* Transmit slave address and read bit. */ if((error = twi_sla_r(IMU_ADDR))) return error; /* Read data from slave. */ for(i = 0; i < num; i++) { if(i == num - 1) { /* Last byte, return NACK. */ if((error = twi_read_nack(&data[i]))) return error; } else { if((error = twi_read_ack(&data[i]))) return error; } } /* Transmit stop condition. */ twi_stop(); return 0; }