enum commands {
start=0,
addr,
recieve,
write,
stop};
void __attribute__((interrupt,no_auto_psv)) _SI2C1Interrupt(void) {
//void i2c(){
I2C1ADD = SLAVE_ADRESS;
static unsigned char *d = 0x00;
unsigned char data_t = 0xFF;
static unsigned char cmd = start;
slave:
switch(cmd) {
case start:
if(I2C1STATbits.S) //start bit arrived
cmd = addr;
else if(I2C1STATbits.P) { //stop bit arrived
cmd = start;
goto slave;}
/*****************************************************************************/
case addr:
if(!I2C1STATbits.D_A) { // data recieved
if(!I2C1STATbits.R_W){
_TRISB5 = 1;
cmd = recieve;
}
else if(I2C1STATbits.R_W){
_TRISB5 = 0;
cmd = write;
}
}
else cmd = recieve; // adress recieved,
break;
/******************************************************************************/
case recieve:
while(!I2C1STATbits.RBF);
*d = I2C1RCV;
cmd = start;
break;
/******************************************************************************/
case write:
I2C1TRN = data_t;
while(I2C1STATbits.TBF);
cmd = start;
/*****************************************************************************/
}
_SI2C1IF = 0;
goto slave;
}