Archiduino – RTC – Real Time Clock DS1307+ and DS1337

 

Archiduino 1284 - RTC DS1307+

Archiduino 1284 – RTC DS1307+

Archiduino 1284 mounts onboard a real time clock chip, the well known DS1307+ (or, to be precise, DS1307ZN+) made by Maxim Integrated. This RTC works at 5VDC and is managed via I2C bus at 0x68 address. In the Arduino world there’s a plenty of libraries designed to manage DS1307+. With Archiduino you can use those libraries without any modification.

Archiduino 32 mounts onboard the  DS1337, which is powered at 3.3VDC. This RTC works at same 0x68 address of Archiduino 1284. Libraries and code are the same for both Archiduino CPU boards.

The RTC works as a clock/calendar with its own 32.768 KHz oscillator crystal, so timing functions can not be influenced by MCU clock or any other external timing factor. It also gives the possibility of connecting a backup battery in order to keep timing data when Archiduino is switched off. The battery is not included in standard version of the Archiduino 1284 or Archiduino 32 CPUs, but it can be bought as an option (see our shop for more info).

 

 

Archiduino – RTC – Complete example

This small examplet shows how we could display on LCD a useful calendar clock. The example uses RTCLib.h library, originally posted by JeeLab’s and forked by AdaFruit. You can upload this program in both Archiduino 32 and Archiduino 1284

#ifdef ARCHIDUINO_32 
#include <Archiduino32.h>
#endif

#include <RTClib.h>
#include <LCD1284.h>


RTC_DS1307 rtc;
LCD1284 lcd; 

/* --------------------------------------------------------------------------*/

// days of the week array (translate in in your favourite language)

  char daysOfTheWeek[7][12] = {"DOM", "LUN", "MAR", "MER", "GIO", "VEN", "SAB"};
//char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

/* --------------------------------------------------------------------------*/

// parameters to manage led blink with no delays -> see function ledBlink()

int ledState = LOW;                  // state of the led
unsigned long previousMillis = 0;    // will store last time LED was updated
const long interval = 500;           // blink interval (milliseconds)


/* --------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------*/
void setup () {

  analogReference(EXTERNAL);
  Serial.begin(9600);

  #ifdef ARCHIDUINO_32
    while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB
    } 
  #endif
  
  Serial.println("Archiduino Available!");

  /* --------------------------------------------------------------------------*/
  
  delay(500);  
  lcd.init();                        // initialize the lcd 
  lcd.begin(16, 2);                  // initialize lcd and keyboard
  lcd.clear();
  lcd.backlight();

  /* --------------------------------------------------------------------------*/

  rtc.begin();
  
  #ifdef ARCHIDUINO_32
    pinModeEx(LED_RUN, OUTPUT);
  #else
    pinMode(LED_RUN, OUTPUT);
  #endif
  
  // The following line sets RTC with date and time of last compile
  // Comment it after first firmware upload
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));



}

/* --------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------*/
void loop () {
  
    DateTime now = rtc.now();
   
    lcd.setCursor(0,0);
    
    lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
    lcd.print(' ');
    
    FMT(now.day(), '/');
    FMT(now.month(), '/');
    FMT(now.year(), ' ');

    lcd.setCursor(0,1);

    FMT(now.hour(), ':');
    FMT(now.minute(), ':');
    FMT(now.second(), ' ');

    delay(1000);

    ledBlink();

}

/* --------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------*/
void ledBlink() {
  
  // This routine manages led blink without using awful and harmful delay();
  
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
      #ifdef ARCHIDUINO_32
      digitalWriteEx(LED_RUN, HIGH);
      #else
      digitalWrite (LED_RUN, HIGH);
      #endif
    } else {
      ledState = LOW;
      #ifdef ARCHIDUINO_32
      digitalWriteEx(LED_RUN, LOW);
      #else
      digitalWrite (LED_RUN, LOW);
      #endif
    }

  }
  
}

/* --------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------*/
void FMT(int fNumber, char fQueue) {
  
  // This routine is a tiny utility to print a number with initial 0 (zero)
  // if you pass a char in the second parameter, you get that char on LCD
  // (useful to print / or : when displaying date or time
  
  if (fNumber < 10) lcd.print("0");
  lcd.print(fNumber, DEC);
  
  if (fQueue != ' ') lcd.print(fQueue);
  
}