The RS485 communication with Modbus protocol is mostly used in the industrial sector for its highlighting features, such as balanced line communication, multi-drop facility, good noise immunity and maximum data transmission speed. The Modbus protocol with RS485 physical line makes multi-client transactions easy and error free.
The serial protocol-based clients can be integrated to an RS485 based host system using an SPI-RS485 bridge.
The PIC16F152xx family of microcontrollers (MCUs) is equipped with the Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART), Timers and MSSP peripherals which can be used in RS485 communication and for Serial Peripheral Interface (SPI) implementation to the RS485 bridge system.
This code example highlights the integration of SPI-based client sensors and communication with host through the RS485 line by using the SPI-RS485 bridge. It demonstrates how to connect SPI clients to the RS485 network.
- SPI-RS485 Bridge: Modbus Host Emulation using PIC16F15244 Microcontroller
- PIC16F152xx Family Code Examples on GitHub
- PIC16F152xx MCU Family Video
- PIC16F15276 Product Page
- PIC16F15276 Data Sheet
The SPI-RS485 bridge example aims to integrate serial protocol (SPI) clients to the RS485 based systems.
This demonstration uses host and client systems. The host sends Modbus RTU query frames to the client to ask for the sensor data. The client receives the Modbus frames and responds to the host with the sensor data, if the received frame is valid.
The host system periodically (every 10s) creates the Modbus RTU frame with the client address, function code and address of the desired data, appending the 2-byte Cyclic Redundancy Check (CRC) to the frame and sending the Modbus query frame for reading the client holding registers.
The PIC16F15244 microcontroller serves as the host and is connected to the RS485 2 click via EUSART. It is used to convert EUSART TTL signals to RS485 standard signals, enabling communication with the RS485 bus network.
The client microcontroller receives the frame from the host and validates the CRC of received frame by calculating the CRC. If the CRC matches, then it validates the address. The client performs the required action as per the requested function code and responds to the host by preparing the response Modbus frame.
The client MCU measures the temperature with a Thermo click and the pressure with a Pressure 5 click, storing the information in the dedicated Flash memory.
The PIC16F15276 microcontroller serves as the client and is connected via SPI to the pressure sensor on the Pressure 5 click and the temperature sensor on the Thermo click. The PIC16F15276 MCU communicates with RS485 2 click using the EUSART protocol. The RS485 2 click is used to interface the SPI sensors to the RS485 bus network.
The figure below shows the SPI-RS485 bridge demo block diagram.
Figure 1. SPI-RS485 Bridge Block Diagram
Modbus is a serial communication protocol developed for usage with Programmable Logic Controllers (PLCs), as a method of transmitting information over serial lines between electronic devices. The device requesting the information is called the Modbus host and the devices supplying information are Modbus clients.
In a standard Modbus network, there is one host and up to 247 clients, each with a unique client address from 1 to 247. The host can also write information to the clients. The host Modbus frame includes the client address, function code, memory address, length of the expected data and CRC.
Host Modbus RTU Frame
- Client ID
- Function code
- Register address
- Number of registers
- CRC
Client Response Frame
- Client ID
- Function code
- Number of data bytes received
- Register data
- CRC
The second byte sent by the host is the function code. This number tells the client which table to access and whether to read from or write to the table.
The information is stored in four different tables within the client device. Two tables store on/off discrete values (coils) and two store numerical values (registers). The coils and registers each have a read-only table and read-write table. Each table has 9999 values. Each coil or contact is one bit and assigned a data address between 0x0000 and 0x270E. Each register is one word = sixteen bits = two bytes wide and has a data address between 0x0000 and 0x270E.
The total number of registers requested.
The CRC has two bytes added to the end of every Modbus frame for error detection and identifying data corruption.
Example frame
Request Frame Bytes | Description | Response Frame | Description |
---|---|---|---|
0x15 | Client ID address | 0x15 | Client ID address |
0x03 | Function code | 0x03 | Function code |
0x00 | Address of the requested register (1st byte) | 0x04 | The number of data bytes received |
0x6B | Address of the requested register (2nd byte) | 0x00 | Register value High (1st byte) |
0x00 | The total number of requested registers (1st byte) | 0x01 | Register value Low (2nd byte) |
0x02 | The total number of requested registers (2nd byte) | 0xA1 | Register value High (3rd byte) |
0xB6 | CRC for error checking (1st byte) | 0x9B | Register value Low (4th byte) |
0xC3 | CRC for error checking (2nd byte) | 0xC7, 0xC9 | CRC value High and Low (1st and 2nd byte) |
Note: Refer to modbus.org for more details on Modbus.
- MPLAB® X IDE 6.15.0 or newer
- MPLAB® XC8 Compiler 2.45.0 or newer
- MPLAB® Code Configurator (MCC) 5.3.7 or newer
- Microchip PIC16F1xxxx_DFP Device Support 1.20.366 or newer pack
- TMR0 MCC Melody driver 4.0.11
- ESUART MCC Melody driver 7.1.4
- MSSP MCC Melody driver 6.1.7
- GPIO/Pins drivers MCC Melody driver 3.5.0
- PIC16F15276 Curiosity Nano Board
- Curiosity Nano Base for Click BoardsTM
- Thermo Click
- Pressure 5 Click
- RS485 2 Click
The PIC16F15276 Curiosity Nano (CNANO) evaluation board with the RS485 2 Click, Thermo Click and Pressure 5 Click are used as the client system. The CNANO and Click boards are mounted on a Curiosity Nano base for Click boards. The following figure shows the hardware setup of the application. Visit the SPI-RS485 Bridge: Modbus Host Emulation using PIC16F15244 Microcontroller git repository for the host hardware setup.
Refer to the hardware connection details table for more information.
Figure 2. SPI-RS485 Bridge Hardware Setup
Sl No. | Microcontroller Pin | Pin Configuration | Signal Name | I/O Pin Direction |
---|---|---|---|---|
1 | RC2 | MOSI | SPI – Host Out Client In | OUT |
2 | RC5 | MISO | SPI – Host in Client Out | IN |
3 | RC6 | SCK | SPI – Clock | IN |
4 | RC7 | IO | Slave Select for Pressure 5 Click | OUT |
5 | RA6 | IO | Slave Select for Thermo Click | OUT |
6 | RD6 | IO | Pressure Click Interrupt | IN |
7 | RC0 | TX1 | TX Pin for RS485 2 Click | OUT |
8 | RC1 | RX1 | RX Pin for RS485 2 Click | IN |
9 | RB0 | TX | CDC/Debug TX Line | OUT |
10 | RB1 | RX | CDC/Debug RX Line | IN |
11 | RD5 | RE | Receive Control of RS485 | OUT |
12 | RA7 | DE | Transmit Control of RS485 | OUT |
Visit the SPI-RS485 Bridge: Modbus Host Emulation using PIC16F15244 Microcontroller for the hardware connection details of the host system.
- Connect the hardware and prepare the demonstrator setup. Follow the steps in Hardware Setup section for hardware setup.
- Configure the Data Visualizer for viewing the custom dashboard window. Follow the instructions provided in the Data Visualizer Configuration section.
- Click the Serial Port Control Panel Connect button.
- Observe the “Host Modbus RTU Frame No” field, on the first row of the custom board, to understand which frame is sent from host to client.
Figure 3. Frame Type Field in Data Visualizer Custom Dashboard Window
- Observe the “Host Modbus RTU Frame” and “Response Modbus RTU Frame” fields to understand the host query frame and client response frame.
Figure 4. Modbus RTU Frames in Data Visualizer Custom Dashboard Window
- Observe “Error Flags” fields to identify the errors when the transaction is not successful.
Figure 5. Error Flags Field in Data Visualizer Custom Dashboard Window
- Observe the temperature and pressure sensor data in the “Holding Register Data” fields for visualizing the measured sensor data received from the client holding registers.
Figure 6. Figure 6. Temperature Data in Holding Register Data Field in the Data Visualizer Custom Dashboard Window
Figure 7. Pressure Data in Holding Register Data Field in the Data Visualizer Custom Dashboard Window
The Data Visualizer tool serves as a graphical user interface during application demonstrations by displaying the host frame and the response received frame from both the host and the client. The preconfigured data streamer file and the custom dashboard file are available in the host firmware package. Follow the steps below to configure the Data Visualizer using the provided files for setup preparation.
- Open the standalone Data Visualizer. Refer to the Data Visualizer Software User’s Guide for installation and features.
Figure 8. Standalone Data Visualizer Icon
- In the Data Visualizer window, click the Configuration tab.
Figure 9. Data Visualizer Configuration Window
- In the Modules section, expand the External connection option and then double click Serial port.
Figure 10. Data Visualizer Modules Window
- From the Serial Port Control panel, select the Curiosity Virtual Com Port which is connected to the host device.
Figure 11. Data Visualizer Serial Control Panel
- Configure the data streamer for interfacing the dashboard window with the serial port control panel.
- To open the Data Stream Control Panel, open the Configuration tab located on the left side of the data visualizer tool, expand Protocols, and click the Data Streamer window that appears, as shown in Figure 12.
- Click the ‘...’ symbol in the Data Stream Control Panel window and set an appropriate path for the provided data streamer file from the host computer, as shown in Figure 12.
Figure 12. Data Visualizer Data Streamer Configure Window
- Click the Load button in the Data Streamer Control Panel, as shown in Figure 12. The Data Streamer window will load the ports, as shown in Figure 13.
Figure 13. Data Visualizer Data Streamer Control Panel
- Drag the COM0 output of the Serial Port control panel window and drop it on the Source port of the Data Streamer Control Panel, as shown in Figure 14.
Figure 14. Connection Between Serial Port and Data Streamer Window
- Configure the custom dashboard window to display the temperature and pressure information.
- To open the custom dashboard window, open the Configuration tab located on the left side of the Data Visualizer tool, expand Visualization, and click Custom Dashboard, as shown in Figure 15.
Figure 15. Custom Dashboard Window
- Check Edit below the dashboard I/O window, click Load and set the appropriate path for the provided custom dashboard file from the host computer, as shown in Figure 16.
Figure 16. SPI-RS485 Bridge Custom Dashboard Window
- Check Show Endpoints below the Dashboard I/O window.
- Drag the appropriate port pins from the Data Streamer control panel and drop them on the respective endpoints, as shown in the Figure 17.
Figure 17. Data Visualizer Connection Reference Diagram
- Follow the Data Visualizer Serial Configuration steps (1-4) to open the Data Visualizer terminal for the Client system.
Figure 18. Data Visualize Terminal Window (Client)
- System Clock: 32 MHz
- Clock Source: HFINTOSC
Figure 19. Clock Module Window
- Default Value for COSC bits: HFINTOSC (32 MHz)
Figure 20. Configuration Bits Window
- Baud Rate: 9600
Figure 21. EUSART Configuration Window
- Clock Prescaler: 1:32768
- Post scaler: 1:3
- Clock Source: FOSC/4
- Requested Period(s): 3
Figure 22. TMR0 Configuration Window