Тема: Простые часы на LCD Keypad Shield и DS1307

//Небольшой легкий пример использования LCD Keypad Shield в качестве простых часов с управлением подсветкой ЖК.
//В качестве, собственно, часов используется микросхема DS1307 подключенная по схеме с arduino.org

/*
  LiquidCrystal_MCP23x17 Library - Hello World
  see http://freeduino.ru for details
 
  sample is based on original Arduino LiquidCrystal sample
  with only difference, that we use MCP23x17-based shield
  for interfacing with LCD display
*/
//include base class code
#include <MCP23x17.h>

////For SPI version of shield we include SPI.h and MCP23S17.h:
//#include <SPI.h>
//#include <MCP23S17.h>

//For I2C version of shield we include Wire.h and MCP23017.h:
#include <Wire.h>
#include <MCP23017.h>
#include <DS1307.h>

//include the library code
#include <LiquidCrystal_MCP23x17.h>

//menu
char *MenuItems_Backlight[] = {
  "Backlight OFF",
  "Backlight ON"
};

////For SPI version of shield we create CMCP23S17 object:
//CMCP23S17 MCP;

//For I2C version of shield we create CMCP20S17 object:
CMCP23017 MCP;

//LCD object
LiquidCrystal_MCP23x17 lcd;

//Menu objects
LCDMenu Menu_Backlight;

void setup() {
//  //For SPI version of shield we configure SPI bus, and MCP object with CS line (10) and address (0)
//  SPI.begin();
//  MCP.init(10, 0);

  //For I2C version of shield we configure I2C bus, and MCP object with address (0)
  Wire.begin();      //TWBR = 12;  // <- uncomment this for 400kHz I2C
  MCP.init(0);
 
  //here we configure LCD object to work through specified MCP object
  lcd.init(MCP);
  //...and turn off backlight
  lcd.Backlight(0);

  //...all the following code will be the same for original LiquidCrystal library and MCP23x17-based
 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  //lcd.print("hello, world!");
  Menu_Backlight.init(lcd, MenuItems_Backlight, 2);
 
  //clock для первого запуска часов раскомментируйте, установите время, затем закомментируйте обратно, иначе часы будут установлены в эти значения каждый раз
//   RTC.stop();
//  RTC.set(DS1307_SEC,1);        //set the seconds
//  RTC.set(DS1307_MIN,51);     //set the minutes
//  RTC.set(DS1307_HR,15);       //set the hours
//  RTC.set(DS1307_DOW,1);       //set the day of the week
//  RTC.set(DS1307_DATE,14);       //set the date
//  RTC.set(DS1307_MTH,5);        //set the month
//  RTC.set(DS1307_YR,12);         //set the year
//  RTC.start();
 
}
void clock(){
   // вывод даты в верхнюю строку.
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(RTC.get(DS1307_DATE,false))+lcd.print(".")+lcd.print(RTC.get(DS1307_MTH,false))+lcd.print(".")+lcd.print(RTC.get(DS1307_YR,true));
 
  lcd.setCursor(0, 1);
  //Добавляем ноль перед цифрой минут, секунд, что бы было удобнее смотреть
  lcd.print(RTC.get(DS1307_HR,true))+lcd.print(":");
  if (RTC.get(DS1307_MIN,false)<10)
  {lcd.print("0")+lcd.print(RTC.get(DS1307_MIN,true))+lcd.print(":");} else {lcd.print(RTC.get(DS1307_MIN,true))+lcd.print(":");}
  if (RTC.get(DS1307_SEC,false)<10)
    {lcd.print("0")+lcd.print(RTC.get(DS1307_SEC,false));}
    else { lcd.print(RTC.get(DS1307_SEC,false));}
}

void loop() {
  uint8_t i_2;
  clock();// подпрограмма вывода времени,
  if (lcd.KeyPressed()==KEY_ENTER) //при нажатии кнопки enter вызываем меню управления подсветкой
  {
    while (lcd.KeyPressed()!=0);
    i_2=Menu_Backlight.Choice();
    lcd.Backlight(i_2);
    lcd.clear();
  }
}