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
- 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
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
1
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.
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.
5
Load the TCP/IP Lite Library
- From the Device Resources > Libraries menu, double-click to add the TCP/IP Lite library.
- 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.
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.
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.
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.
- 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.
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.
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();
}
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.
Table of Contents
|