//////////////////////////////////////////////////////////////////////////////// //"#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0x44)" / //Indicates a slave address of 0x22 where a write is 0x44 and a read is 0x45: / // / //Master write: [start],[write(0x88)],[write(X)],[write(Y)],[stop] / //Master read: [start],[write(0x89)],[X=read(1)],[Y=read(0)],[stop] / // / //read(1) is the same as a read(ack) and read(0) is the same as read(nack). / //the nack read must be the last used before a stop else the bus will be locked/ //up with sda and scl=0 / //////////////////////////////////////////////////////////////////////////////// //status meaning respond with / //-----------------------------------------------------------------------------/ //0x00/0x80 Address match, write block dummy=i2c_read() / //0x01 First write from master=pointer Address=i2c_read() / //0x02-0x7f Subsequent writes from master=data Data=i2c_read() / // / //0x80 Address match, read block dummy=i2c_read() / //0x80-0xff Read by master from this slave i2c_write(data[status & 0x7f]) / //////////////////////////////////////////////////////////////////////////////// //i2c control registers for PIC16F677 #byte sspcon =0x14 #bit sspen =sspcon.5 //1=en / 0=dis #bit sspov =sspcon.6 #bit sspwcol =sspcon.7 #byte sspstat =0x94 #bit stopbit =sspstat.4 //1=stop detected #bit startbit =sspstat.3 //1=start detected #byte i2c_addr_reg =0x93 //ssp address match location //////////////////////////////////////////////////////////////////////////////// #int_SSP static void SSP_isr(void){ unsigned int8 i2c_register[16]; //I2C register stack unsigned int8 register_addr; //for multi writes unsigned int8 i; unsigned int8 status; //.............................................................................. restart_wdt(); if (sspwcol) sspwcol=0; if (sspov) sspov=0; //.............................................................................. status = i2c_isr_state(); if (status <= 0x80) //0x00-0x80 = data master-to-slave { i=i2c_read(); if (status == 1) //address register_addr = i-1; else { //data sent from master if ((status > 0) && (status < 0x80) && (register_addr < 15)) { register_addr++; i2c_register[register_addr]=i; } } } if (status >= 0x80) //0x80-0xff = data slave-to-master { i2c_write(i2c_register[status & 15]); } //.............................................................................. } ////////////////////////////////////////////////////////////////////////////////