This sample program uses the SNTP protocol to obtain the time and date from a remote time server. The time and date returned is encoded, so we use the utility module "UTCDateTime.bas" to decode the returned value. Here's what you need to do.

  • Plug a network cable into your board and connect to your network. For example, just plug your Ethernet board into your broadband router, which will act as a DHCP server.
  • Compile and then program the code into your device.
  • Start the Swordfish Serial Communicator plugin, and a connect to your MCUs USART at 115200 baud.
  • Reset the MCU.

If all goes well, something like the following should be displayed in the Serial Communicator window

 Waiting for DHCP...
 Host Name     : SWORDFISH      
 MAC Address   : $00 $04 $A3 $00 $00 $00 
 IP address    :
 Mask          :
 Gateway       :
 Primary DNS   :
 Secondary DNS :
 Enter 't' to display UTC time

Now all you need to do is enter "t" and press enter. The value returned will look something like this

 Wed 06/02/2008 09:00:31

An interesting feature of the Swordfish SNTP (time server) module is that is does not repeatedly interrogate a time server for information. Instead, the PIC uses and internal millisecond counter and polls the time server every ten minutes or so to resynchronise.

Sample Code

// device and clock - code will work on an 18F452 as well...
Device = 18F4680
Clock = 20

// important configurations...
#option ENC28J60_CS = PORTD.6
#option NET_ICMP = true   // enable ping 
#option NET_DHCP = true   // enable Dynamic Host Configuration Protocol (DHCP)
#option NET_NBNS = true   // enable NETBIOS
#option NET_SNTP = true   // enable time server
Include "NETApp.bas"      // always first!

// TCPIP stack specifics...
Include "NETTypes.bas"
Include "SNTP.bas"

// program helpers...
Include "NETAppDisplay.bas"
Include "usart.bas"
Include "convert.bas"
Include "UTCDateTime.bas"
* Name    : UTCTime                                                        *
* Purpose : Gets the UTC time and formats the output to                    *
*         : DayOfWeek DAY MONTH YEAR HOUR:MINS:SECS                        *
Function UTCTime() As String(24)
   Dim Seconds As DWORD
   Seconds = SNTPGetUTCSeconds
   result = UTC.DayOfWeek(DateTime.DOW) + " " + 
            DecToStr(DateTime.Day,2) + "/" + DecToStr(DateTime.Month,2) + "/" + DecToStr(DateTime.Year) + " " + 
            DecToStr(DateTime.Hour,2) + ":" + DecToStr(DateTime.Min,2) + ":" + DecToStr(DateTime.Sec,2)
End Function

// program start...

Write("Waiting for DHCP...",13,10)
USART.Write("Enter 't' to display UTC time",13,10)

// loop forever...
While true

   // This task performs normal stack task including checking for incoming 
   // packet, type of packet and calling appropriate stack entity to process it.

   // read USART value - if equal to "t" then display UTC time...
   If DataAvailable Then
      If USART.ReadByte = Byte("t") Then

   // If DHCP is enabled, display configuration when a DHCP event
   // has occurred...
   #if NET_DHCP
   If NETApp.DHCPEvent() Then
      USART.Write("Enter 't' to display UTC time",13,10)