|
在许多电子项目中,必须根据时间或日期运行操作。当系统关闭时,不应停止计算时间和日期。为此,使用实时时钟(RTC)模块。在本篇文章中,您将学习如何使用RTC DS1307模块和Arduino开发板制作一个提醒器。
什么是实时时钟? 实时时钟(Real Time Clock)或RTC是一个跟踪当前时间的系统,可用于需要保持准确时间的任何设备。
您还可以在不使用RTC系统的情况下跟踪确切时间,但RTC具有一些重要优势。以下是其中一些优点: ● 功耗低 ● 从时间计算中释放系统时间(此功能至关重要,因为在许多情况下CPU正在执行一些精细的任务,如接收传感器数据。如果您不使用RTC,CPU还必须跟踪时间并且它可能会中断处理器主任务。) ● 高精确度
RTC通常具有备用电源,因此它们可以在主电源关闭或不可用时继续保持时间。 RTC通常使用32.768 kHz晶体振荡器。但为什么32,768? 32768等于2的15次方,因此可以轻松生成1秒。此外,晶体必须小,宽度适中,功耗低,使用32876 Hz即可满足要求。频率越高,晶体越脆弱,频率越低,功耗就越大。
DS1307模块功能和规格 DS1307模块是最经济实惠且最常见的RTC模块之一。它可以准确地跟踪秒、分、小时、日、月和年。
DS1307的一些重要功能包括: ● 生成可编程方波的能力 ● 低电流使用;电池备份模式下低于500nA ● 能够将日期设置为2100年 ● I2C串行接口
DS1307模块能够安装3伏CR2023备用电池。该模块上还有一个嵌入式EEPROM 24c32存储器,可以节省32kb数据。此外,您还可以通过在内置预留位置安装DS18B20传感器来测量环境温度。您还可以从BAT引脚读取备用电池电压。
连接DS1307到Arduino开发板 使用该模块很简单,RTC模块使用I2C协议与微控制器通信。
所需的材料 ● Arduino Uno R3开发板 ● DS1307模块 ● 跳线 ● Arduino IDE
电路
代码 您需要Adafruit的RTClib库才能将DS1307与Arduino配合使用。现在在电路板上上传以下代码,并在串行监视器窗口中查看结果。 以下代码是库的示例。 - // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
- #include <Wire.h>
- #include "RTClib.h"
-
- RTC_DS1307 rtc;
-
- char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
-
- void setup () {
- while (!Serial); // for Leonardo/Micro/Zero
-
- Serial.begin(57600);
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
-
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- }
-
- void loop () {
- DateTime now = rtc.now();
-
- Serial.print(now.year(), DEC);
- Serial.print('/');
- Serial.print(now.month(), DEC);
- Serial.print('/');
- Serial.print(now.day(), DEC);
- Serial.print(" (");
- Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- Serial.print(") ");
- Serial.print(now.hour(), DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.print(now.second(), DEC);
- Serial.println();
-
- Serial.print(" since midnight 1/1/1970 = ");
- Serial.print(now.unixtime());
- Serial.print("s = ");
- Serial.print(now.unixtime() / 86400L);
- Serial.println("d");
-
- // calculate a date which is 7 days and 30 seconds into the future
- DateTime future (now + TimeSpan(7, 12, 30, 6));
-
- Serial.print(" now + 7d + 30s: ");
- Serial.print(future.year(), DEC);
- Serial.print('/');
- Serial.print(future.month(), DEC);
- Serial.print('/');
- Serial.print(future.day(), DEC);
- Serial.print(' ');
- Serial.print(future.hour(), DEC);
- Serial.print(':');
- Serial.print(future.minute(), DEC);
- Serial.print(':');
- Serial.print(future.second(), DEC);
- Serial.println();
-
- Serial.println();
- delay(3000);
- }
复制代码您可以通过两种方式设置日期和时间。第一种方法是,使用以下代码在PC上设置它们: - rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
复制代码第二种方法是使用以下代码手动设置日期和时间: - rtc.adjust(DateTime(YEAR, MONTH, DAY, HOUR , MINUTE, SECOND));
复制代码注意:您只需在项目中设置一次日期和时间,之后,您应该从代码中删除相关的代码行。否则,只要您打开系统,就会设置日期和时间,这可能会导致一些错误。
制作一个智能的提醒器 您可以使用DS1307模块制作一个简单而有用的提醒器。
所需的材料 ● Arduino Uno R3开发板 ● DS1307模块 ● LCD键盘模块 ● 被动式蜂鸣器 ● 跳线 ● Arduino IDE
电路
代码 在您的Arduino开发板上传以下代码后,您的智能提醒器准备就绪! - /*
-
- Arduino Reminder
-
- modified on 13 March 2019
- by Saeed Hosseini @ Electropeak
- https://electropeak.com/learn/
-
- */
-
- #include <LiquidCrystal.h>
- #include <Wire.h>
- #include "RTClib.h"
-
-
- #define NOTE_C4 262
- #define NOTE_D4 294
- #define NOTE_E4 330
- #define NOTE_F4 349
- #define NOTE_G4 392
- #define NOTE_A4 440
- #define NOTE_B4 494
- #define NOTE_C5 523
- #define NOTE_D5 587
- #define NOTE_E5 659
- #define NOTE_F5 698
- #define NOTE_G5 784
- #define NOTE_A5 880
- #define NOTE_B5 988
-
- //LCD Pin to Arduino
- const int pin_RS = 8;
- const int pin_EN = 9;
- const int pin_d4 = 4;
- const int pin_d5 = 5;
- const int pin_d6 = 6;
- const int pin_d7 = 7;
- const int pin_BL = 10;
- //BUZZER Pin to Arduino
- const int buzzer = 2;
-
- LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
- RTC_DS1307 rtc;
-
- byte smiley[8] = {
- B00000,
- B10001,
- B00000,
- B00000,
- B10001,
- B01110,
- B00000,
- };
-
-
- String menuItems[] = {"1-Medicin", "2-Wake up", "3-Go out"};
-
- int songspeed = 1.5;
-
- int flag = 0;
- int menuPage = 0;
- int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
- int cursorPosition = 0;
- int btn;
- int h = 0, m = 0;
-
- int duration[] = { //duration of each note (in ms) Quarter Note is set to 250 ms
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 125,
-
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 125,
-
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 125, 250, 125,
-
- 125, 125, 250, 125, 125,
- 250, 125, 250, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 375,
-
- 250, 125,
- //Rpeat of First Part
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 125,
-
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 125,
-
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 125, 250, 125,
-
- 125, 125, 250, 125, 125,
- 250, 125, 250, 125,
- 125, 125, 250, 125, 125,
- 125, 125, 375, 375,
- //End of Repeat
-
- 250, 125, 375, 250, 125, 375,
- 125, 125, 125, 125, 125, 125, 125, 125, 375,
- 250, 125, 375, 250, 125, 375,
- 125, 125, 125, 125, 125, 500,
-
- 250, 125, 375, 250, 125, 375,
- 125, 125, 125, 125, 125, 125, 125, 125, 375,
- 250, 125, 375, 250, 125, 375,
- 125, 125, 125, 125, 125, 500
- };
-
-
- int notes[] = { //Note of the song, 0 is a rest/pulse
- NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
- NOTE_A4, NOTE_G4, NOTE_A4, 0,
-
- NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
- NOTE_A4, NOTE_G4, NOTE_A4, 0,
-
- NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
- NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
- NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
- NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
-
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_D5, NOTE_E5, NOTE_A4, 0,
- NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
- NOTE_C5, NOTE_A4, NOTE_B4, 0,
-
- NOTE_A4, NOTE_A4,
- //Repeat of first part
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
- NOTE_A4, NOTE_G4, NOTE_A4, 0,
-
- NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
- NOTE_A4, NOTE_G4, NOTE_A4, 0,
-
- NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
- NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
- NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
- NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
-
- NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
- NOTE_D5, NOTE_E5, NOTE_A4, 0,
- NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
- NOTE_C5, NOTE_A4, NOTE_B4, 0,
- //End of Repeat
-
- NOTE_E5, 0, 0, NOTE_F5, 0, 0,
- NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
- NOTE_D5, 0, 0, NOTE_C5, 0, 0,
- NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
-
- NOTE_E5, 0, 0, NOTE_F5, 0, 0,
- NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
- NOTE_D5, 0, 0, NOTE_C5, 0, 0,
- NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4
- };
-
- void show_time()
- {
- lcd.clear();
- DateTime now = rtc.now();
- lcd.setCursor(0, 0);
- lcd.print("* Electropeak *");
- lcd.setCursor(5, 0);
- lcd.print(now.hour(), DEC);
- lcd.print(":");
- lcd.print(now.minute(), DEC);
- if (now.hour() == h && now.minute() == m)
- {
- lcd.clear();
- lcd.setCursor(0 , 0);
- switch (flag)
- {
- case 1:
- lcd.print("** ءedicine Time **");
- break;
- case 2:
- lcd.print("You'r late!!");
- lcd.setCursor(0, 1);
- lcd.print("Come onnnnnnn");
- break;
- case 3:
- lcd.print("Befor you go:");
- lcd.setCursor(0, 1);
- lcd.print("Wallet,Keys,...");
- break;
- }
- for (int i = 0; i < 203; i++) { //203 is the total number of music notes in the song
- int wait = duration[i] * songspeed;
- tone(buzzer, notes[i], wait); //tone(pin,frequency,duration)
- delay(wait);
- }
- }
- delay(2000);
- }
-
- void welcome_menu(int show_delay)
- {
- lcd.setCursor(0, 0);
- lcd.print("Arduino reminder");
- lcd.createChar(1, smiley);
- lcd.setCursor(7, 1);
- lcd.write(1);
- delay(show_delay);
- lcd.clear();
- }
-
- int detect_button() {
-
- int readkey = 0;
- int result = 0;
- int activeButton = 0;
-
- while (activeButton == 0) {
- readkey = analogRead(0);
- if (readkey < 790) {
- delay(100);
- readkey = analogRead(0);
- if (readkey < 60) {
- result = 1; // right
- } else if (readkey < 200) {
- result = 2; // up
- } else if (readkey < 400) {
- result = 3; // down
- } else if (readkey < 600) {
- result = 4; // down
- } else if (readkey < 800) { result = 5; // select } return result; } } activeButton = 1; } int choose_menu() { lcd.clear(); lcd.print("Remmeber what?"); while (btn != 5) { btn = detect_button(); if (btn == 1) { if (menuPage > maxMenuPages) menuPage = 0;
- lcd.clear();
- lcd.print("Remmeber what?");
- lcd.setCursor(0, 1);
- lcd.print(menuItems[menuPage]);
- menuPage = menuPage + 1;
- }
- }
-
- return menuPage;
- }
-
-
- void set_alarm() {
- int sit = 0;
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("**Set Alarm**");
- lcd.setCursor(5, 1);
- lcd.print("00:00");
- while (sit != 1)
- {
- sit = detect_button();
-
- if (sit == 2)
- {
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("**Set Alarm**");
- h++;
- if (h > 23)
- {
- h = 0;
- }
- lcd.setCursor(5, 1);
- lcd.print(h);
- lcd.print(":00");
-
- }
- else if (sit == 3)
- {
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("**Set Alarm**");
- h--;
- if (h < 0) { h = 23; } lcd.setCursor(5, 1); lcd.print(h); lcd.print(":00"); } } while (sit != 5) { sit = detect_button(); if (sit == 2) { lcd.clear(); lcd.setCursor(1, 0); lcd.print("**Set Alarm**"); m++; if (m > 59)
- {
- m = 0;
- }
- lcd.setCursor(5, 1);
- lcd.print(h);
- lcd.print(":");
- lcd.print(m);
-
- }
- else if (sit == 3)
- {
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print("**Set Alarm**");
- m--;
- if (m < 0)
- {
- m = 59;
- }
- lcd.setCursor(5, 1);
- lcd.print(h);
- lcd.print(":");
- lcd.print(m);
-
- }
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Alarm set");
- lcd.setCursor(0, 1);
- lcd.print("I'll be on time");
- delay(1500);
-
- }
-
-
- void setup() {
-
- lcd.begin(16, 2);
- rtc.begin();
- pinMode(buzzer, OUTPUT);
- welcome_menu(1000);
- flag = choose_menu();
- set_alarm();
-
-
-
- }
- void loop()
- {
-
- show_time();
-
- }
复制代码打开设备后,LCD上将显示一个菜单。 选择提醒器模式(您可以通过右键在选项之间移动,然后按选择键选择所需的模式。)然后设置您的闹钟时间(通过向上和向下键设置小时,然后按向右键设置小时并设置它 。)现在可以毫无顾虑地完成您的日常工作!
以上就是本篇文章的全部内容。如果遇到问题,请随时在本帖下面进行回复。 |