USB Modbus Interface
From Field of Cows
This page describes the design and build of a Modbus I/O board that connects to a PC using a USB interface.
I decided to design this board following on from the Modbus board I built for my Boxford 125 TCL CNC Lathe. The board I designed for that project was more complicated than it needed to be so I wanted to simplify it and replace the existing one with this new design.
I am trying to keep this design as generic as possible so it will be useful to anyone else that wants to build them rather than just in my lathe. I'll try and give as much detail as possible as well as publishing the source code for my firmware.
The requirements for the I/O board are mainly defined by what I need the board to do with my lathe and consists of, at minimum the following:
- 16 digital inputs
- 1 analogue input
- 2 digital outputs
- 1 analogue output
- Connection to PC via USB
- Implementation of Modbus RTU protocol
Conveniently, the 18F4550 PIC I will be using can provide most of this without any extra circuitry.
For my lathe, the 16 digital inputs will be used to monitor the state of the buttons on the front panel. The analogue input will be used for the feed rate override potentiometer. The digital outputs will control the spindle forward/reverse. The analogue output will set the spindle speed.
By using a PIC, it is very easy in software and hardware to change the functions of the I/O so you can easily trade some of the inputs as outputs if necessary.
The schematic shown below is based on the circuit I made for my CNC lathe. As such, it has mostly been tested but as I haven't built it yet (and the fact that I am not brilliant at electronics) it is quite possible it contains errors. Please bear this in mind if you are planning on building it. I accept no responsibility for damage to equipment or yourself if you decide to build this.
Most of the circuit is very basic and comprises just a PIC and the essentials required to drive it.
The digital I/O and analogue inputs are connected directly to headers on the board for connection to switches, etc. It makes sense to use buffers on all the I/O to protect the PIC from noise and spikes. I may add them in a future revision.
You can download the Eagle file for the latest schematic at the bottom of this page.
I used Eagle to layout the components on a PCB and used the autorouter to generate the traces. It may not be optimal but it works. Eagle does a far better job than I could do by hand.
As I will be milling the PCB myself, I have increased the width of the tracks and spacing accordingly. This means that traces cannot fit between pins of an IC and makes it very hard to make a single sided board. Therefore I will be making a double-sided board.
Also, as I don't have any through-plating on my board I have to ensure that wherever a component obstructs access to the top surface, the tracks are routed to the bottom layer. You can do this easily in Eagle by drawing regions on the "tRestrict" layer before autorouting.
You can download the latest Eagle file for this PCB at the bottom of this page.
Building a Prototype
I haven't yet built a prototype board or even layed-out the PCB yet but I will in the near future. I will use my CNC Router to mill a PCB. I find this the simplest way to make a PCB, much easier than the UV exposure/etching method I used to use.
My router does a reasonable job of milling PCBs but I have to be careful to ensure the bed is completely flat before starting. I normally mill a fresh flat area in my spoilboard to ensure it is completely flat.
You can see my router in action milling the PCB for my Boxford lathe below.
This PCB is based on the same schematic as above but includes a couple of extra interfaces for my Boxford lathe. It shows the sort of board I can make on my router.
Microchip provide a very good set of development tools for the PIC18 series so I chose to use that to write the firmware.
The tools I used were:
- MPLAB Integrated Development Environment
- MPLAB C Compiler for PIC18 MCUs
- Microchip Application Libraries
The MPLAB IDE is completely free. The C compiler can be freely downloaded but only gives full functionality for 60 days. After this time, certain optimisations are disabled. This shouldn't really be a problem for this project but I don't know this for sure as my evaluation period has not yet expired.
The Microchip Application Libraries contain a full USB stack for the PIC18 series and are used as a basis for providing the USB serial port capability for the I/O board.
The firmware was written entirely in C. It was originally written for my Boxford Lathe project and used an old version of the Microchip USB stack.
I've recently reworked the firmware source code to use the latest Microchip libraries as well as tidying up the code and adding documentation.
The code is formed of only a few tasks:
- Main processing loop
- Modbus Protocol Handler
The initialisation code simply configures the PIC ready for USB operation and for I/O. The I/O configuration is performed by the Modbus module to encapsulate the setup with the configuration of inputs and outputs for Modbus.
The main processing loop is just an endless loop that polls the Modbus code to see if any processing needs to be done. The loop does not need to handle USB as this is interrupt based rather than polled.
The Modbus Protocol Handler is the main module within the firmware. It is designed to be compilable cross-platform (I used this to develop the module on a desktop platform to make debugging simple).
The module is basically a state machine with a parser to transition between states. The parser handles both the RTU and ASCII Modbus protocols, the protocol being either switchable at runtime or compile time.
I've released the source code under the GNU General Public License. The download link for the source code is at the bottom of the page.
Building the Source Code
I've tried to make the source code build as simple as possible. If you're starting from a fresh system then you should be able to perform the following steps to build the source code:
- Install MPLAB Integrated Development Environment in the default folder.
- Install MPLAB C Compiler for PIC18 MCUs in the default folder.
- Install Microchip Application Libraries in the default folder.
- Download and unzip the source code in your "My Documents" folder in a subdirectory called "Src".
- Double-click on the workspace file (with the extension ".mcw") in the unzipped folder.
- Select "Build All" from the "Project" menu.
- http://www.modbustools.com/modbus.asp - Good description of the Modbus protocol.