Phone

    00852-6915 1330
  • Contents

Introduction

What is RS485?

Materials

MAX485 pinout

Half duplex operation

Here is how the program works

Full duplex operation

Half duplex operation code

Full duplex code

Introduction

In digital computer communication between two computers can be made using either parallel or serial method. In parallel communication separate line is dedicated for a one-bit information to transfer. This communication is fast and easy, but it requires a lot of wires at least as many as the number of bits need to be sent in parallel. For example, to transfer a 64-bit data from one device to another, 64 data lines will be required which is impractical in embedded systems. The alternative method to transfer data is to use serial communication. In serial communication one bit at a time is transferred from one device to another one. While this method solves the wiring problem it has a lot of other problems such as bandwidth, data lagging, complex protocol, and electrical standards. There are lot of different methods to do serial communication while one method is good in one situation another one is better in another situation. In this article we will discuss RS485 communication protocol which is one of the many available serial communication methods.

Materials

1 MAX485 module
2 STM32 F401CDU6

What is RS485?

An industry specification called RS-485 outlines the physical layer and electrical interface for point-to-point electrical device communication. RS485 is the industrial standard for communication that defines the electrical interface and physical layer for point-to-point communication. RS485 is a robust communication system it can support multiple devices on a single bus, works in a noisy environment as well and requires a maximum of 4 lines.

RS485 was first developed in 1983 and has since been used in many industrial applications because of its robustness and simplicity. It has the ability to transmit data over long distances while at the same time it is cheap, thus engineers are using it in all sorts of applications such as automotive, manufacturing, and theater spaces. Nowadays almost all motor controllers, VFDs and manufacturing machines will have a port available for RS485.

RS485 is actually a standard that defines the electrical characteristics of the transmitters and receivers for communication protocols. RS482 uses two lines usually called A and B which must be balanced and differential. It means that the two lines must have same impedance, nearly same length and must be differential. The key features of RS485 communication are given below

  • Multipoint operation
  • 10 Mbps data transfer rate at 40 feet length
  • Maximum cable length is 4000 feet

RS485 works both in half duplex as well full duplex mode. In half duplex mode one device can either transmit or receive data at a time. While in full duplex mode, a device can transmit and receive data at the same time. Having more than one device on a bus can cause problem when two or more devices transmit data at the same time. Therefore, software control is necessary to ensure only one device transmit data at a time.

RS485 is the physical layer of communication in the OSI model. It means this layer can be used as a base for other protocols such as UART which in most application people use because UART is an asynchronous communication protocol that does not require any clock signal which make it very easy to use. In this article we will demonstrate how RS485 can be used between two STM32 microcontrollers to communicate and exchange data. We will be using MAX485 module which is an easily available RS485 module.

module

 

MAX485 pinout

  • RO → Receiver output
  • RE → Receiver enable
  • DE → Data enable
  • DI → Data input
  • VCC → Input voltage
  • GND → Ground
  • A, B → RS485 differential lines

Half duplex operation

half-duplex-operation

In half duplex operation either data can be received or transmitted at a time. Both operations cannot be done at the same time. MAX485 has data flow control pins called DE and RE which puts the module in receiver mode or in transmit mode. Making them low puts the module in receiving mode while making them high puts the device in transmitter mode.

In CubeMX the microcontroller of our choice is selected which in our case is STM32 F401CDU6. In connectivity UART1 should be enabled with 115200 bps baud rate. Other necessary settings are given below.

  • RCC → Crystal/Ceramic Resonator
  • SYS → Debug → Serial Wire
  • Clock Configuration → HCLK → 84 MHz
  • Clock Configuration → PLL Source Mux → HSE
  • GPIO A7 is set as output

Here is how the program works

The setup has two microcontrollers. We will call one side as A and the other side as B. When a user presses the user key on A STM32 microcontroller it will send the information to the B microcontroller via RS485. The receiving B microcontroller will switch on the onboard LED and will responds with an OK message. The OK message will blink the led on A microcontroller twice. Similarly, when the user presses key on B microcontroller it will transmit a message to A microcontroller and turns on the onboard LED and will responds with an OK message. The OK message will blink LED on B microcontroller twice. Similarly pressing the button again will do the same except this time it will turn off the LED.

Full duplex operation

full-duplex-operation

In full duplex operation data can be received or transmitted at the same time. Both operations can be done at the same time. In this mode two MAX485 modules will be required at each end and overall, 4 MAX485 modules will be used. It means that the two MAX485 modules will be constantly in receiving mode while the other two will constantly in transmission mode. MAX485 has data flow control pins called DE and RE which puts the module in receiver mode or in transmit mode. We will put the data control pins of two module as high while put the data control pins of other two module low. The configuration is shown below.

The program works the same way as it was working in the half duplex mode however, this time the transmitted and received by MCUs at the same time.

Half duplex operation code

#include "main.h"

 

UART_HandleTypeDef huart1;

 

/* USER CODE BEGIN PV */

int8_t R_Data[1] = {0};

int8_t T_Data[1] = {69};

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

 

int main(void)

{

 

  HAL_Init();

 

  SystemClock_Config();

 

  

  MX_GPIO_Init();

  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);   //Put RS485 module in receiving mode

  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);   //Turn Off LED pin

  

  while (1)

  {

    

  HAL_UART_Receive(&huart1, R_Data, 1, 10);

  // If button is pressed on the other MCU

  if(R_Data[0] == 83)

  {

  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);                //Toggle LED pin

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);    //Put RS485 module in transmission mode

  HAL_UART_Transmit(&huart1, T_Data, 1, 10);             //Send acknowledgment

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);  //Put RS485 module in transmission mode

  R_Data[0] = 0;

  }

  // If OK message is receive

  if(R_Data[0] == 69)

  {

  if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13))

  {

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  }

  else

  {

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  }

  R_Data[0] = 0;

  }

  // Button is pressed

  if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))

  {

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);    //Put RS485 module in transmission mode

  T_Data[0] = 83;

  HAL_UART_Transmit(&huart1, T_Data, 1, 10);

  T_Data[0] = 69;

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);    //Put RS485 module in Receiving mode

  }

  }

  /* USER CODE END 3 */

}

Full duplex code

#include "main.h"

 

UART_HandleTypeDef huart1;

 

/* USER CODE BEGIN PV */

int8_t R_Data[1] = {0};

int8_t T_Data[1] = {69};

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

 

int main(void)

{

 

  HAL_Init();

 

  SystemClock_Config();

 

  

  MX_GPIO_Init();

  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */

  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);   //Turn Off LED pin

  

  while (1)

  {

    

  HAL_UART_Receive(&huart1, R_Data, 1, 10);

  // If button is pressed on the other MCU

  if(R_Data[0] == 83)

  {

  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);                //Toggle LED pin

  HAL_UART_Transmit(&huart1, T_Data, 1, 10);             //Send acknowledgment

  R_Data[0] = 0;

  }

  // If OK message is receive

  if(R_Data[0] == 69)

  {

  if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13))

  {

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  }

  else

  {

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  }

  R_Data[0] = 0;

  }

  // Button is pressed

  if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))

  {

  T_Data[0] = 83;

  HAL_UART_Transmit(&huart1, T_Data, 1, 10);

  T_Data[0] = 69;

  }

  }

  /* USER CODE END 3 */

}

 

 

Victoria

Victoria is a highly accomplished technical writer with over 8 years of experience in the semiconductor electronics industry. She possesses a deep understanding of complex technical concepts and a proven ability to translate them into clear, concise, and user-friendly documentation. She is also an excellent communicator and collaborator, with the ability to work effectively with engineers, product managers, and other technical professionals.

Join our mailing list!

Be the first to know about new products, special offers, and more.

Leave a Reply

We'd love to hear from you! Feel free to share your thoughts and comments below. Rest assured, your email address will remain private.

Name *
Email *
Captcha *
Rating:

Kynix

  • How to purchase

  • Order
  • Search & Inquiry
  • Shipping & Tracking
  • Payment Methods
  • Contact Us

  • Tel: 00852-6915 1330
  • Email: info@kynix.com
  • Follow Us

authentication

Kynix

© 2008-2026 kynix.com all rights reserved.