当前位置:网站首页>[Arduino connects the clock module to display the time on LCD1602]

[Arduino connects the clock module to display the time on LCD1602]

2022-08-02 04:33:00 WENJIE Technology

前言

Time is of the essence in today's society,对于某些电子产品,时间至关重要;就像我们一样,They need a way to keep track of time and precise time.那么,How does electronics do this?实时时钟或 RTC is embedded in an integrated circuit or IC 中的计时设备.答案是 DS1307.Many time-critical applications and devices rely on it,包括服务器、GPS 和数据记录器.

关于 DS1307

DS1307 是具有 56 字节 SRAM battery powered clock/日历.时钟/calendar in seconds、分钟、小时、天、日期、Month and year display data.The end date of each month changes automatically,Especially for less than 31 天的月份.

They are integrated circuits (IC) 的形式出现,Time can be controlled like a clock,Control dates like a calendar.RTC The main advantage is that it has a battery backup system,Keeps the clock even when power is lost/Calendar works fine.RTC Requires a small amount of power to stay active.这些 RTC Can be found in various applications,Includes embedded devices and computer motherboards.

DS1307 引脚排列

在这里插入图片描述SQ==》This pin can be programmed to emit one of four square wave frequencies:1Hz、4kHz、8kHz 或 32kHz.
DS==》If your module is installed next to the battery holder DS18B20 温度传感器,Then this pin is designed to output temperature information(标记为 U1).
SCL==》是 I2C Clock input for the interface,Used on a synchronous serial interface​​的数据传输.
SDA==》是 I2C 串行接口的数据输入/输出.
VCC==》This pin powers the module.它的范围可以从 3.3 到 5.5 伏.
GND==》是接地引脚.
BAT==》is any standard 3V Backup power input for lithium batteries or other energy sources,Allows gadgets to keep accurate time when mains power is lost.

DS1307 模块元件

DS1307 RTC 模块包括 DS3107 All onboard components required for the chip to function properly.此外,它还有一个 20mm 3V Holder for lithium coin cell batteries.This module can use any CR2032 电池.Let's go through each component of the module one by one.DS1307 记录秒、分、小时、日和月.The chip resets its seconds at the end of each month、分、hour and date.时间可以用 AM 和 PM 的 12 小时格式或 24 小时格式显示.

32kHz 晶体振荡器:

DS1307 芯片需要一个 32KHz external crystal oscillator to work(计时).因此,RTC 模块有一个 32KHz 的外部晶振.但是,这个 32KHz There is a problem with the crystal oscillator:Changes in ambient temperature will change the oscillation frequency of the crystal.32KHzThe difference in the oscillation frequency of the external crystal is insignificant.但是,从长远来看,这是一个错误.It causes monthly clock drift 2-3 分钟.

板载 24C32 EEPROM:

DS1307 RTC The modules also have onboard 24C32 EEPROM.该 EEPROM 具有 32 字节容量,Only regional read and write operations are allowed.Use for alert based projects RTC Modules can take advantage of this memory to save time.例如,We want it every morning 8:00 起床,We can record this time value at EEPROM 中,As long as the time is equal to the saved value,就​​An alarm will sound.

这些 EEPROM 芯片使用 I2C 接口与 Arduino Wait for the microcontroller to connect.因此,它使用与 DS1307 相同的 I2C 总线.To be the same I2C 总线上的 EEPROM (o 0x50 Hex) 和 DS1307 芯片进行通信,Different slave addresses are set for them.

备用电池:

在这里插入图片描述
用于连接 CR2032 The bracket for the coil unit is located RTC the back of the module.即使连接到 DS1307 The main power supply has failed,This backup battery also keeps accurate time.The chip has a power sensor circuit,Detects mains power and switches to backup coil unit if mains power is lost.

DS18B20 传感器:

外部 DS18B20 Digital temperature sensors can be connected to empty slots on this module.The three empty pins in the lower right corner are used as DS18B20 Placeholder for the sensor,Its output is passable RTC 模块的 DS 引脚接收.

DS1307 Schematic of the sensor module

在这里插入图片描述

对 DS1307 进行编程和设置时间

in the settings below DS1307 模块连接到 Arduino is all you need to set the time in the module.
在这里插入图片描述完成上述连接后,您需要将 Arduino Uno 连接到您的 PC,打开 Arduino IDE,并安装 Arduino DS1307 Time Set Library.打开 Arduino IDE 并从菜单栏中选择 Library Manager.现在查找 RTCLib 并获取最新版本,如下图所示.
在这里插入图片描述代码很容易理解.The time will be set and displayed on the serial monitor.

rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

在这一行中,rtc The object adjusts the time according to the time on your machine.It will modify the time on your computer.

rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

You can manually set the time for this row by providing the function with the following datetime values:年、月、日、小时、分钟和秒.在下面的代码中,We will set the system time.结果,这一行被注释掉了.

#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 rtc;
char t[32];
void setup()
{
    
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  //rtc.adjust(DateTime(2019, 1, 21, 5, 0, 0));
}
void loop()
{
    
  DateTime now = rtc.now();
  sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());  
  Serial.print(F("Date/Time: "));
  Serial.println(t);
  delay(1000);
}

Now we know how to set the time in the module,Let's use it and LCD to make a simple clock

在 LCD 上显示时间的代码

Wire.h用于通过 I2C 与模块连接,LiquidCrystal_I2C.h用于在 LCD The time is shown on the display,RTClib.h Used to set and format the time on the display.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 
#include <RTClib.h>

此行设置 16x2 LCD The address of the display,以通过 I2C 协议进行通信.

LiquidCrystal_I2C lcd(0x27,16,2);

If the project starts with a disconnected connection,The code will report DS1307 RTC Module is not Present on the serial monitor.

if (! rtc.begin())
  {
    
    Serial.println("DS1307 RTC Module is not Present");
    while (1);
  }

如果 RTC Power was lost and the time in the module was incorrect,The code will automatically adjust the time in the module using the computer clock.因此,在设置时间时,Make sure the clock on your computer is set to the correct time.

if (! rtc.begin())
  {
    
    Serial.println("DS1307 RTC Module is not Present");
    while (1);
  }

This code block will LCD The cursor resets to 0,and by date/月/Output date in year format.

void displayTime()
 {
    
  lcd.setCursor(0,0);
  lcd.print("Time:");
  lcd.print(now.hour());
  lcd.print(':');
  lcd.print(now.minute());
  lcd.print(':');
  lcd.print(now.second());
  lcd.print(" ");
 }

In this part of the code the cursor is set to 1,时间以 Hour: Minute: Second 格式打印.

void displayDate()
 {
    
  lcd.setCursor(0,1);
  lcd.print("Date:");
  lcd.print(now.day());
  lcd.print('/');
  lcd.print(now.month());
  lcd.print('/');
  lcd.print(now.year());
 }

输入代码后,The date and time will appear LCD 屏幕上.
在这里插入图片描述

Schematic and code

The full schematic and code have been uploaded to the personal homepage.

原网站

版权声明
本文为[WENJIE Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020323554329.html