Using The TCPlite Library In MCC

 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.

 Materials

Hardware Tools

Software Tools

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

1

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).
image1.png

2

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.
image2.png

3

Open MCC in MPLAB X IDE

If you do not see the MCC logo, please check that you have installed the MCC plugin. More information on MCC installation can be found on the step-by-step MCC installation page.

image3.png
image4_1.png

4

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.
image5_1.png

5

Load the TCP/IP Lite Library

  • From the Device Resources > Libraries menu, double-click to add the TCP/IP Lite library.
image6.png
  • The MAC module from the Ethernet library will be loaded automatically.
  • The 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.
image7.png

6

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.

image8_2.png

7

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.
image9_1.png

8

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.
image10_1.png
  • 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.
image11_1.png

9

Configure MAC Module

  • Open the MAC module from Project Resources > Libraries > Ethernet menu
  • Select the ENC28J60 controller from the drop-down box.
image12_1.png

10

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.
    • Give these GPIO pins custom names as below.
image13_1.png
image14_1.png

11

Generate Code

  • Click on the Generate button.
  • After the code has been generated, click on the MCC logo again to close the plugin.
image15_1.png

12

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.
image16_1.png

13

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:
Network_Manage();
ip = ipdb_getAddress();
if(ip)
{
TCP_Demo_Client();
}

14

14. Modify the server IP address

  • Open tcp_client_demo.c.
  • In the TCP_Client_Initialize() function:
    • Modify the server IP address with the PC’s IP address.
    • The PC’s IP address can be found by opening Window’s Command Prompt on your PC and typing the command "ipconfig".
image17.png
image18_1.png

15

Program the Board

After connecting the board to the PC with a USB cable, click the Program button to program the board.

image19_1.png

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.
image20_1.png

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.
image21.png
  • In Wireshark, the TCP packets can be viewed by setting the filter to tcp.port == 65534.
image22.png
  • Click on the Led buttons {0,1,2,3}. This will toggle LEDs on the Curiosity board.
image23.png
  • Push the Disconnect button to close the TCP connection. A client disconnected message will appear on the STATUS text box.
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.