is the implementation identical for the 16f87x ? shouldn't the whole thing be interrupt driven? thanks! tino Michael Rigby-Jones Gesendet von: pic microcontroller discussion list 01.04.03 14:21 Bitte antworten an pic microcontroller discussion list =20 An: PICLIST@MITVMA.MIT.EDU Kopie:=20 Thema: Re: [PIC]: I2C MSSP slave implementation > -----Original Message----- > From: Tony K=FCbek [SMTP:tony.kubek@FLINTAB.COM] > Sent: Tuesday, April 01, 2003 1:00 PM > To: PICLIST@MITVMA.MIT.EDU > Subject: [PIC]: I2C MSSP slave implementation >=20 > Hi, > I'm just about to implement an slave on an 18F chip and > found that I'm having an hard time understanding the manual. >=20 > I'm not sure how to build an ISR based slave, particulary as there > is no info of the states when start/stop irq is enabled with slave > 7 bit adresses. I mean in which order does one test the bits ?=20 >=20 > I think it should be like: >=20 > Test start->clear start > Test stop->clear stop > Test write, last=3Dadress=20 > Test write, last=3Ddata > Test read, last=3Dadress > Test read, last=3Ddata > Test nack ? >=20 > But I'm not sure about this sequence, or does anyone have an > isr based i2c slave module willing to share ? >=20 > I've coded an master module a long time ago and find that was quite > easy, however the slave module gives me gray hairs.=20 >=20 I have coded a slave for a 16F877 which worked fine on an 18 series. You don't (normaly) need to check for start/stop condtions, the way I implemented it was as a very simple state machine. If the SSPSTAT.DA bit is showing that the buffer is holding an address,=20 then use the R/W bit to set an appropriate state. The following is the ISR=20 from some code I wrote a few years ago which worked perfectly. Regards Mike if(SSPIF && SSPIE) { if(SSPOV) // buffer overflow { SSPOV =3D 0; I2cState =3D COMMAND=5FERR; return; } if(STAT=5FDA =3D=3D I2C=5FADDRESS) /* first check if we have an address */ I2cState =3D ADDRESS; /* if so initialise the state machine */ switch (I2cState) { case ADDRESS: /* SSP has just been addressed */ Temp =3D SSPBUF; /* clear Buffer Full flag */ if(STAT=5FRW =3D=3D TRUE) /* master wants to read data... */ { I2cState =3D SEND; /* so state changes to "SEND" */ goto xmit; /* this is a bit rough, but it reduces duplication */ } else { RxBuffPtr =3D 0; /* reset comms buffers */ TxBuffPtr =3D 0; I2cState =3D COMMAND; /* first byte of data is a command */ } break; case COMMAND: /* first byte received is a command */ if(STAT=5FBF) /* data in SSP buffer? */ { if(ValidCommand(SSPBUF)) /* ensure the command is valid */ { I2cCommand =3D SSPBUF; /* if so add to the buffer */ CommandStack(); /* process the command */ I2cState =3D RECIEVE; /* change to recieve state */ } else /* command is not valid */ { I2cState =3D COMMAND=5FERR;= =20 /* do nothing until re-addressed */ } } break; case RECIEVE: /* more data to be received */ if(STAT=5FBF) /* is SSP buffer full? */ { if(RxBuffPtr < RX=5FBUFFER=5FMAX ) /* check for end of buffer */ RxBuff[RxBuffPtr++] =3D SSPBUF; /* if ok, add data to next location */ CommandStack(); /* process command */ } break; =20 case SEND: xmit: if (!STAT=5FBF && !CKP) /* Ensure buffer = is empty and SCL is disabled */ { if(TxBuffPtr > 0) /* check for remaining data to send */ { SSPBUF =3D TxBuff[TxBuffPtr-1]; /* get next byte, and place in MSP buffer */ TxBuffPtr--; /* point to next byte to send */ } else { /* shouldn't be here. Master incorrectly ACKed last byte */ SSPBUF =3D 0xFF; /* send a dummy byte of 0xFF to avoid locking SDA low */ } CKP =3D 1; /* enable SCL */ } break; case COMMAND=5FERR: /* "do nothing" state */ Temp =3D SSPBUF; /* just=20 clear buffer */ TxBuffPtr =3D 0; /* invalid = command sent so ensure nothing sent back */ break; } SSPIF =3D 0; // clear the interrupt flag } } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Any questions about Bookham's E-Mail service should be directed to=20 postmaster@bookham.com. -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads