/*!\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; }
// Write 1 byte to register reg of devantec sensor void e_i2cd_write (char device_add, char reg, char value) { e_i2c_start(); e_i2c_write(device_add); // Writing the device (slave) address e_i2c_write(reg); // Device register address e_i2c_write(value); // Data to device e_i2c_stop(); // Ending the communication e_i2c_reset(); }
//read 1 byte from the register reg of devantec sensor char e_i2cd_readb(char device_add, char reg) { char value; e_i2c_start(); // start e_i2c_write(device_add); // Device address e_i2c_write(reg); // Register address e_i2c_restart(); // 2nd start e_i2c_write(device_add+1); // Device address e_i2c_read(&value); // read single byte e_i2c_nack(); // only 1 byte is being read, so send nack e_i2c_stop(); // end read cycle e_i2c_reset(); return value; }
/*! \brief Write a specific register on a device * \param device_add The address of the device you want information * \param reg The register address you want read on the device * \param value The data you want to write * \return 1 to confirme the oparation and 0 for an error */ char e_i2cp_write (char device_add, char reg, char value) { char error=0; while(!error) { error=1; error&=e_i2c_start(); error&=e_i2c_write(device_add); // Writing the device (slave) address error&=e_i2c_write(reg); // Device register address error&=e_i2c_write(value); // Data to device error&=e_i2c_stop(); // Ending the communication if(error) break; e_i2c_reset(); } return error; }
char e_i2cp_write (char device_add,char reg, char value) { char error=0; // Initialize to no error while(!error) { // While they are still some errors... error=1; // We initilize the error to 1 error&=e_i2c_start(); // We intiate a start and... error&=e_i2c_write(device_add); // ... write the I2C MODULE ADDRESS we want to write error&=e_i2c_write(reg); // ... and the REGISTER ADDRESS we want to write error&=e_i2c_write(value); // ... and send the value we want to write error&=e_i2c_stop(); // ... the tranfer is then terminated by a stop if(error) { break; } e_i2c_reset(); } return error; }
char e_i2cp_read(char device_add,char reg) { char error=0; // Initialize to no error char value; // Value that will be returned while(!error) { // While they are still some errors... error=1; // We initilize the error to 1 error&=e_i2c_start(); // We intiate a start and... error&=e_i2c_write(device_add); // ... write the I2C MODULE ADDRESS we want to read error&=e_i2c_write(reg); // ... and the REGISTER ADDRESS we want to read error&=e_i2c_restart(); // ... In order to indicate to the slave that he can send the data, we iniate a restart error&=e_i2c_write(device_add+1); // ... and rewrite the MODULE ADDRESS with change of data direction ([bit 0]=1) error&=e_i2c_read(&value); // ... and read the data sent by the slave (the I2C Module) e_i2c_nack(); // ... only 1 byte is being read, so send nack e_i2c_stop(); // ... the tranfer is then terminated by a stop if(error) { break; } e_i2c_reset(); } return value; }
/*! \brief Read a specific register on a device * \param device_add The address of the device you want information * \param reg The register address you want read on the device * \return The readed value */ char e_i2cp_read(char device_add, char reg) { char error=0; char value; while(!error) { error=1; error&=e_i2c_start(); error&=e_i2c_write(device_add); // Device address error&=e_i2c_write(reg); // Register address error&=e_i2c_restart(); error&=e_i2c_write(device_add+1); // To change data direction ([bit 0]=1) error&=e_i2c_read(&value); // read single byte e_i2c_nack(); // only 1 byte is being read, so send nack e_i2c_stop(); // end read cycle if(error) break; e_i2c_reset(); } return value; }