Creating RS-485 network with atmel mcu's

Thread Starter

Zotto

Joined Mar 3, 2016
45
Hey all, I am working with my first RS-485 network, using ATXMega256A3U as the lone master in the system, communicating to ATmega slave devices. The slaves are connected to external peripherals, which the MCU will control, then send data back to the master, when requested. So far, I have found a basic RS485 driver IC from Maxim (https://datasheets.maximintegrated.com/en/ds/MAX1487-MAX491.pdf), but was wondering if someone with more experience would be able to give me some advice/point me in the right direction towards some literature or good examples of creating a proper system/protocol. I appreciate any advice/suggestions. Thanks.
 

MrChips

Joined Oct 2, 2009
34,719
RS-485 is a physical layer.
One of the forerunners of RS-485 transceiver was the 8-pin DS75176B.


Since then, many manufactures have followed suit with there own products, fMAX481/3/5/7 or example, with different power and slew characteristics.




There is no standard protocol. You are free to choose or develop your own based on your own needs, e.g. simplicity, throughput, reliability, etc.
I have created my own protocols and have been doing so for many years.

MODBUS is a good starting point for designing your own protocol.
 

Papabravo

Joined Feb 24, 2006
22,075
The most important consideration from a protocol standpoint is that a 2-wire physical interface means all data transmission is half duplex. RS-232 is inherently full-duplex. You can make a 4-wire RS-485 interface for full duplex but you need to be careful to prevent two transmitters from talking at the same time. Here the big problem is when to disable the transmitter and listen. The answer is some amount of time after the last stop bit of the last transmitted byte has been completely sent and received. Don't underestimate the advantage of using 2-stop bits for some extra margin between successive transmissions.
 

Picbuster

Joined Dec 2, 2013
1,057
The node is always in listening mode.
The master will address a node and wait for an answer.
The addressed node will not answer within predefined time frame ==>> repeat n times still no answer remove node from polling list and create error msg.

Answer arrives ==> checksum false ==>> repeat N times still wrong remove from polling list and create error msg.

Al Ok But------- all information is also received by all nodes ==>swing all data away when not addressed.

The baud rate used, turnaround delay and N will effect the throughput. Be aware of that when controlling time critical processes.
Take for turn around delay the practical value of 3 or 4 transmission bytes ( 1 start 8 data 1or 2 stop bits)
I assume that you are using half duplex async communication.

Update polling list after T sec with the non responding nodes allowing them to join in after reconnect or recovered from error state.

Picbuster.
 

MrChips

Joined Oct 2, 2009
34,719
One of the issues with designing any fail-safe system is what to do when an error is detected.

Your system will be 99.99% error free. It is that rare error that will bloat the system design by 1000%.

Keep it simple.
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
@MrChips would you be able to show me an example of a half-duplex rs485 system using modbus RTU. Like the software implementation. I don't quite understand how to use modbus RTU to create slaves. How to create coils and hold registers in software?

So my system will be a master ATXmega device, communicating with up to 10 other atmel mcu's. I can do that with a single UART line into the RS-485 line driver (MAX485 for instance) right? Then connect the output of the MAX485 to each of the slave MCU's UART lines. I dont quite understand how the actual PCB routing would be for that daisy chain, because I can only imagine that I would be able to create stubs on the line, which I understand can issues with the transmission line impedance.
 

MrChips

Joined Oct 2, 2009
34,719
You don't have to implement Modbus exactly. What I will describe is a derivative of Modbus ASCII mode.

Firstly, you choose the serial protocol. Here is a starting point.
7-bits
Even Parity
Two stop-bits
9600 baud
No flow control

Here is the master protocol:

[start] [slave address] [command] [data] [checksum] [stop]

where,
[start] - single byte, '$', 0x24
[slave address] - single character, '0-9,A-Z', where '0' is used for broadcast purposes
[command] - two character, 'A-Z,0-9', for example, 'RD' stands for READ.
[data] - any number of readable text characters
[checksum] - two character, '0-9, A-F'
[stop] - single byte, CR, 0x0D

[start] - single byte, '*', 0x2A is substituted when the slave responds.

Notes:
All characters are readable text for ease of debugging.

[checksum] is mod 256 sum of all characters (7-bit only) starting with the [start] and not including the [checksum] and [end] fields.
[checksum] characters are 0-9 and A-F (uppercase) and appear most significant nybble followed by least significant nybble.
[checksum] characters can be replaced with ?? for testing.

Example of setting an 8-bit port on device #1:

$1OBFF??[CR]

Response from #1:

*1OK??[CR]

where the ?? above can remain as ?? for testing or correct checksum characters inserted.
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
@MrChips

Hi, I just have a few more questions. When I am connecting the master MCU to 3 slaves, for instance, how many MAX485 IC's would I have? Would it be 1 driver for the master, then 3 (1 for each slave)? Then I would have the masters A and B lines daisy chained to all of the slaves, right? So in this case I would only need 1 GPIO to control the Driver/Recieve enable pins on the MAX485, and the slaves would be connected to the masters A and B line, and the then I would connect the DI and RO lines to the TX and RX of that MCU respectively?. This is for 2 way half-duplex communication, I am thinking about this correctly?

Also, I have the master MCU connected to these slaves via breakout PCB's, what signals should carry over from the main board to the breakout. Obviously the A and B lines from the master go to the breakouts, but in terms of power and ground signals. Do I have a single ground connection between the boards, or for each slave (3 in this case), should I send a power/ground signal connection. To word this better, if I am creating headers to connect from a main board to a breakout board, should it have:
1) 2 pins (A and B)
2) 3 pins (A and B and ground)
3) 4 pins (A and B, power and ground)

Or something else? I appreciate any help!
 
Last edited:

MrChips

Joined Oct 2, 2009
34,719
In essence, that is correct.

Just consider each node, master or slave, as one device.
Each device would contain one MAX485 transceiver.

Correct. DI and RO connect to TX and RX.
One GPIO line connects to DE/RE for control.

RS-485 A and B lines are connected as party lines, i.e. all A lines connected, all B lines connect.

What is the longest distance between any two devices?

You will also need terminating resistors across A and B lines at the two extremities of the network.
In addition, two bias resistors are required, one pull-up between A and Vcc, and one pull-down between B and GND. These can be placed at the master node.
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
In essence, that is correct.

Just consider each node, master or slave, as one device.
Each device would contain one MAX485 transceiver.

Correct. DI and RO connect to TX and RX.
One GPIO line connects to DE/RE for control.

RS-485 A and B lines are connected as party lines, i.e. all A lines connected, all B lines connect.

What is the longest distance between any two devices?

You will also need terminating resistors across A and B lines at the two extremities of the network.
In addition, two bias resistors are required, one pull-up between A and Vcc, and one pull-down between B and GND. These can be placed at the master node.
Longest run should be no longer than 1-2 feet.

But now I am confused again, I'm sorry, right now I am trying to figure out how I am going to wire these boards. If I have the 3, for instance, and the main board connects to the 3 breakouts, where do I put the MAX485's, on the mainboard or the breakouts? I have also seen 680 ohm resistors in addition to the termination resistors, and the one's that you just described, what are the purpose of these 680 ohm resistors?

Also, should I have the slaves be responsible for controlling their enable pins, or let the master control when the slaves can talk? Just curious, in term of good design/convention.

Thanks again for all the help!
 
Last edited:

MrChips

Joined Oct 2, 2009
34,719
Longest run should be no longer than 1-2 feet.

But now I am confused again, I'm sorry, right now I am trying to figure out how I am going to wire these boards. If I have the 3, for instance, and the main board connects to the 3 breakouts, where do I put the MAX485's, on the mainboard or the breakouts? I have also seen 680 ohm resistors in addition to the termination resistors, and the one's that you just described, what are the purpose of these 680 ohm resistors?

Also, should I have the slaves be responsible for controlling their enable pins, or let the master control when the slaves can talk? Just curious, in term of good design/convention.

Thanks again for all the help!
Treat each node/device as being independent, regardless of whether it is a master or slave.
Each device connects to the network cable via a short stub (no more than 10 inches or there about) to the A and B lines. The exception is the device at each end of the line. That is, the two ends of the line will be terminated with a proper terminating resistor.

Each device will have its own MAX485, that is, consider each daughter board as one independent device. Each MCU will control its own DE/RE.

I don't have the recommended values here with me but for such a short run (3 feet or less), 330Ω terminators will do. For now, use 1kΩ pullup and pulldown resistors.

For such a short run, why do you need RS-485?

You can connect all TX and RX UART lines together to form a single party line provided each MCU is able to disable its TX driver.
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
Treat each node/device as being independent, regardless of whether it is a master or slave.
Each device connects to the network cable via a short stub (no more than 10 inches or there about) to the A and B lines. The exception is the device at each end of the line. That is, the two ends of the line will be terminated with a proper terminating resistor.

Each device will have its own MAX485, that is, consider each daughter board as one independent device. Each MCU will control its own DE/RE.

I don't have the recommended values here with me but for such a short run (3 feet or less), 330Ω terminators will do. For now, use 1kΩ pullup and pulldown resistors.

For such a short run, why do you need RS-485?

You can connect all TX and RX UART lines together to form a single party line provided each MCU is able to disable its TX driver.

I like the differential pairs reducing common mode noise, but If i were to just use UART, how would the topology change? How would I account for bus contention issues, I think you touched upon it with disabling the TX driver, but Im not quite sure how to implement this conceptually.

Also RS485 allows for many slaves right? I may have up to 5-10 or so in my final design.
 

MrChips

Joined Oct 2, 2009
34,719
The advantage of RS-485 is it is designed for networks over long distances and provides a reliable means of communication over harsh electrical environments.

Over much shorter distances, SPI and I2C are suitable hardware level layer.

Bus contention only occurs when you have multiple masters, i.e. peer-to-peer networks.
In a master-slave network, there is only one master. The master initiates communication and only the addressed slave device is allowed to respond.
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
The advantage of RS-485 is it is designed for networks over long distances and provides a reliable means of communication over harsh electrical environments.

Over much shorter distances, SPI and I2C are suitable hardware level layer.

Bus contention only occurs when you have multiple masters, i.e. peer-to-peer networks.
In a master-slave network, there is only one master. The master initiates communication and only the addressed slave device is allowed to respond.
Im building a robot, that is supposed to operate in semi-harsh terrain (mars rover type), do you think I should proceed with a faster communication protocol like SPI? Or would the RS-485 be better. I don't quite know what my noise will be like, but how resistant to noise is SPI?
 

MrChips

Joined Oct 2, 2009
34,719
SPI is not necessarily faster.
The deciding factor will be the length of the runs (also limited by the data rate).
SPI needs four wires, MOSI, MISO, SS, and SCLK.

UART uses one wire TX/RX for half-duplex communication.

There is also 1-Wire communication.
What data rate are you aiming to achieve?
 

Thread Starter

Zotto

Joined Mar 3, 2016
45
SPI is not necessarily faster.
The deciding factor will be the length of the runs (also limited by the data rate).
SPI needs four wires, MOSI, MISO, SS, and SCLK.

UART uses one wire TX/RX for half-duplex communication.

There is also 1-Wire communication.
What data rate are you aiming to achieve?
Ideally at least 100kbps, but I am also interfacing with a radio, that will be communicating to a base station, while that communication is separate (the base station is sending movement commands), I don't know how to interface to the radio using SPI or any other protocol besides UART/RS485. But I haven't searched enough. Do you have any experience purchasing robust radio's (900 MHz or 2.4 GHz band)
 

MrChips

Joined Oct 2, 2009
34,719
Design your inter-board network depending on the majority of the needs of your devices.
Treat the radio as a special case. You can create a communications link with the radio based on its requirements.
Until you have concrete specifications of the radio on hand there is no point in speculations.
 
Top