The RS485 communication with MODBUS protocol is mostly used over industrial sector for its highlighting features like balanced line communication, multi-drop facility, good noise immunity, and maximum data transmission speed. The MODBUS protocol with RS485 physical line makes transactions with multi-client easy and error free.
The serial protocol-based clients can be integrated to RS485 based host system using SPI-RS485 bridge.
The PIC16F152xx family of microcontrollers is equipped with Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART), Timers, and MSSP peripherals which can be used in RS485 communication and for implementation of SPI to RS485 Bridge system.
This firmware highlights the host functionalities. It demonstrates how to get connect to SPI clients to the RS485 network.
- SPI-RS485 Bridge and Modbus Client Implementation using PIC16F15276 Microcontroller
- PIC16F152xx Product Family Page
- PIC16F152xx Family Code Examples on GitHub
- PIC16F152xx MCU Family Video
- PIC16F15244 Datasheet
The SPI-RS485 Bridge example aims to integrate serial protocol (SPI) clients to RS485 based systems.
This demonstration is having Host and Client systems. Host sends MODBUS RTU query frames to client for asking the sensor data. Client receives MODBUS frames and responds back to host with sensor data, if received frame is valid.
The host system periodically (every 10s) creates the Modbus RTU frame with client address, function code, address of the desired data, it appends the 2-bytes CRC to the frame and sends the MODBUS query frame for reading holding registers of Client.
The PIC16F15244 microcontroller acts as host and is interfaced with the RS485 2 click over EUSART and it is used to convert EUSART TTL signals to RS485 standard signals for communicating with RS485 bus network.
The client microcontroller receives the frame from host, it validates the CRC of received frame by calculating the CRC, if CRC matches, then it validates the address. Client performs the required action as per the requested function code and responds to the host by preparing the response MODBUS frame.
The SPI-RS485 bridge demo block diagram is shown in below figure.
Figure 1 : SPI-RS485 Bridge Block Diagram
Modbus is a serial communication protocol developed for use with its programmable logic controllers (PLCs). In simple terms, it is a method used for 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 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 bytes data 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.
Information is stored in the Client device in four different tables. 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 1 bit and assigned a data address between 0x0000 and 0x270E. Each register is 1 word = 16 bits = 2 bytes wide and it has data address between 0x0000 and 0x270E.
The total number of registers requested.
CRC stands for Cyclic Redundancy check. It has two bytes added to the end of every MODBUS frame for error detection and to identify the 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 register requested (1st Byte) | 0x04 | The Number of bytes data received | | 0x6B | Address of the register requested (2nd Byte) | 0x00 | Register value Hi (1st Byte) | | 0x00 | The total number of registers requested (1st Byte) | 0x01 | Register value Lo (2nd Byte) | | 0x02 | The total number of registers requested (2nd Byte) | 0xA1 | Register value Hi (3rd Byte) | | 0xB6 | CRC for error checking (1st Byte) | 0x9B | Register value Lo (4th Byte) | | 0xC3 | CRC for error checking (2nd Byte) | 0xC7,0xC9 | CRC value Hi and Low (1st and 2nd Byte) |
Note: Refer modbus.org for more details on MODBUS.
- MPLAB® X IDE 6.0.0 or newer
- MPLAB® XC8 Compiler 2.36.0 or newer
- MPLAB® Data Visualizer 1.3.1136 or newer
- Microchip PIC16F1xxxx_DFP Device Support 1.9.163 or newer pack
- MPLAB® Code Configurator (MCC) 5.1.1 or newer
- TMR0 MCC Melody driver 4.0.8
- UART MCC Melody driver 1.6.0
- MSSP MCC Melody driver 6.1.1
- GPIO/Pins drivers MCC Melody driver 3.2.2
The PIC16F15244 Curiosity Nano evaluation board with the RS485 2 click is used as host system. The boards are mounted on a Curiosity Nano base for Click boards. The following figure shows hardware setup of the application. Visit SPI-RS485 Bridge and Modbus Client Implementation using PIC16F15276 Microcontroller git repository for client hardware setup.
Refer 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 | RB5 | TX1 | Tx Pin for RS485 2 Click | IN |
2 | RB7 | RX1 | Rx Pin for RS485 2 Click | OUT |
3 | RC0 | TX | CDC/Debug TX Line | IN |
4 | RC1 | RX | CDC/Debug RX Line | OUT |
5 | RA2 | RE | Receive Control of RS485 | OUT |
6 | RC6 | DE | Transmit Control of RS485 | OUT |
Visit SPI-RS485 Bridge and Modbus Client Implementation using PIC16F15276 Microcontroller for hardware connection details of client system.
Note:
- Connect jumper on the host curiosity nano base for click board pins from AN2 (RB5) to TX2.
- Connect jumper on the host curiosity nano base for click board pins from AN1 (RB7) to RX2.
- Connect jumper on the host curiosity nano base for click board pins from SCK (RC6) to CS2.
- 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 “Host Modbus RTU Frame No” field (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 “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 transaction is not successful.
Figure 5 : Error flags field in Data Visualizer custom dashboard window
- Observe Temperature and Pressure sensor data in “Holding Register Data” fields for visualizing measured sensor data received from holding registers of client.
Figure 6 : Temperature data in Holding register data field on Data Visualizer custom dashboard window
Figure 7 : Pressure data in Holding register data field on Data Visualizer custom dashboard window
The data visualizer tool is used as a graphical user interface while demonstrating the application by displaying host frame, response received frame from Host and Client respectively. Pre-configured data streamer file and custom dashboard file are available with the Host firmware package. Follow the steps below, for data visualizer configuration using these files to prepare the setup.
- Open the standalone data visualizer. Refer Data Visualizer Software User’s Guide for installation and features.
Figure 8 : Standalone Data Visualizer icon
- In the data visualizer window, click on Configuration tab.
Figure 9 : Data Visualizer Configuration Window
- In the Modules section, expand External connection option and then double click on Serial port.
Figure 10 : Data Visualizer Modules Window
- From 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 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, Data Streamer window that appears, as shown in Figure 12.
- Click the ‘...’ symbol in the Data Stream control panel window, 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 Load in the data streamer control panel window, as shown in Figure 12. The Data Streamer window will load with ports, as shown in Figure 13.
Figure 13 : Data Visualizer Data streamer control panel
- Drag COM0 output of the Serial Port control panel window and drop on to Source port of data streamer control panel window, as shown in Figure 14.
Figure 14 : Connection between Serial Port and Data Streamer Window
- Configure the custom dashboard window to display the battery monitoring 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 that appears 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 onto the respective endpoints, as shown in the Figure 17.
Figure 17 : Data Visualizer Connection Reference Diagram
- Follow the Data Visualizer Serial Configuration (Steps 1,2,3 and 4) steps to open the Data Visualizer terminal for Client system.
Figure 18 : Data Visualize Terminal Window (Client)
- System Clock: 32MHz
- Clock Source: HFINTOSC
Figure 19 : Clock Module Window
- Default Value for COSC bits: HFINTOSC (32MHz)
Figure 20 : Configuration Bits Window
- Baud Rate: 9600
Figure 21 : EUSART Configuration Window
- Clock Prescaler: 1:8192
- Post scaler: 1:15
- Clock Source: HFINTOSC
- Requested Period(s): 0.98 (1s)
Figure 22 : TMR0 Configuration Window