Pages: [1]   Go Down
Author Topic: DS3234 Deadon RTC with NTP question  (Read 704 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a problem with the Deadon RTC "(DS3234).

1) I have compiled and ran NTP sketches and got them to function just fine (without overloading our network btw).
2)I have successfully set the time on the RTC to compile time and it functions but gets less accurate over time.

My idea is to have the RTC updated with NTP and set THAT time each time I send an NTP request (approx once a day/week)

However.... I cannot seem to do the following:

a)set the RTC time to compile time;
b)send out an NTP request;
c)update RTC with the received NTP packet.

And I have done many searches for this exact thing and am confused with the results that I have found.

Does anyone have an example of how I  would accomplish this?

It would be greatly appreciated.

Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not using this http://arduino.cc/en/Tutorial/UdpNtpClient example and make the loop() (a bit modified to fulfill your needs) just a function you call when you wanna your RTC corrected?
Logged

The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 5
There are 10 types of people in the world: Those who understand binary, and those who don't.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe I can help here.....
I have a sketch wich uses NTP to sync my DS3234. The sketch uses my Arduino Duemilanove, an Ardino Ethernet R3 Shield and a DS3234.
The shield is stacked on the Duemilanove and the DS is connected like this; SS – pin 8, MOSI – pin 11, MISO – pin12, and SCK – Pin13.
I connected the shield to my local network and uploaded the sketch....

Please note that the sketch uses the mac-address of my ethernet shield, change it to yours. Also I use the RTC_DS3234.h library.

Code:
/*
 
 Udp NTP Client
  
 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket
 For more on NTP time servers and the messages needed to communicate with them,
 see http://en.wikipedia.org/wiki/Network_Time_Protocol
  
 This code is in the public domain.
 
 */
 
#include <SPI.h>        
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
const int  RTCchipSelect = 8; //chip select
RTC_DS3234 RTC(RTCchipSelect);

// Enter a MAC address for your controller below.
 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x24 }; // Change this to your Ethernet Shield's mac!
 
unsigned int localPort = 8888;      // local port to listen for UDP packets
 
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
 
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
 
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
 
void setup()
{
  // Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }
 
  
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
   }
   Udp.begin(localPort);
 }

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  
  // wait to see if a reply is available
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);              
    
    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;  
    // print Unix time:
    Serial.println(epoch);
        
    // Syncing the UTC to the RTC
    SPI.begin();
    RTC.begin();
    SetTimeDate(DateTime(epoch).day(), DateTime(epoch).month(), DateTime(epoch).year() - 2000, DateTime(epoch).hour(), DateTime(epoch).minute(), DateTime(epoch).second());
    Serial.println(ReadTimeDate());
    SPI.setDataMode(SPI_MODE0);
    
    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');  
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch %60); // print the second
  }
  // wait a second before asking for the time again
  delay(1000);
}
 
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:        
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);        
digitalWrite(RTCchipSelect, HIGH);
int a=n & B00001111;    
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;        
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
        //Year
temp.concat("20") ;
        if (TimeDate[6] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[6]);
temp.concat("-") ;
        //Month
        if (TimeDate[5] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[5]);
temp.concat("-") ;
        //Day
        if (TimeDate[4] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[4]);
temp.concat("\t") ;
        //Hours
        if (TimeDate[2] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[2]);
temp.concat(":") ;
        //Minutes
        if (TimeDate[1] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[1]);
temp.concat(":") ;
        //Seconds
        if (TimeDate[0] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
temp.concat(TimeDate[0]);
  return(temp);
}

//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6; i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
 
digitalWrite(RTCchipSelect, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);        
digitalWrite(RTCchipSelect, HIGH);
  }
}

Here's a screen shot from the Serial console in the attachment.


* NTP-DS3234.png (39.48 KB, 558x390 - viewed 23 times.)
« Last Edit: September 21, 2012, 04:01:43 pm by MSpijk » Logged

Pages: [1]   Go Up
Jump to: