時間模組

使用DS1307 RTC

[材料]

1. Arduino UNO主板 x 1

2. DS1307 RTC


[Arduino函式庫]

DS1307 RTC:https://www.arduino.cc/reference/en/libraries/ds1307rtc/

[DS1307 RTC IIC腳位]

SCL --> arduino A5

SDA --> arduino A4

VCC --> arduino 5V

GND --> arduino GND


  1. 設定RTC時間和電腦相同



#include <Wire.h>

#include <TimeLib.h>

#include <DS1307RTC.h>


const char *monthName[12] = {

"Jan", "Feb", "Mar", "Apr", "May", "Jun",

"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

};


tmElements_t tm;

byte adjustt = 9; // 修正編譯上傳的時間差 adustment for seconds lag allowing for compiler download time


void setup() {

bool parse=false;

bool config=false;


// get the date and time the compiler was run

if (getDate(__DATE__) && getTime(__TIME__)) {

parse = true;

// and configure the RTC with this info

if (RTC.write(tm)) {

config = true;

}

}


Serial.begin(9600);


if (parse && config) {

Serial.print("DS1307 configured Time=");

Serial.print(__TIME__);

Serial.print(", Date=");

Serial.println(__DATE__);

} else if (parse) {

Serial.println("DS1307 Communication Error :-{");

Serial.println("Please check your circuitry");

} else {

Serial.print("Could not parse info from the compiler, Time=\"");

Serial.print(__TIME__);

Serial.print("\", Date=\"");

Serial.print(__DATE__);

Serial.println("\"");

}

}


void loop() {

}


bool getTime(const char *str)

{

int Hour, Min, Sec;


if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;

tm.Hour = Hour;

tm.Minute = Min;

tm.Second = Sec + adjustt;

return true;

}


bool getDate(const char *str)

{

char Month[12];

int Day, Year;

uint8_t monthIndex;


if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;

for (monthIndex = 0; monthIndex < 12; monthIndex++) {

if (strcmp(Month, monthName[monthIndex]) == 0) break;

}

if (monthIndex >= 12) return false;

tm.Day = Day;

tm.Month = monthIndex + 1;

tm.Year = CalendarYrToTm(Year);

return true;

}


2. 在LCD上顯示時間



#include <Wire.h>

#include <TimeLib.h>

#include <DS1307RTC.h>


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //設定LCD


void setup() {

Serial.begin(9600);


lcd.begin(16, 2); //初始化 LCD,代表我們使用的LCD一行有16個字元,共2行。

lcd.backlight(); //開啟背光

}


void loop() {

tmElements_t tm;


if (RTC.read(tm)) {

Serial.print("Ok, Time = ");

print2digits(tm.Hour);

Serial.write(':');

print2digits(tm.Minute);

Serial.write(':');

print2digits(tm.Second);

Serial.print(", Date (D/M/Y) = ");

Serial.print(tm.Day);

Serial.write('/');

Serial.print(tm.Month);

Serial.write('/');

Serial.print(tmYearToCalendar(tm.Year));

Serial.println();

} else {

if (RTC.chipPresent()) {

Serial.println("The DS1307 is stopped. Please run the SetTime");

Serial.println("example to initialize the time and begin running.");

Serial.println();

} else {

Serial.println("DS1307 read error! Please check the circuitry.");

Serial.println();

}

delay(9000);

}


lcd.home(); //LCD歸零,清除所有內容

lcd.clear(); //LCD歸零,清除所有內容

lcd.setCursor(0, 0); // 設定游標位置在第一行第一個字

lcdprint2digits(tm.Hour);

lcd.write(':');

lcdprint2digits(tm.Minute);

lcd.write(':');

lcdprint2digits(tm.Second);

lcd.setCursor(0, 1); // 設定游標位置在第二行第一個字

lcd.print(tm.Day);

lcd.write('/');

lcd.print(tm.Month);

lcd.write('/');

lcd.print(tmYearToCalendar(tm.Year));

delay(100);

}


void print2digits(int number) {

if (number >= 0 && number < 10) {

Serial.write('0');

}

Serial.print(number);

}

void lcdprint2digits(int number) {

if (number >= 0 && number < 10) {

lcd.write('0');

}

lcd.print(number);

}