Using The TCPlite Library in MPLAB® Code Configurator
Objective
This guide shows how to use the MPLAB® Code Configurator (MCC) TCP/IP Lite Stack library to create a TCP client project. With the demo code provided, a TCP connection can be made between your device running the TCP client project and the Microchip TCP/IP Demo Server application running on a PC. This allows the server application to control LEDs on the Curiosity board.
The set-up uses a PIC18F27K42, Curiosity HPC Development board (DM164136) and a MikroE ETH Click (MIKROE-971) with an ENC28J60 Ethernet Controller on-board.
Hardware Tools
- PIC18F27K42
- Curiosity High Pin Count (HPC) Development Board (DM164136)
- Ethernet ENC28J60 Click board by MikroE (ETH Click) (MIKROE-971)
- Ethernet cable (RJ45)
Software Tools
- MPLAB X IDE v5.50
- MPLAB XC8 v2.32
- MPLAB Code Configurator v4.2.1
- Microchip TCP/IP Lite Stack v2.2.13
- Foundation Services Library v0.2.2
- Microchip TCP/IP Demo GUI v1.0
- Wireshark® Tool
Exercise Files
Procedure
Set up the Hardware
- Connect the ETH Click board to the Curiosity HPC Development board (connector J28).
- Connect the ENC28J60 on the ETH Click board to an Ethernet network using an Ethernet cable.
- Connect the Curiosity HPC Board to the computer using the on-board micro-USB connector (connector J12).
Click image to enlarge.
Create an MPLAB X IDE Project for the PIC18F27K42
- Choose Project: Standalone Project
- Select Device: PIC18F27K42
- Select Tool: Curiosity/Starter Kit (PKOB4)
- Select Compiler: XC8
- Select Project Name and Folder.
Click image to enlarge.
Open MCC in MPLAB X IDE
MCC now comes pre-installed with MPLAB X IDE. Use either the MCC logo button near the center of the toolbar or Tools > Embedded > MCC from the main menu.
Click image to enlarge.
Here is the Layout of the MCC windows. To learn more about MCC start here.
Click image to enlarge.
Configure the System Clock
- Open the System Module from the Project Resources System menu.
- Select HFINTOSC from the Oscillator Select drop-down box.
- Select 8_MHz from the HF Internal Clock drop-down box.
- Choose a division factor of 1 from the Clock Divider drop-down box.
- This will result in an 8 MHz system clock.
Click image to enlarge.
Load the TCP/IP Lite Library
From the Device Resources > Libraries menu, double-click to add the TCP/IP Lite library.
Click image to enlarge.
- The MAC module from the Ethernet library will be loaded automatically.
- The Serial Peripheral Interface (SPI) module from the Peripheral library and the SPIMASTER module from the Foundation Services library are loaded automatically to support communication with the Ethernet controller.
- The TMR1 module from the Peripherals library is loaded automatically to provide the 1-second timer required for the TCP/IP Lite to work.
Click image to enlarge.
Configure the TCP/IP Lite Library for a TCP Application
Enable the following protocols by checking the corresponding box:
- UDP
- DHCP
- IPV4
- TCP
- ICMP
- ARP
Although this is a TCP demo project, the UDP module must also be included as both the Dynamic Host Configuration Protocol (DHCP) module and the SysLog protocol call UDP functions. If you want to generate a TCP-only project through MCC, the DHCP module must be excluded and a static IP configuration provided. Furthermore, the files log_syslog.c, log_syslog.h must be manually removed from the generated project.
Click image to enlarge.
Configure Timer1 Module
The TCP/IP Lite Stack must be serviced every 1 second. Timer1 is used to generate this one-second clock.
- Open the TMR1 module from the Project Resources > Peripherals menu.
- In the Hardware Settings pane:
- Select FOSC/4 from the Clock Source drop-down box.
- Select 1:1 from the Prescaler drop-down box.
- Set the Timer Period to 25 ms.
- Enable Timer Interrupts by checking the box.
- In the Software Settings pane:
- Set the Callback Function Rate to 40 x Time Period
Click image to enlarge.
Configure SPI Module
The PIC18F27K42 microcontroller communicates with the ENC28J60 Ethernet controller through an SPI. The ENC28J60 SPI supports clock speeds up to 20 MHz and only supports SPI mode 0,0.
- Open the SPI1 module from the Project Resources > Peripherals menu.
- In the Hardware Settings pane:
- Select Master from the Mode drop-down box.
- Select FOSC from the Clock Source Selection drop-down box.
- Set the Clock Divider to 0.
Click image to enlarge.
- Open the SPIMASTER module from the Project Resources > Libraries > Foundation Services menu.
- In the Hardware Settings pane:
- Select MODE0 from the SPI Mode drop-down box.
- Select END from the Input Data Sampled At drop-down box.
- Set the Speed to 8000 kHz.
- Select SPI1 from the SPI drop-down box.
- This will result in a 2000 kHz SPI clock.
Click image to enlarge.
Configure MAC Module
- Open the MAC module from Project Resources > Libraries > Ethernet menu
- Select the ENC28J60 controller from the drop-down box.
Click image to enlarge.
Configure Pin Module:
From the Pin Manager: Grid View, configure the following pins:
- SPI1 Module
- SCK1 – output pin RB1
- SDI1 – input pin RB2
- SDO1 – output pin RB3
- MAC Module
- ETH_CS – output pin RA3
- Pin Module
- GPIO – output pins RA4, RA5, RA6, RA7
- These pins are connected to LEDs on the Curiosity board.
Click image to enlarge.
Give these General Purpose Input/Output (GPIO) pins custom names as shown.
Click image to enlarge.
Generate Code
- Click on the Generate button.
- After the code has been generated, click on the MCC logo again to close the plugin.
Click image to enlarge.
Add Application Files to Project
- Download the app_files folder containing tcp_client_demo.c and tcp_client_demo.h.
- Move the app_files folder into the project folder.
- Add tcp_client_demo.c and tcp_client_demo.h to the project in the Projects tab.
Click image to enlarge.
Add Code to main.c
- Open the generated main.c file.
- Include the following header files:
- app_files/tcp_client_demo.h
- mcc_generated_files/TCPIPLibrary/ip_database.h
- In the main() function:
- Declare variable IP as type unsigned long.
- Uncomment the function INTERRUPT_GlobalInterruptEnable() to enable global interrupts.
- Place TCP_Client_Initialize() before the while loop.
- Add the following line of code to the while loop:
ip = ipdb_getAddress();
if(ip)
{
TCP_Demo_Client();
}
Modify the server IP address
The PC’s IP address can be found by opening Window’s Command Prompt on your PC and typing the command "ipconfig".
Click image to enlarge.
- Open tcp_client_demo.c.
- In the TCP_Client_Initialize() function:
- Modify the server IP address with the PC’s IP address from above.
Click image to enlarge.
Program the Board
After connecting the board to the PC with a USB cable, click the Program button to program the board.
Click image to enlarge.
Running the Demo:
- Open the Java application TCP_Demo.exe. Java must be installed on the PC to run this application.
- Go to the TCP Server Demo tab and assign the port number as 65534.
- Click on Listen to wait for a connection from the device.
After the connection is established:
- Type text inside the Send text box and click on the Send button. The text sent is displayed inside the Send/Received Databox.
- In Wireshark, the TCP packets can be viewed by setting the filter to tcp.port == 65534.
- Click on the Led buttons {0,1,2,3}. This will toggle LEDs on the Curiosity board.
- Push the Disconnect button to close the TCP connection. A client disconnected message will appear on the STATUS text box.