/* * MAIN */ int main(int argc, char** argv) { // RA1 as DO LATA = 0; ADCON1 = 0x06; TRISA1 = 0; LATA1 = 1; // I2C configuration i2c_setup(); unsigned char w; for(w=0;w<20;w++) I2C_Recv[w]=0; unsigned int temps = 25; unsigned char temp, length=0; while(1){ LATA1 = ~LATA1; Delay10KTCYx(temps); check_interruptions(); if (DataRdyI2C()==1) { temps += 25; temp = ReadI2C(); //********************* Data reception from master by slave ********************* do { while(DataRdyI2C()==0); // WAIT UNTILL THE DATA IS TRANSMITTED FROM master I2C_Recv[length++]=getcI2C(); // save byte received } while(length!=20); } } //******************** write sequence from slave ******************************* while(SSPSTATbits.S!=1); //wait untill STOP CONDITION //********************* Read the address sent by master from buffer ************** while(DataRdyI2C()==0); //WAIT UNTILL THE DATA IS TRANSMITTED FROM master temp = ReadI2C(); //********************* Slave transmission ************************************ if(SSPSTAT & 0x04) //check if master is ready for reception while(putsI2C(I2C_Send)); // send the data to master //-------------TERMINATE COMMUNICATION FROM MASTER SIDE--------------- CloseI2C(); //close I2C module while(1) { LATA1 = ~LATA1; Delay10KTCYx(100); }; //End of program return (EXIT_SUCCESS); }
/********************************************************************* * Function: XEE_RESULT XEERead(void) * * PreCondition: XEEInit() && XEEBeginRead() are already called. * * Input: None * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Reads next byte from EEPROM; internal address * is incremented by one. * * Note: This function does not release I2C bus. * User must call XEEEndRead() when read is not longer * needed; I2C bus will released after XEEEndRead() * is called. ********************************************************************/ unsigned char XEERead(void) { getcI2C(); while( EEPROM_SPICON2bits.RCEN ); // check that receive sequence is over. EEPROM_SPICON2bits.ACKDT = 0; // Set ack bit EEPROM_SPICON2bits.ACKEN = 1; while( EEPROM_SPICON2bits.ACKEN ); return EEPROM_SSPBUF; }
/********************************************************************* * Function: XEE_RESULT XEEEndRead(void) * * PreCondition: XEEInit() && XEEBeginRead() are already called. * * Input: None * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Ends sequential read cycle. * * Note: This function ends seuential cycle that was in * progress. It releases I2C bus. ********************************************************************/ XEE_RESULT XEEEndRead(void) { getcI2C(); while( EEPROM_SPICON2bits.RCEN ); // check that receive sequence is over. NotAckI2C(); while( EEPROM_SPICON2bits.ACKEN ); StopI2C(); while( EEPROM_SPICON2bits.PEN ); return XEE_SUCCESS; }