Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Finished Project: RS485 network 1-Master 8-Slaves

Status
Not open for further replies.

SPDCHK

Member
After what feels like forever I finally finished a basic RS485 Master/Slave network controller. The bus is a half duplex, asynchronous 2 wire bus, making use of twisted pair wire medium. The current design allows for a maximum of 8 slave network nodes.

rs485-png.49989


For the master I used the 28 pin 16F876A PIC. I kept the design to a maximum of 8 network nodes, because all the commands and status feedbacks to/from the slave nodes will be done via hardwired switches (for commands to the slaves) and LED’s (for status indications from the slaves). I used all eight bits on PORTC to drive status LED’s and all the bits on PORTB are wired to switches that will be used to command the respective nodes. The next design revision will have the commands and status feedback signals multiplexed, but that’s for another time.

I initially wanted to make use of the USART features in most PIC’s, BUT because I only planned on using a two wire bus system, I had to let go of the full duplex USART function. (A full duplex system will in any case require two RS485 line drivers). Instead I wrote (well, I borrowed code from ) or rather modified Nigel’s code to my needs. I had to change the way RS232 normally works (i.e. having a “space” - logic 0) to start the Rx/Tx process. Normally, if RS232 is idle, the bus is kept at a “mark” level (Logic 1). This will cause problems when all slave nodes are driving the bus high, and the master wants to transmit logic 0. Hence the need to change the start and stop bits from logic 0 to logic 1. Now, in idle state, nothing is transmitted on the bus. The receiving process now only starts when a logic level “1” is received on the bus.

The master starts by addressing Node0 first. The status of Node1 switch is merged into the transmitting register with the node number. This register is then transmitted onto the RS485 bus. Node0 should be the only slave node on the bus to reply. Once the master receives the reply back from Node0, it then increments the Node Number count, and Node1 becomes the next target. This process repeats up to Node8, where the node numbers are reset to zero, and the whole process repeats over and over.

I used the first three bits of the byte to store the node number in. I use the very last bit of the byte to store the command that will be issued to the respective node. If the master requires the node to switch on it’s output device (i.e. relay, LED etc.) the logic level of bit<7> will be high, and visa versa for switching off the device.

The slave node is built around the 8 pin 12F675 PIC. One I/O point is used for the command signal, received from the master and one I/O pin is used as status feedback back to the master. When the node receives the full byte, it goes through a decoding sequence to make sure that the address received in the byte identifies the current node. If the received node address is not the same as the pre-programmed address in the slave node, that node will not reply any data back onto the bus, and will simply keep listening until the next round of transmitted bus data comes around.

If however, the address does match, the slave node will then reply to the master the node’s address again, and the state of the node’s input switch. This switch can be a direct mechanical link to the output device (i.e. a contact on a relay) or it can be a secondary feedback condition, having no relation to the output device. A slave node will only send data back to the master if the slave node was polled by the master. Any change in the slave node’s switch status will not interrupt the bus to then transmit the change of state.

The bus communication protocol runs basically at 9600 BAUD. That’s about 100us for each bit in the byte to be sent/received. It takes a total time of about 1ms to transmit/receive one full byte, with one start and one stop bit. At this rate, each node will be addressed around 63 times per second (8 nodes). This refresh rate should be enough to pick up any state changes in any slave node switches to give a real time view.

I include schematic drawings of the maste and the slave node, as well as the asm source code files for the master and the 8 nodes. All the slave nodes source code files are basically the same with the exception of the Node Number address permanently programmed into each slave node.

Hope this may be of some use to somebody.

Regards
 

Attachments

  • 1on8.zip
    38.1 KB · Views: 5,013
  • RS485Bus.pdf
    14.1 KB · Views: 4,605
  • Master.pdf
    27.4 KB · Views: 4,793
  • SlaveNode.pdf
    16.7 KB · Views: 4,022
  • RS485.PNG
    RS485.PNG
    19 KB · Views: 28,767
Thanks very much SPDCHK for your post. I've been searching for a sample code of RS458 that will aid me in a design that I have working on. This is a great starting point for me. In fact the early hours of this of this morning, I did a simulated design of Proteus Software and the schamatics and code worked well. I hope to upload the DSN file soon.
 
This is my ISIS design file I used for testing
 

Attachments

  • ISIS 8on1.zip
    22.3 KB · Views: 2,515
Very nice project. Did you find the RS485 didn't need bias resistors?

Since RS485 is unidirectional when I did it with a software UART I only needed two pins. TX & RX can be tied together but you have to remember to change the port direction.
 
Last edited:
Very nice project. Did you find the RS485 didn't need bias resistors?
On the simulation I did not need the resistors, but on the prototype boards I actually did need the resistors. I used 120Ω resistors. Without the resistors I had lot's of false readings, but with the resistors it all cleared.

Since RS485 is unidirectional when I did it with a software UART I only needed two pins. TX & RX can be tied together but you have to remember to change the port direction.
Do I understand you correctly? You used the Rx & Tx pins of the UART of a PIC, of which you used one of then to change the I/O direction of the SN75176 RS485 driver chip?
 
The 120 is a terminator isn't it? Often each node will have a pair of bias resistors. See R.E.Smith Serial Communications RS485 for examples.
Yes, you are correct. In my application I only tested to about 30m, but in real life, the installation rules must be adhered to. The same philosophy we follow with the Profibus DP installations we use for SIEMENS PLC applications.

It wont work with a hardware UART, but since the 12F675 has no UART you can use the same pin for RX & TX and an output pin connected to DE & RE
Ah! now I understand you. That's a good idea.

Just a pity I'm currently busy with commissioning. I'd like to take the whole design further and include basic I/O modules to the slave node stations, but that's for later this year.

Regards
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top