cfulfordBables14iupucmet
Created February 19, 2019

Time and Date LCD display

Uses the DS3231 easy library to display the date and time to the 16x2 LCD

BeginnerProtip16
Time and Date LCD display

Things used in this project

Story

Read more

Code

code for ds3231 and lcd screen

C/C++
paste into your ide - comments for pin out in header description
/* RTC time library Created by Bodmer: 2/3/15
 *   The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * 
 * Baby Boilers for TLI -31300 Project 
 * This code is intended to be a function call, or possibly a library call 

*/

#include <Wire.h>
#include <LiquidCrystal.h>

#include <DS3231_Simple.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


DateTime rtc;

DS3231_Simple Clock;

void setup() {
 lcd.begin(16, 2);
 lcd.clear();
  Clock.begin();

  // Get the time and date from the sketch compile time
  rtc.Hour   = conv2d(__TIME__);
  rtc.Minute = conv2d(__TIME__ + 3);
  rtc.Second = conv2d(__TIME__ + 6);

  rtc.Day    = conv2d(__DATE__ + 4);
  rtc.Month  = convm2d(__DATE__);
  rtc.Year   = conv2d(__DATE__ + 9); 

  // Set the clock
  Clock.write(rtc);
  
  rtc = Clock.read();
}

void loop() {

  // Wait here until seconds change
  uint8_t secs = rtc.Second;
  do
  {
    rtc = Clock.read();
  } while (secs == rtc.Second);
   lcd.setCursor(0, 0);
   lcd.print("Time -->");
  // Send the time and date tot he serial port
  // Add leading zeros to time to keep a nice format
  if (rtc.Hour < 10) lcd.print("0");
  
  lcd.print(rtc.Hour);
  lcd.print(":");
  if (rtc.Minute < 10) lcd.print("0");
  lcd.print(rtc.Minute);
  lcd.print(":");
  if (rtc.Second < 10) lcd.print("0");
  lcd.print(rtc.Second);

/*lcd.setCursor(col, row)
  lcd.setCursor(0, 0); // top left
lcd.setCursor(15, 0); // top right
lcd.setCursor(0, 1); // bottom left
lcd.setCursor(15, 1); // bottom right

*/

  lcd.setCursor(0,1 );
  lcd.print("Date -->");
  lcd.print(rtc.Day);
  lcd.print("/");
  lcd.print(rtc.Month);
  lcd.print("/");
  lcd.print(rtc.Year);
  lcd.print("  "); // Print 2 spaces to erase old digits (OK on I2C displays)


}

// Function to extract numbers from compile time string
uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}

// Function to extract month number from compile date string
uint8_t convm2d(const char* p) {
  uint8_t v = 0;
  if ('J' == *p && 'a' == *(p + 1)) v = 1;
  if ('F' == *p) v = 2;
  if ('M' == *p && 'a' == *(p + 1) && 'r' == *(p + 2)) v = 3;
  if ('A' == *p && 'p' == *(p + 1)) v = 4;
  if ('M' == *p && 'a' == *(p + 1) && 'y' == *(p + 2)) v = 5;
  if ('J' == *p && 'u' == *(p + 1) && 'n' == *(p + 2)) v = 6;
  if ('J' == *p && 'u' == *(p + 1) && 'l' == *(p + 2)) v = 7;
  if ('A' == *p && 'u' == *(p + 1)) v = 8;
  if ('S' == *p) v = 9;
  if ('O' == *p) v = 10;
  if ('N' == *p) v = 11;
  if ('D' == *p) v = 12;
  return v;
}

Credits

cfulford
16 projects • 4 followers
Contact
Bables14
14 projects • 2 followers
Contact
iupucmet
15 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.