EthernetSNTP

SwordfishUser.EthernetSNTP History

Hide minor edits - Show changes to output

Changed lines 8-9 from:
If all goes well, the following should be displayed in the Serial Communicator window
to:
If all goes well, something like the following should be displayed in the Serial Communicator window
Changed line 14 from:
 IP address    : 192.168.254.1
to:
 IP address    : 192.123.223.1
Changed lines 16-18 from:
 Gateway      : 192.168.254.254
Primary DNS  : 192.168.254.254
Secondary DNS : 158.152.1.58
to:
 Gateway      : 192.123.223.254
Primary DNS  : 192.123.223.254
Secondary DNS : 158.123.223.58
Added lines 1-30:
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, the following should be displayed in the Serial Communicator window

>>default bgcolor=#f5f5f5<<
 Waiting for DHCP...
 Host Name    : SWORDFISH     
 MAC Address  : $00 $04 $A3 $00 $00 $00
 IP address    : 192.168.254.1
 Mask          : 255.255.255.0
 Gateway      : 192.168.254.254
 Primary DNS  : 192.168.254.254
 Secondary DNS : 158.152.1.58
 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

>>default bgcolor=#f5f5f5<<
 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
Added lines 1-72:
=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
  UTC.Decode(Seconds)
  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...
SetBaudrate(br115200)

#if NET_DHCP
Write("Waiting for DHCP...",13,10)
#else
DisplayConfig
USART.Write("Enter 't' to display UTC time",13,10)
#endif

// 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.
  NETApp.Task
       
  // read USART value - if equal to "t" then display UTC time...
  If DataAvailable Then
      If USART.ReadByte = Byte("t") Then
        USART.Write(UTCTime,13,10)
      EndIf
  EndIf
 
  // If DHCP is enabled, display configuration when a DHCP event
  // has occurred...
  #if NET_DHCP
  If NETApp.DHCPEvent() Then
      DisplayConfig()
      USART.Write("Enter 't' to display UTC time",13,10)
  EndIf
  #endif
Wend
=]