Electronic – Communicating multiple PICs with one PC

picrs232

I've got a task in which I need to connect 5 or more ucontrollers to one PC for the purpose of sending data which will be stored in the PC. The conditions are as follows:

  • The ucs in question are PIC 16F877As; each one of them is part of a system that keeps track of the number of screws used(as of now), powered by constant DCV from plugs so power is not an issue.
  • the data being sent are just numbers; the current number of screws used
  • the environment is that of a factory assembly line; the screw counters are used in the line and the env is generally noisy
  • the data received by the PC is to be stored in a table; I figured i can take care of this part later
  • the distance between each PIC is about 2-3 meters; the PC is at the end of the line, about 10 meters, the link between the PIC and PC can either be physical or wireless, although I prefer wireless as it is more hassle free(I think…), although robustness of data sent is priority
  • as usual, the system must be made to be as cheap as possible without sacrificing reliability

I've successfully connected a PIC to PC using RS-232 so I know enough that you can't easily connect all 5 of the PICs directly to one PC using RS; too troublesome and distance issues. What I'm thinking is something like a hub; the 5 PICs connect to a master PIC which in return gets whatever data from the 5 PICs and sends them to the PC. I've read some stuff about I2C and I think that's doable enough. I've also looked up on wireless solutions like XBee; I got SKKCA from Cytron but I dunno how to make it handle many-to-one data communications.

Anyone got better ideas on how am I can pull this off in the least painful, cheap way possible? This entire project is a one-man show so I'd rather keep things simple and inexpensive.

Best Answer

I don't know how stuck you are on the 16F877A, but that's a poor choice for something like this. Think of the 16 series as being only for special situations, like high volume where a little lower price matters, extra low power, or small physical size. You have none of these issues, and the 16F877A is a old general purpose part anyway.

I would use something like a 18F4580, which has a CAN transceiver built in. Your application is just crying out for CAN. It is a differential bus, so has good noise immunity. Your distances can easily be handled even at the maximum rate of 1 Mbit/s. RS-485 as others have mentioned is differential too, but it stops at the electrical spec. You have to design your own protocol on top of that, considering collisions, retries, arbitration, bit errors, etc. This is of course doable, but there are a lot more little gotchas that aren't probably apparent to you at first glance. There are a lot ways to get this wrong, and most people do, at least the first time.

With CAN, all that is defined in the standard and implemented in the hardware. You send a message, and it just shows up at the other nodes. Multiple nodes trying to transmit at the same time is handled automatically. there is a defined arbitration scheme that the hardware CAN peripherals implement, with automatic retry until the message gets thru. Each message also contains a 16 bit CRC, which is again generated and checked in the hardware.

It will take a little effort to learn CAN and the CAN peripheral, but this will be less than doing your own protocol on RS-485, at least if you do it right. Besides, CAN is a good thing to learn whereas RS-485 is a legacy from a bygone era.

If you can't use a PIC with CAN built in (although there are some with the same footprint as the archaic 16F877A), you can use the external CAN chip that talks to the PIC over SPI. Our free PIC Development Tools release at http://www.embedinc.com/pic/dload.htm includes source code both for driving the external CAN chip and the internal CAN peripheral of a 18F4580.

You will need something that allows the PC to communicate with the CAN bus, but such things are available off the shelf. We have our own USB to CAN adapter which hasn't made it to a product yet, but I'd be willing to publish the design and the source code for it. If I remember right, National Instruments is one of the companies that makes off the shelf CAN adapters for a PC.