//##############################################################################
unsigned short int zed=0;
volatile unsigned short int new =0 ,dataRead=0 , dataWrite=0;
unsigned char dataReadCh[7] , dataWriteCh[7];
//##############################################################################
void interrupt()
{
if(PIR1.SSPIF == 1)
{
SSPCON1.CKP = 0;
new=1;
zed = SSPBUF;
PIR1.SSPIF = 0;
SSPCON1.CKP = 1;
if ((SSPCON1.SSPOV) || (SSPCON1.WCOL))
{
zed = SSPBUF; // Read the previous value to clear the buffer
SSPCON1.SSPOV = 0; // Clear the overflow flag
SSPCON1.WCOL = 0; // Clear the collision bit
SSPCON1.CKP = 1;
}
if(!SSPSTAT.D_A && !SSPSTAT.R_W)
{
while(!SSPSTAT.BF){};
while(SSPSTAT.BF){dataRead = SSPBUF;}
SSPCON1.CKP = 1;
}
else if(SSPSTAT.D_A && SSPSTAT.R_W)
{
while(SSPSTAT.BF){zed = SSPBUF;}
SSPBUF = dataWrite ;
SSPCON1.CKP = 1;
while(SSPSTAT.BF){};
}
PIR1.SSPIF = 0;
}
}
//##############################################################################
void I2C_Slave_Init(unsigned short int address)
{
SSPSTAT = 0b11000000;
SSPADD = address;
SSPCON1 = 0b00110110;
SSPCON2 = 0b00000001;
//TRISC.B3 = 1; //Setting as input as given in datasheet
//TRISC.B4 = 1; //Setting as input as given in datasheet
TRISC=0b00011000;
LATC= 0b00011000;
INTCON = 0b00000000;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIE1.SSPIE = 1;
PIR1.SSPIF = 0;
while(SSPSTAT.BF){zed = SSPBUF;SSPCON1.SSPOV = 0;SSPCON1.WCOL = 0;}
}
//##############################################################################
void main()
{
OSCTUNE.PLLEN=0;
ADCON1=0b00001111;
TRISA.B0 = 0; //PORTD as output
PORTA.B0=0;
I2C_Slave_Init(0xAA); //Initialize as a I2C Slave with address 0x30
while(1)
{
if(new==1)
{
PORTA.B0=1;
new=0;
}
}
}