RS485 Communication using MAX485 and MSP430 Launchpad
In the previous tutorial we discussed how to implement a serial connection between MSP430 launchpad (MSP430G2553 ) and a PC .Serial(TTL) or RS232 protocol is fine if you are planning to communicate over short distances of a few meters,when you increase the range to a few 100 meters RS232 protocol will become inadequate.For long distance wired communication you have to use a different protocol called RS485 which can communicate over a kilometer.In this tutorial we will interface a MSP430 launchpad containing MSP430G2553 with a RS485 decoder chip from Maxim (MAX 485).
The above circuit shows how to interface the MAX485 RS485 chip with MSP430G2553 on Launchpad.Please note that only the relevant pins of MSP430 are shown in the above circuit.If you are using the launchpad ,capacitors C1 and C3 are already present on the (launchpad) board, you don't have to assemble them.
On the other hand if you are building a complete standalone circuit ( MSP430G2553 + MAX485 ),you have to include the capacitors C1 and C3 ,RESET pin circuit for MSP430(not shown in above figure) and programming connector for MSP430(not shown).You can check out the entire circuit (PDF file) in the zip file.
Source Code and Circuit Diagram
Circuit Diagram for interfacing MSP430 Launchpad with MAX485 Chip can be found here.
USB2SERIAL - An easy to use USB to RS485/RS232/Serial converter that is used to add a RS485 to the Linux or Windows PC .More Details
Robot Shield - A booster Pack for MSP430 Launchpad that can be used for controlling DC motors as well a providing RS485 communication.More details
The UART pins of MSP430G2553 are connected to the MAX485 chip while Pins P2.0 and P2.1 are used to select the receive and transmit modes of the MAX485.
Unlike the previous serial communication tutorial,there is no need to interchange the TX and RX pins.The UCA0RXD pin of MSP430 is connected to the RO (Receiver Output) pin of MAX485 and UCA0TXD pin of MSP430 is connected to DO (Driver input) pin of MAX485.
P2.0 is connected to DE (Driver enable) pin of MAX485 which is used to put the chip in Transmit Mode.P2.1 is connected to ~RE pin (Active Low) of MAX485 which is used to put the chip in Receive mode.
Please note that MSP430 operates from a 3 to 3.3 V while MAX485 requires 5V DC.On the Launchpad board there is no 5V output,you can take the 5V output by soldering a wire to the USB connector(use a multimeter to identify the 5V DC pin from ground).
You can wire up the RS485 section on a small general purpose PCB and connect with the Launchpad board using bergstrip connectors.You can see the photo of my board below.
If you already have the MSP430 Robot Shield you can use the board's Built in RS485 section instead of building the above circuit.
The code for transmitting or receiving a character is quite simple and is basically the code for asynchronous serial communication with a few additional lines.MSP430 configures its UART to transmit or receive at the selected baudrate.P2.0 and P2.1 pins are used to put the MAX485 in either transmit or receive modes.MAX485 is a half duplex RS485 tranceiver which means that it can only transmit or receive at a time,not both at the same time.
Transmitting a Byte
For transmitting a byte using MAX485,MSP430 makes the DE (Driver Enable) and the ~RE (Receive Enable) pin high and transmit the bytes to the DO pins of MAX485.Making DE high would put the MAX485 in transmit mode while making the ~RE pin high would inhibit the receive mode.
//---- Program to Transmit a byte through RS485 network ----//
DCOCTL = 0; // Select lowest DCOx and MODx settings
P1DIR |= BIT0; // P1.0 output
//------------ Configuring MAX485 Control Lines ---------------//
Receiving a Byte
In data reception,MSP430G2553 puts the MAX485 in receive mode by making the ~RE pin low.DE is also made low to inhibit the transmit mode.MSP430G2553 remains in LPM0 until a character is received in the UCA0RXBUF and the receive interrupt toggles LED connected to P1.0.Please note that only the changes are shown in the code given below,full code is available in the zip file.
//---- Program to Receive a byte through RS485 network ----//
// Configure Port pins //
//---------------- Enabling the interrupts ------------------//
#pragma vector = USCIAB0RX_VECTOR
Testing the RS485 Transmission
One way to test the RS485 transmission is to connect your launchpad with your PC using a USB to RS485 converter like USB2SERIAL.USB2SERIAL is a cross platform (windows /Linux) USB to RS485 converter that can be used to control an RS485 network.Sample Programs for controlling the RS485 network in C (Win32 API/Linux API),C#(.NET Framework) and Python are available for download.If you are interested you can purchase the board here.The bottom figure shows how the USB2SERIAL board can be used to interface a MSP430 launchpad through a RS 485 network.Even though the below figure shows a single MSP430 board, you can also add a multiple number of MSP430 nodes to the network.
When connecting the RS485 twisted wire make sure that Pin A of MAX485 (on the launchpad side) is connected to PinA of MAX485 on the USB2SERIAL board.Also please make sure that jumper settings on the USB2SERIAL board are set to the RS485 mode (USB2SERIAL can also to USB to Serial/RS232 conversion) and~RTS and ~DTR lines should be used to control the DE and ~RE pins of MAX485.The below picture shows USB2SERIAL interfaced with Launchpad.
PC Side Software
On the PC side you would require some sort of software to control the RS 485 network you just created.Developing the code for RS 485 communication is quite easy you can check out the this General Purpose tutorial to develop code in Windows or Linux system using either C ,C sharp(.NET Framework) or Python languages.If you are new to programming ,I would recommend Python for developing PC side software.
The bottom figure shows the screenshot on a windows machine showing the data ("Hello From MSP430") received from the Launchpad.I have modified the string,just sending 'A' didnt look good :-).
Another way you can test the RS485 network is to use two launchpad boards along with their MAX485 modules and configure one launchpad board as transmitter and another as receiver.The below block diagram shows the connections.
RS485 network can support multiple receiver nodes (MSP430 + MAX485).In the above figure i am using only a single receiver.