Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd (0X27, 24, 2);
- byte hours = 15, minutes = 17, seconds = 5;
- byte days = 3, months = 5, years = 19;
- byte bcdToDec(byte val){
- val = val / 16 * 10 + val % 16;
- return val;
- }
- byte decToBcd(byte val){
- val = val / 10 * 16 + val % 10;
- return val;
- }
- void setupTime(int second, int minute, int hour){
- Wire.beginTransmission(0x68);
- Wire.write(0x0);
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- Wire.endTransmission();
- }
- void readTime(){
- Wire.beginTransmission(0x68);
- Wire.write(0x0);
- Wire.endTransmission();
- Wire.requestFrom(0x68, 3);
- if(Wire.available()){
- seconds = bcdToDec(Wire.read());
- minutes = bcdToDec(Wire.read());
- hours = bcdToDec(Wire.read() & 0b111111);
- }
- }
- void printTime(){
- readTime();
- lcd.setCursor(0, 0);
- Serial.print(hours / 10);
- lcd.setCursor(1, 0);
- Serial.print(hours % 10);
- lcd.setCursor(2, 0);
- Serial.print(":");
- lcd.setCursor(3, 0);
- Serial.print(minutes / 10);
- lcd.setCursor(4, 0);
- Serial.print(minutes % 10);
- }
- void setupDate(int day, int month, int year) {
- Wire.beginTransmission(0x68);
- Wire.write(0x3);
- Wire.write(0);
- Wire.write(decToBcd(day));
- Wire.write(decToBcd(month));
- Wire.write(decToBcd(year));
- Wire.endTransmission();
- }
- void readDate(){
- Wire.beginTransmission(0x68);
- Wire.write(3);
- Wire.endTransmission();
- Wire.requestFrom(0x68,4);
- if(Wire.available()){
- bcdToDec(Wire.read());
- days = bcdToDec(Wire.read());
- months = bcdToDec(Wire.read());
- years = bcdToDec(Wire.read());
- }
- }
- void printDate(){
- readDate();
- lcd.setCursor(6, 0);
- Serial.print(dateArray[0] / 10);
- lcd.setCursor(7, 0);
- Serial.print(dateArray[0] % 10);
- lcd.setCursor(8, 0);
- Serial.print("/");
- lcd.setCursor(9, 0);
- Serial.print(dateArray[1] / 10);
- lcd.setCursor(10, 0);
- Serial.print(dateArray[1] % 10);
- lcd.setCursor(11, 0);
- Serial.print("/");
- lcd.setCursor(12, 0);
- Serial.print(dateArray[2] / 10);
- lcd.setCursor(13, 0);
- Serial.print(dateArray[2] % 10);
- }
- void setup() {
- Serial.begin(9600);
- Wire.begin();
- lcd.begin();
- lcd.backlight();
- setupTime(seconds, minutes, hours);
- setupDate(days, months, years);
- }
- void loop() {
- printTime();
- printDate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement