/********************************************************************* * Function: LDSequentialReadI2C() * Input: ControlByte, address, *rdptr, length. * Overview: Performs a sequential read of length bytes starting at address * and places data in array pointed to by *rdptr ********************************************************************/ unsigned int LDSequentialReadI2C(unsigned char ControlByte, unsigned char address, unsigned char *rdptr, unsigned char length) { IdleI2C(); //Ensure Module is Idle StartI2C(); //Initiate start condition WriteI2C(ControlByte); //write 1 byte IdleI2C(); //Ensure module is Idle WriteI2C(address); //Write word address IdleI2C(); //Ensure module is idle RestartI2C(); //Generate I2C Restart Condition WriteI2C(ControlByte | 0x01); //Write 1 byte - R/W bit should be 1 for read IdleI2C(); //Ensure bus is idle getsI2C(rdptr, length); //Read in multiple bytes NotAckI2C(); //Send Not Ack StopI2C(); //Send stop condition return(0); }
/********************************************************************* * Function: LDByteReadI2C() * Input: Control Byte, Address, *Data, Length. * Overview: Performs a low density read of Length bytes and stores in *Data array * starting at Address. ********************************************************************/ unsigned int LDByteReadI2C(unsigned char ControlByte, unsigned char Address, unsigned char *Data, unsigned char Length) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C(ControlByte); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C(ControlByte | 0x01); //Write control byte for read IdleI2C(); //wait for bus Idle getsI2C(Data, Length); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop }
/********************************************************************* * Function: HDByteReadI2C() * * Input: Control Byte, HighAdd, LowAdd, *Data, Length. * * Output: None. * * Overview: Performs a low density read of Length bytes and stores in *Data array * starting at Address formed from HighAdd and LowAdd. * * Note: None ********************************************************************/ unsigned int HDByteReadI2C(unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char *Data, unsigned char Length) { IdleI2C(); //Wait for bus Idle StartI2C(); //Generate Start condition WriteI2C(ControlByte); //send control byte for write IdleI2C(); //Wait for bus Idle WriteI2C(HighAdd); //Send High Address IdleI2C(); //Wait for bus Idle WriteI2C(LowAdd); //Send Low Address IdleI2C(); //Wait for bus Idle RestartI2C(); //Generate Restart WriteI2C(ControlByte | 0x01); //send control byte for Read IdleI2C(); //Wait for bus Idle getsI2C(Data, Length); //Read Length number of bytes to Data NotAckI2C(); //send Not Ack StopI2C(); //Send Stop Condition return(0); }
int i2ReadData(char SlaveAddress, unsigned char Address, int Bytes, unsigned char *Dades) { // Pre: Bytes >= 0; Dades t‚ espai per a NumBytes // Pre: El bit m‚s baix de Address no t‚ informaci¢ (ser… read o write) // Post: retorna 0 si tot OK o 1 si hi ha un timeout d'un msegon // en un ack IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C((SlaveAddress<<1)); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C((SlaveAddress<<1 | 0x01)); //Write control byte for read IdleI2C(); //wait for bus Idle getsI2C(Dades, Bytes); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop return 0; }
unsigned char HDByteReadI2C( unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char *data, unsigned char length ) { IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( ControlByte ); // write 1 byte IdleI2C(); // ensure module is idle WriteI2C( HighAdd ); // WRITE word address to EEPROM IdleI2C(); // ensure module is idle while ( SSPCON2bits.RSEN ); // wait until re-start condition is over WriteI2C( LowAdd ); // WRITE word address to EEPROM IdleI2C(); // ensure module is idle RestartI2C(); // generate I2C bus restart condition while ( SSPCON2bits.RSEN ); // wait until re-start condition is over WriteI2C( ControlByte | 0x01 ); // WRITE 1 byte - R/W bit should be 1 for read IdleI2C(); // ensure module is idle getsI2C( data, length ); // read in multiple bytes NotAckI2C(); // send not ACK condition while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over StopI2C(); // send STOP condition while ( SSPCON2bits.PEN ); // wait until stop condition is over return ( 0 ); // return with no error }