Go Down

Topic: Regarding RTc (Read 2724 times) previous topic - next topic

salaideva

Can i glow  my LED based on time set by RTC module,pls guide me through how to set time using RTC(DS1307) in my DUE board.

JimboZA

There's an example in the DS1307RTC library's "example" folder; it sets the RTC's time to the time from your PC when you compiled and uploaded the sketch in the IDE.

Code: [Select]
//set the time to pc system time at compile time
//from the ds1307rtc library's examples

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

salaideva

Arduino: 1.5.7 (Windows 8), Board: "Arduino Due (Programming Port)"

Build options changed, rebuilding all

In file included from sketch_oct01a.ino:6:0:
C:\Program Files (x86)\Arduino\libraries\DS1307RTC/DS1307RTC.h:19:22: error: 'tmElements_t' has not been declared
     static bool read(tmElements_t &tm);
                      ^
C:\Program Files (x86)\Arduino\libraries\DS1307RTC/DS1307RTC.h:20:23: error: 'tmElements_t' has not been declared
     static bool write(tmElements_t &tm);
                       ^
sketch_oct01a.ino:13:1: error: 'tmElements_t' does not name a type
sketch_oct01a.ino: In function 'void setup()':
sketch_oct01a.ino:23:21: error: expected primary-expression before ')' token
sketch_oct01a.ino: In function 'bool getTime(const char*)':
sketch_oct01a.ino:56:5: error: expected unqualified-id before '.' token
sketch_oct01a.ino:57:5: error: expected unqualified-id before '.' token
sketch_oct01a.ino:58:5: error: expected unqualified-id before '.' token
sketch_oct01a.ino: In function 'bool getDate(const char*)':
sketch_oct01a.ino:73:5: error: expected unqualified-id before '.' token
sketch_oct01a.ino:74:5: error: expected unqualified-id before '.' token
sketch_oct01a.ino:75:5: error: expected unqualified-id before '.' token

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

it show error like this

JimboZA

Maybe you don't have that library installed?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

salaideva

will u pls provide me the link of that libary
thanks

JimboZA


will u pls provide me the link of that libary
thanks



I think it was this one, which I found in the Playground:

http://www.pjrc.com/teensy/td_libs_DS1307RTC.html

In any case, you might find the playground useful:

http://playground.arduino.cc/code/time
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

graynomad

IIRC the 1307 is a 5v-only chip, will it work with 3v3 signals?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

JChristensen


In any case, you might find the playground useful:

http://playground.arduino.cc/code/time


The Time library is not compatible with the Due. I haven't tried PJRC's improved version but am fairly certain that it would not be, either. Have a look at these:

http://forum.arduino.cc/index.php/topic,136843.0.html
http://forum.arduino.cc/index.php/topic,136126.0.html
https://github.com/MarkusLange/Arduino-Due-RTC-Library


IIRC the 1307 is a 5v-only chip, will it work with 3v3 signals?


You do recall correctly, recommended supply voltage is 4.5V min, 5.0V typical, 5.5V max.

Go Up