Anyway, just as blinking a LED,Â i2c is a must-have protocol. Several links helped me a lot, likeÂ this oneÂ explaining the whole theory, step-by-step, message-by-message. But the most helpful documentation still remainsÂ PIC 16F88 datasheet, and theÂ Application Note AN734AÂ which explains quite well how to implement an i2c slave as aÂ state-machine. AnÂ interesting postÂ is also explaining this very same App. note has a lot of bugs. I wouldn’t say “a lot”, still there’s a little bug about clock stretching:Â CKP must be set to high when receiving a NACK. Anyway…
So, this time, this is about setting anÂ i2c communication busÂ between twoÂ PIC 16F88, usingÂ Jal v2. The first thing to note isÂ 16F88 implements SSPÂ (Synchronous Serial Port), butÂ not MSSPÂ (Master Synchronous Serial Port). This meansÂ 16F88 can’t be just configured to act as a i2c master, all of this must be done in software. Good news is jal v2 standard libraries come with an i2c module, which can handle all the i2c protocol subtleties, from a master point-of-view…
This jal v2 master implementation isn’t based on interrupts: since it’s the master which decides to take control of the bus, there’s no need to react to external event… except when usingÂ Multi-Master i2c bus. To help this, 16F88 can be configured to generate interrupts on START/STOP bits (which defines when the bus is available or not). A Multi-Master i2c bus seems great but I didn’t give a try, regarding all the debugging time it took me to set a “simple” 1-Master/1-Slave bus.
So, the master part is ok, now the slave part. This is where things are getting tough. Particularly without any oscilloscope or digital probe/analyzer.Â 16F88 can be configured as a hardware slave i2c. An IC address must be set, it must be the same as the one the master is using… This is what I’ve first thought, but it’s not actuallyÂ exactlyÂ true ! Actually,Â SSP must be configured so SSPADD contains a 8-bit address, that is, with the 8th bit setting read/write address type.Â Whereas in Jal, IC address is coded on 7-bits.Â This means, for instance:
Jal slave i2c master
(declared slave address)
|Hardware i2c slave address|
|0x2E = 0b00101110||0x5C = 0b1011100|
Setting a i2c bus is quite easy, from a hardware point of view.Â SDA and SCL pins must be connected on each side, on a bus using pull-upÂ resistors to +5V. Different values can be used, they’re determining the stability and the bus speed. I’ve successfully tried 2.2K and 3.3K, these are standard values.
|PIC 16F88 i2c Master||PIC 16F88 i2c Slave|