Interfacing ENC28J60 Ethernet Controller with PIC Microcontroller – XC8Bitahwa Bindu
Watch the Video Tutorial
Ethernet is the leading wired standard for networking as it enables to connect a very large number of computers, microcontrollers and other computer-based equipment to one another.
With just a network switch, many different devices can easily communicate with one another with Ethernet, allowing different devices and equipment to be accessed remotely and this also provides a cost-effective and reliable means of remote control and monitoring. Most of computers nowadays have an Ethernet port implemented on them so it is with many electronic devices. Many microcontrollers have built-in Ethernet peripheral, like the PIC18F97J60, this PIC18 Microcontroller has an integrated 10Mbps Ethernet communications peripheral but many other microcontrollers don’t have a built-in Ethernet peripheral.
For microcontrollers which don’t have an integrated Ethernet peripheral, Microchip offers a serial Ethernet chip that can easily be used by any microcontroller with an SPI interface to provide Ethernet capability to the application. The ENC28J60 is a popular 28-pin serial Ethernet chip, 10BASE-T stand alone Ethernet Controller with SPI interface, on board MAC & PHY, 8 Kbytes of Buffer RAM and an SPI serial interface. With a small foot print package size the ENC28J60 minimizes complexity, board space and cost.
Figure 1: Controlling devices from the internet
ENC28J60 Ethernet Controller Features
- IEEE 802.3™ Compatible Ethernet Controller
- Fully Compatible with 10/100/1000Base-T Networks
- Integrated MAC and 10Base-T PHY
- Supports One 10Base-T Port with Automatic Polarity Detection and Correction
- Supports Full and Half-Duplex modes
- Programmable Automatic Retransmit on Collision
- SPI Interface with Clock Speeds up to 20 MHz
- Buffer: 8-Kbyte Transmit/Receive Packet Dual Port SRAM
- MAC address: Supports Unicast, Multicast and Broadcast Packets
- Operating Voltage of 3.1V to 3.6V (3.3V typical)
- Temperature Range: -40°C to +85°C Industrial, 0°C to +70°C Commercial (SSOP only)
- 28-Pin SPDIP, SSOP, SOIC, QFN Packages
More information can be obtained from the ENC28J60 datasheet. The figures below show ENC28J60 package types
Figure 2: ENC28J60 package types.
ENC28J60 Ethernet Controller Connections
The interface between the microcontroller and the Ethernet chip is based on the SPI bus protocol, The SI, SO, and SCK pins of the Ethernet chip are connected to SPI pins (SDO, SDI and SCLK) of the microcontroller. The Ethernet controller chip operates at 3.3V, its output SO pin cannot drive the microcontroller input pin without a voltage translator if the microcontroller is operated at 5V. Figure 3 below shows how the ENC28J60 Ethernet controller can be interfaced to a PIC Microcontroller.
Figure 3: ENC28J60 Ethernet Controller Connections
To make the design of Ethernet applications easy, there are ready made boards that include the EC28J60 controller, voltage translation chip and an RJ45 connector. Figure 4 below shows an ENC28J60 Ethernet LAN Network module. This module includes Microchip ENC28J60/SS Chip, 25MHZ crystal, 3.3VV supply pin and HR911105A network interface board. With this board you can add Ethernet connectivity easily to your Arduino or PIC microcontroller project for your Internet of Things project.
Figure 4: An ENC28J60 Ethernet LAN Network module
The ETH Click board can also be used with any development board with mikroBus like the Curiosity Development board. The ETH Click is an accessory board in mikroBus™ form factor. It features ENC28J60 and contains standard RJ-45 connector, transmit, receive and power LEDs. On-board crystal oscillator ensures stable operation. Board is designed to use 3.3V power supply only. Figure 5 below shows our board setup: Curiosity development board with ETH Click connected to router.
Figure 5: Curiosity development board with ETH Click connected to router
Buy the Curiosity Development Board, ENC28J60 IC or Module from our Online Store
Embedded Ethernet applications
With internet in mind, one can think about many applications, below a few applications are listed:
- Web-based monitoring: A person can monitor several vending machines located in different places which can be several kilometers apart. From the PC at the conform of your desk, you could monitor live every day the status and stock of each machine, without wasting time and money to travel to each machine. A simple online interface to the vending machines can allow you to monitor everything from the internet.
- Access control: Monitor and control the access of an area from a remote location
- Environmental monitoring: Environmental sensors can installed unattended in remote locations feeding data online to a control and monitoring center.
- Industrial control: The status of machines could be remotely monitored, switch on or off,…
- Home automation: Turning devices inside the house like lights ON and OFF from a remote location even from a hand held mobile device connected to internet like a smart phone.
- Voice Over IP( VoIP)
- Internet of Things (IoT) applications
Microchip TCP/IP Lite Stack Library
Microchip provides a simple to use TCP/IP Lite Stack library that can be used with MPLAB Code Configurator.
This light stack provides an optimized (low Flash and RAM footprint) TCP/IP stack for microcontrollers with ≥8 KB Flash (UDP only) and ≥16 KB Flash (TCP/IP), while still having a fully functional TCP/IPv4 stack. The stack will allow users to add wired communication and interoperability with other systems to their applications over Ethernet.
The Microchip TCP/IP Lite Stack is implemented in a configurable and modular way, allowing users to include only the intended features or functionalities to their application. The stack is written in C programming language and it is intended to be compiled with the MPLAB® XC8 compiler.
To learn more how to use MPLAB Code Configurator, please read this article first:
Installing MPLAB® Code Configurator’s TCP/IP Lite Library
Start by downloading the TCP/IP Lite Stack from microchip website. Make sure you have these minimum system requirements:
- MPLAB® X IDE v3.61 or later
- XC8 compiler v1.42 or later
- MCC v3.36 or later
- PIC10 / PIC12 / PIC16 / PIC18 v1.45
- Foundation Services v0.1.21
To install the MCC TCP/IP Lite Library:
- In the MPLAB® X IDE, select Plugins from the Tools menu
- Select the Available Plugins tab
- Check the box for the TCP/IP Lite Stack v2.2.8, and click on Install
You can read more how to download and install the library from the release note.
TCP/IP Lite Stack Library Configuration with MCC
In this example we are using the Curiosity board with ETH click, setup the hardware as shown on figure 5 above:
- Connect the ETH click board to the Curiosity Development board (connector J35).
- Connect the ENC28J60 to an Ethernet network using an Ethernet cable (Straight-through cable if the board will be connected to a router/switch or Crossover cable if the board will be connected to the computer directly).
- Connect the USB power supply to the Curiosity Development board using the J2 connector.
Start a new project and select the PIC you are going to use. In this example we are going to use PIC16F18346 but any 8-bit MCU supported by MCC can be utilized instead as long as it has ≥8 KB Flash (UDP only) and ≥16 KB Flash (TCP/IP)
As the TCP/IP Lite stack is scalable (the user can configure the protocols as per application requirements). MCC gives the user the flexibility to choose and to generate code only for the needed protocols. Apart from the protocol selection, users also have the option of configuring the stack parameters using MCC. These stack parameters will reside in the tcpip_config.h file generated by MCC. In this example we’re not gonna do much, just to configure our Ethernet board to communicate with TCP, give it an IP address and be able to ping it so that we can know it’s working.
Click on System module to setup your clock. We’re gonna use 1MHz internal oscillator as shown in Figure 6 below:
Figure 6: System module
From Device Resources –> Libraries, double click on the TCP/IP Lite module and configure it as shown in Figure 7 below:
Figure 7: TCP/IP Lite module
- UDP: Not checked. This is used to send/receive UDP packets. In this example we are going to use TCP.
- DHCP: Not checked. We will use a static IP address.
- IPV4: Checked. We have set the IP Address to 192.168.8.5 the Subnet Mask to 255.255.255.0 and the Default Gateway to 192.168.8.1. You can use different IP address as long as it’s within the same range as your local network.
- TCP: Checked. No configuration required in MCC.
- ICMP (Internet Control Message Protocol): Checked. We’re gonna tick the option to generate Echo Response and Port Unreachable messages. The Ping command uses this ICMP protocol. Any TCP packet with invalid port number will respond with an “ICMP PORT UNREACHABLE” message, if “ICMP Port Unreachable” option is selected else the TCP packet will be discarded.
- ARP (Address Resolution Protocol): Checked. This refers to the maximum size of the ARP table. The default value of 8 is used.
- TFTP (Trivial File Transfer Protocol): Not checked.
- NTP (Network Time Protocol): Not checked.
- DNS (Domain Name System): Not Checked.
- LLDP (Link Layer Discovery Protocol): Not Checked.
TCP/IP Lite module requires the Ethernet MAC library and Timer1 module. The TCP/IP stack needs a background task called periodically by the user, in order to handle asynchronous events like managing a timeout, checking the status for the Ethernet controller, and parsing the received buffers.
From Device Resources –> Libraries, double click on MAC under Ethernet and configure it as shown in Figure 8 below. select the ENC28J60 controller from the drop down selection box. The ENC28J60 controller is based on the Serial Peripheral Interface. The Curiosity development board (DM164137) supports the MSSP1 – SPI module interface on the J35 connector. So we will also select MSSP1 from the MSSP drop down selection box
Figure 8: MAC selection
From Device Resources –> Peripherals, double click on TMR1 and configure it as shown in Figure 9 below. Update the Timer Period to 250 ms. Enable the Timer Interrupt and update the Callback Function Rate to 4 to generate a 1s period. This feature is required to configure the device with a 1s tick of system clock.
Figure 9: Timer1 configuration
From Device Resources –> Peripherals –> MSSP, double click on MSSP1 and configure it as shown in Figure 10 below. Select SPI Master from the Mode drop down box, select Input Data Sampled at End and Clock Edge Active to Idle. Set the SPI clock to 250KHz.
Figure 10: MSSP1 configuration
To learn more how to configure SPI bus, please read this article first:
And lastly in the pin manager, we will set the pins functions and names. Set MAC ETH_CS (chip select) for the ENC28J60 controller to output Port RC6. MSSP1 module SCK1 pin to output Port RB6, SDI1 to input Port RB4 and SDO1 to output Port RC7.
Figure 11: Pin Manager
Click the MCC Generate button to generate all our library files.
In main(), the Global and Peripheral Interrupts are enabled. In while(1) loop, the Network_Manage() command must be called. It is an API which polls the Ethernet controller for new packets and processes them.
// initialize the device
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
// Enable the Peripheral Interrupts
// Disable the Global Interrupts
// Disable the Peripheral Interrupts
Network_Manage();//API which polls the Ethernet controller for new packets
//and processes them
// Add your application code
End of File
And that’s all for this tutorial, Build your project and power up your curiosity board. Pin your board IP address you should get some replies as shown on figure 12 below.
Figure 12: Pinging the ENC28J60 board
Download MPLAB Project: Interfacing ENC28J60.