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; }
// 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(); }
void write_ext_eeprom(unsigned short address, char data) { char error=0; // Initialize to no error while(!error) // While they are still some errors... { error=1; // We initilize the error to 1 //while(!ext_eeprom_ready()); // Proposed in 2432.c from PICC, not very useful here error&=e_i2c_start(); // We intiate a start and... error&=e_i2c_write(0x10); // ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we write (last bit = 0) error&=e_i2c_write(0x00); // ... write the most significant byte of the memory address error&=e_i2c_write(0x05); // ... and finally the value that we want to store error&=e_i2c_stop(); // ... the tranfer is then terminated by a stop } // delay_ms(8); }
//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 read_ext_eeprom(unsigned short address) { char error=0; // Initialize to no error char data=0x00; // Initialize the data while(!error) { // While they are still some errors... error=1; // We initilize the error to 1 //while(!ext_eeprom_ready()); // Proposed in 2432.c from PICC, not very useful here error&=e_i2c_start(); // We intiate a start and... error&=e_i2c_write(0x10); // ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we write (last bit = 0) error&=e_i2c_write(0x01); // ... then the least significant byte of the memory address 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(0x11); // ... write the hardware selectable address A0 = 0, A1 = 1, A2 = 0 and we read (last bit = 1) error&=e_i2c_read(&data); // ... and read the data sent by the slave (here the memory) error&=e_i2c_nack(); // ... only 1 byte is being read, so send nack error&=e_i2c_stop(); // ... the tranfer is then terminated by a stop } return(data); }
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; }