If you are going to use two wires, then you might as well use software RS-232 in it's open-collector, idle-high incantation. The best reason to use this protocol is the large amount of support already in PBP. RS-232 open-collector operation gives the advantages of I2C without the hassle.
In my network, I send out the slave's address, plus a command byte, plus a checksum.
When the slave decodes it's address, it sends back the slave address, the data and a checksum.
It very reliably "talks" at 38.4Kbaud across a large network. The Master is a 18F8723 running at 40MHz, but all the slaves are 18F2221's running on their internal 8MHz oscillators ! If you have a chip with a less stable oscillator, you can just run at a slower baud rate.
An additional benefit of doing things this way is that you can use the hardware receiver shift register (HSERIN). This gives you a two-byte buffer and an easy interrupt source.
Because of the interrupt and the buffer, the slave never misses any data and can do other tasks while waiting for a poll. On the slave side, you can't use HSEROUT (at least without a hardware buffer) because it isn't open-collector, but that isn't a problem.
The host can use HSERIN as well, reducing the load on that side.
I have a network "reserved" address of 253. All slave devices are initially programmed to that address. Whenever the MASTER calls that address, the command byte it sends contains the new address that I want to give the slave. That new address value is stored in the EEPOM of the slave device. The slave reads that value from EEPOM every time it powers up. I can easily program the slave address of the device that way, and no dip switches are needed. I write the device's address on a label to keep track.
Before anyone says that I'm lying about getting 38,400 baud with an 8Mhz oscillator and SEROUT2, I should admit that I'm actually using DEBUGOUT, which has lower software overhead than SEROUT2. I modified the PBP libraries to give me an open-collector output on the DEBUGOUT command.
If you don't want to modify any libraries, you can still easily get 19.2Kbaud using an 8MHz oscillator and SEROUT2.
I use 4.7K pull-ups on TRANSMIT and RECEIVE of all devices (including the host), and have no trouble across at least 25' of wire.
One final benefit: You can hook a MAX-232 to the two data lines and easily debug any communication issues with your PC.




Bookmarks