char e_i2cp_read_string(char device_add, char *read_buffer, char start_address, char string_length) { char error=0; error=1; error&=e_i2c_start(); error&=e_i2c_write(device_add); // Device address error&=e_i2c_write(start_address); // address of first register to be read error&=e_i2c_restart(); error&=e_i2c_write(device_add+1); // To change data direction ([bit 0]=1) while (string_length) { error&=e_i2c_read(read_buffer); // read a single byte read_buffer++; string_length--; if(string_length == 0) { /* If last char, generate NACK sequence */ error&=e_i2c_nack(); // the last byte to be read, must send nack } else { /* For other chars,generate ACK sequence */ error&=e_i2c_ack(); } while(I2C1CONbits.ACKEN == 1); /* Wait till ACK/NACK sequence is over */ } e_i2c_stop(); // End read cycle return error; }
/*!\brief Continuous read (multiple register reading). * \param device_add The device address * \param read_buffer the buffer that will contain the values read from the registers * \param reg The register start address * \return 1 to confirme the oparation and 0 for an error */ char readRegMulti(char device_add, unsigned char *read_buffer, char start_address, unsigned char numBytes) { char error = 0; unsigned int i = 0; while (!error) { error = 1; error &= e_i2c_start(); error &= e_i2c_write(device_add); // Device address error &= e_i2c_write(start_address | 0x80); // address of first register to be read error &= e_i2c_restart(); error &= e_i2c_write(device_add + 1); // To change data direction ([bit 0]=1) for (i = 0; i < numBytes; i++) { error &= e_i2c_read(&read_buffer[i]); // read the next byte if (i == (numBytes - 1)) { // the last byte to be read, must send nack error &= e_i2c_nack(); } else { error &= e_i2c_ack(); // not the last byte, send ack } } e_i2c_stop(); // End read cycle if (error) break; e_i2c_reset(); } return error; }