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.
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 : 192.123.223.1 Mask : 255.255.255.0 Gateway : 192.123.223.254 Primary DNS : 192.123.223.254 Secondary DNS : 158.123.223.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
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.
// 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