synchronise AVR clock using the Network Time Protocol (NTP)
For my 7-segment display AVR clock i was searching for a way to keep the clock accurate. One option would be using a DCF77 standard time receiver. The module I had seemed to be broken so I decided to use an ethernet interface and NTP. For this I purchased a small ENC28J60 + AVR board from Simon Küppers. This small board is only about 38×30mm in size and includes an ATMega168, ENC28J60, Ethernet-Jack + Magnetics and a voltage regulator. The 14 available I/O Pins are connected to a standard 0.1″ header, so there are possibilities to extend the board.
The Network Time Protocol (NTP) is described in RFC958.
The protocol defines 48 Bytes of data and splits up into some control bytes and four 64bit timestamp. Each timestamp is the number of seconds to UTC since Jan. 1st 1900. Basically the NTP server receives the timestamp of the local requester, exchanges the adress fields, fills in it’s timestamp and sends the packet back. With the 4 timestamp fields routing delay and therefor absolute time errors can be reduced to a minima.
Simon also provides a port of the UIP TCP/IP Stack from Adam Dunkes including a sample web-server application. Also a simple framework for implementing additional applications was added by Simon. This NTP Clock implementation uses that framework. To get this port running correct for UDP applications some changes have to be made to the original code.
Find the following in file main.c and add the below:
Navigate to folder Net -> uip and add the following line to uip_UdpAppHub.h:
and then change uip_UdpAppHub.c to:
With this the TCP/IP Stack and the framework now knows how to pass and handle our data.
Under the direcory net create a new one called UdpApps (like the TcpApps folder).
Add the following two files to this directory:
ntpD.h:
ntpD.c
Find the changed files and the NTP code in the ZIP archive below.
Download here: uwebsrv-changed