/********************************************************************* * Function: LDByteWriteI2C() * Input: Control Byte, 8 - bit address, data. * Overview: Write a byte to low density device at address LowAdd ********************************************************************/ unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode1; unsigned int ErrorCode2; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(ControlByte); //Write Control byte IdleI2C(); ErrorCode1 = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); ErrorCode2 = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition //EEAckPolling(ControlByte); //Perform ACK polling if(ErrorCode1 == 0) { printf("ACK 1 not recieved"); } if(ErrorCode2 == 0) { printf("ACK 2 not recieved"); } //return(ErrorCode); }
unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode; IdleI2C(); StartI2C(); WriteI2C(ControlByte); IdleI2C(); ErrorCode = ACKStatus(); WriteI2C(LowAdd); IdleI2C(); ErrorCode = ACKStatus(); WriteI2C(data); IdleI2C(); StopI2C(); EEAckPolling(ControlByte); return(ErrorCode); }
/********************************************************************* * Function: LDByteWriteI2C() * * Input: Control Byte, 8 - bit address, data. * * Output: None. * * Overview: Write a byte to low density device at address LowAdd * * Note: None ********************************************************************/ unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(ControlByte); //Write Control byte IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition EEAckPolling(ControlByte); //Perform ACK polling return(ErrorCode); }
int i2WriteData(char SlaveAddress, unsigned char Address, unsigned char Dada) { // Pre: Bytes >= 0; // 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 segon // en un ack unsigned int ErrorCode; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C((SlaveAddress << 1) & 0xfffe); //Write Control byte IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(Address); //Write Low Address IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(Dada); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition return(ErrorCode); }
/********************************************************************* * Function: EEAckPolling() * Input: Control byte. * Output: Error state. * Overview: Polls the bus for an Acknowledge from device ********************************************************************/ unsigned int EEAckPolling(unsigned char control) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start condition if(I2C1STATbits.BCL) { return(-1); //Bus collision, return } else { if(WriteI2C(control)) { return(-3); //error return } IdleI2C(); //wait for bus idle if(I2C1STATbits.BCL) { return(-1); //error return } while(ACKStatus()) { RestartI2C(); //generate restart if(I2C1STATbits.BCL) { return(-1); //error return } if(WriteI2C(control)) { return(-3); } IdleI2C(); } } StopI2C(); //send stop condition if(I2C1STATbits.BCL) { return(-1); } return(0); }