I2C com. with two 16F88 (master/slave) using Jal v2

Weeks ago, while I was trying to interface my mainboard with a DC motor controller board, I had to dive into the i2c protocol. Serial com. couldn’t make the deal, as I plan to connect several daughter boards, thus need to address them. Although RS485 was also possible, it implied a lot of changes, particularly on the PC side where I’d need an adaptor…

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
That is, if the slave is declared to have 0x5C address, the master will have to talk to 0x2E as jalv2 is actually shifting 1-bit while building a write- or a read-address. As usual (and as everyone seems to report), i2c com. problems mainly come from address issues.

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.

Finally, here’s a two small Jal programs to test this i2c bus. The master 16F88, also connected via UART, gets a character from serial (from a PC), echoes it and send it to slave 16F88. This ones gets this char, process it (char = char + 1…). The master then gets the result and send it back to the PC. So, if you type “a”, you’ll get “a” as echo, then “b” as the slave’s results.
PIC 16F88 i2c Master PIC 16F88 i2c Slave
simple_i2c_master.jal simple_i2c_slave.jal
  1. Jef De Bruyne’s avatar

    Hi,

    I need to set up a communication between a ECIO40P (matrix multimedia) as master, and about 5 PIC16F88 IC’s as slaves.
    I already managed to write the master program (in Flowcode) and i’m shure that works. But the program ‘Flowcode’ does not support to make a slave program, so I have to white that in C wich I do not know well.

    Are you able to help me a little..?

    Greetings,
    Jef

    Reply

Reply to Jef De Bruyne Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>