RS485 Communication using MAX485 and MSP430 Launchpad

Pinterest icon

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).

MSP430G2553 interfaced with MAX485 circuit diagram

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

link to xanthium enterprises repo containing Serial Programming Code using Win32 API on Github

All the Embedded C sourefiles used for RS485 communication can be  downloaded from our GitHub Page.If you are new to Github Check this article to download code .

Circuit diagram for the tutorial

Circuit Diagram for interfacing MSP430 Launchpad with MAX485 Chip can be found here.

 

Hardware Used 

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 

Buy USB to RS485 converter online from here   Buy USB to RS485 converter online from here (made in India )

Robot Shield - A booster Pack for MSP430 Launchpad that can be used for controlling DC motors as well a providing RS485 communication.More details 

Buy MSP430 Motor Control Booster Pack from here  Buy MSP430 Launchpad Shield for Motor Control from here

Circuit Description
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). 

how to take 5V VCC from msp430 launchpad

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.

Photo of MSP430G2553 interfaced with MAX485 RS485 tranceiver

If you already have the MSP430 Robot Shield you can use the board's Built in RS485 section instead of building the above circuit.

msp430 booster pack buy in india for rs485 communication  

Software
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 ----//

#include "msp430g2553.h"
void main(void)
{
   WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog
     
   //------------------- Configure the Clocks -------------------//
        
   if (CALBC1_1MHZ==0xFF)   // If calibration constant erased
      {    
        while(1);          // do not load, trap CPU!!
      } 

   DCOCTL  = 0;             // Select lowest DCOx and MODx settings
   BCSCTL1 = CALBC1_1MHZ;   // Set range
   DCOCTL  = CALDCO_1MHZ;   // Set DCO step + modulation 
      
   //---------------- Configuring the LED's ----------------------//

   P1DIR  |=  BIT0;  // P1.0 output
   P1OUT  &= ~BIT0;  // P1.0 = 0

   //------------ Configuring MAX485 Control Lines ---------------//
      
   P2DIR  |= BIT0 + BIT1; // P2.0 -> DE,P2.1 -> ~RE output
   P2OUT   = 0x00;        // P2.0 and P2.1 = 0
      
   //--------- Setting the UART function for P1.1 & P1.2 ---------//
      
   P1SEL  |=  BIT1 + BIT2;              // P1.1 UCA0RXD input
   P1SEL2 |=  BIT1 + BIT2;              // P1.2 UCA0TXD output
      
   //------------ Configuring the UART(USCI_A0) ----------------//
      
   UCA0CTL1 |=  UCSSEL_2 + UCSWRST;  // USCI Clock = SMCLK,USCI_A0 disabled
   UCA0BR0   =  104;                 // 104 From datasheet table-  
   UCA0BR1   =  0;                   // -selects baudrate =9600,clk = SMCLK
   UCA0MCTL  =  UCBRS_1;             // Modulation value = 1 from datasheet
   UCA0CTL1 &= ~UCSWRST;             // Clear UCSWRST to enable USCI_A0
      
   //----------------- Put MAX485 in Transmit Mode ---------------//
      
   P2OUT |= BIT0;  // P2.0 ->  DE high
   P2OUT |= BIT1;  // P2.1 -> ~RE high
      
   UCA0TXBUF = 'A';                  // Transmit a byte 
      
   _BIS_SR(LPM0_bits + GIE);         // Going to LPM0
  }

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 ----//

void main(void)
{
   WDTCTL = WDTPW + WDTHOLD; //stop WDT

   // Configure Port pins //
   // Configure UART pins // 
   // Configure UART      //

   //---------------- Enabling the interrupts ------------------//
   
   IE2 |= UCA0RXIE;                  // Enable the receive interrupt
   _BIS_SR(GIE);                     // Enable the global interrupt
      
   //----------------- Put MAX485 in Receive Mode ---------------//
 
   P2OUT &= ~BIT0;  // P2.0 ->  DE Low
   P2OUT &= ~BIT1;  // P2.1 -> ~RE Low
      
   _BIS_SR(LPM0_bits + GIE); // Going to LPM0 and wait for data reception
}

//---------------------------------------------------//
//             Receive  interrupt                    //
//---------------------------------------------------//

#pragma vector = USCIAB0RX_VECTOR
__interrupt void ReceiveInterrupt(void)
{
  P1OUT  ^= BIT0;     // light up P1.0 Led on RX
  IFG2 &= ~UCA0RXIFG; // Clear RX flag 
}

 

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.  

interfacing a msp430g2553 launchpad with PC through RS 485 protocol

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.

USB 2SERIAL -USB to RS485  converter interfaced with launchpad board

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.

Here is another link for developing RS485 software using C# on Linux and Windows.

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 :-).

 a c program for receiving a string of characters through RS485 network running on Windows 7

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.

MSP430 launch pad communicating with another MSP430 launchpad through RS485 protocol

RS485 network can support multiple receiver nodes (MSP430 + MAX485).In the above figure i am using only a single receiver.