#include <LiquidCrystal.h>
#include <Wire.h>
// LiquidCrystal(RS,RW,EN,d0,d1,d2,d3,d4,d5,d6,d7)
LiquidCrystal lcd(24,23,22,37,36,35,34,33,32,31,30); // initialize the LCD as per Section2
#define RTC 0x68 // set 7-bit I2C address for RTC
char *d_o_t_w[8] = {"N/A ","Sunday ", "Monday ", "Tuesday ", "Wednesday", "Thursday ", "Friday ", "Saturday "};
int dmyhms[6] = {31, 12, 99, 11, 59, 45};
int date = 0;
//**********************************************************************
// Some global variables declared and initialized here.
//**********************************************************************
// RTC variables
int ampm=0;
//**********************************************************************
void setup()
{
lcd.begin(20,4); delay(2); // Initialize the LCD
delay(500);
lcd.setCursor(1, 0); // Start LCD at Col. 1, Line 0 (top line)
// lcd.print("Lab 3: RTC and I2C Lab");
// I2C setup
Wire.begin();
RTC_init(); // Initialize DS1307 RTC
}
//**********************************************************************
void loop()
{
RTC_read();
lcd.setCursor(1, 1);
if(date == 0)
date++;
lcd.print(d_o_t_w[date]);
lcd.setCursor(1, 2);
if (dmyhms[3]<10)
lcd.print('0'); // Pad hours with '0'
lcd.print(dmyhms[3] & 0x3F,HEX); // Mask out top 3 bits then hours to LCD
lcd.print(":");
if (dmyhms[4]<10)
lcd.print('0'); // Pad minutes with '0'
lcd.print(dmyhms[4],HEX); // Minutes to LCD
lcd.print(":");
if (dmyhms[5]<10)
lcd.print('0'); // Pad seconds with '0'
lcd.print(dmyhms[5],HEX); // Seconds to LCD
ampm = dmyhms[3] & 0x20; // Maskout all values but the am/pm bit
if (ampm == 0x20)
lcd.print("pm ");
else
lcd.print("am ");
lcd.setCursor(1, 3);
if(dmyhms[0] < 10)
lcd.print('0');
lcd.print(dmyhms[0] & 0x3F,HEX); // Days to LCD
lcd.print("/");
if(dmyhms[1] < 10)
lcd.print('0');
lcd.print(dmyhms[1] & 0x1F,HEX); // Months to LCD
lcd.print("/");
if(dmyhms[2] == 0xA0)
dmyhms[2] = 0;
if(dmyhms[2] < 10)
lcd.print('0');
lcd.print(dmyhms[2],HEX); // Years to LCD
}
//**********************************************************************
// My functions start right now.
//**********************************************************************
void RTC_init()
{
Wire.beginTransmission(RTC); // Open comm. with RTC
Wire.write(0x00); // Set RTC address to be accessed
// SEND 3 bytes to RTC to start clock ticking and set initial time and in the modes desired
// i.e. (continuous/one shot) or 12hr/24hr you will need to add more HEX bytes for calendar.
Wire.write(0x45); // Seconds (this one is easy)
Wire.write(0x59); // Minutes (...so is this one)
//Wire.send(0x11); // Hours (also selects 24hr - bit6=0 (24hr) Wire.send() is no longer supported by the library
// Wire.write(0x51); // 12hr mode in the AM (bit6=1 and bit5=0)
Wire.write(0x71); // 12hr mode in the PM (bit6=1 and bit5=1)
// Wire.write(0x11); // 24hr mode 11
// Wire.write(0x23); //24 mode 23
// When in 12hr mode you must also write to bit5 => 0=am 1=pm
// Since in 12hr mode this bit is not needed for hours it is now the am/pm bit:
// Example 0x51 ==> 0 1 0 1 – 0 0 0 1
// – 12hr am 1 1 o'clock (am)
// Example 0x71 ==> 0 1 1 1 – 0 0 0 1
// – 12hr pm 1 1 o'clock (pm)
// Example 0x11 ==> 0 0 0 1 – 0 0 0 1
// – 24hr x 1 1 hundred hours
Wire.write(0x05); //Date of the week
Wire.write(0x31); //Day
Wire.write(0x12); //Month
Wire.write(0x99); //Year
Wire.write(0x12); //Control
Wire.endTransmission(); // Close I2C com. with RTC
}
//**********************************************************************
void RTC_read()
{
Wire.beginTransmission(RTC); // Open comm. with RTC
Wire.write(0x00);// SEND RTC address to be accessed
Wire.endTransmission(); // Close I2C com. with RTC
// Get current time from RTC by requesting 3 bytes (you will add more)
Wire.requestFrom(RTC, 7); // Read in 3 BCD bytes from RTC
dmyhms[5] = Wire.read() & 0x7F; // Store seconds
dmyhms[4] = Wire.read() & 0x7F; // Store minutes
dmyhms[3] = Wire.read() & 0x3F; // Store hours
date = Wire.read() & 0x07;
dmyhms[0] = Wire.read() & 0x3F; //days
dmyhms[1] = Wire.read() & 0x1F; //months
dmyhms[2] = Wire.read() & 0xFF; //years
}