Arduino drives DS1302 display clock

foreword

There are many popular serial clock circuits, such as DS1302, DS3231, DS1307, PCF8485, etc. They are widely used due to their simple interface, low cost and ease of use. In this experiment, we will use the DS1302 real-time clock module. DS1302 is a high-performance, low-power, real-time clock circuit with RAM. It can time the year, month, day, week, hour, minute, and second. With leap year compensation function.
insert image description here

Electrical parameters

Wide range of working voltage 2.0 -5.5V
Working current At 2.0V, less than 300nA
charset mode single-byte transfer and multi-byte transfer
Operating temperature -40℃~+85℃

Classic Application Circuit

insert image description here
DS1302 uses a three-wire interface to communicate synchronously with the CPU, and can transmit multiple bytes of clock signals or RAM data at a time in burst mode. DS1302 has a 31×8 RAM register for temporary storage of data. DS1302 is an upgraded product of DS1202, which is compatible with DS1202, but adds dual power supply pins for main power supply and backup power supply, and also provides the ability to charge the backup power supply with trickle current.

wiring

Arduino LCD1602 DS1302
5V VCC VCC
A4 SDA -
A5 SCL -
5 - RST
6 - THAT
7 - CLK
GND GND GND

program

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <DS1302.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // 将LCD地址设置为0x27,用于16个字符和2行显示

namespace {
    
    

const int kCePin   = 5;  
const int kIoPin   = 6; 
const int kSclkPin = 7;   
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
    
    
  switch (day) {
    
    
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
    
    
  // 获取当前时间和日期
  
  Time t = rtc.time();


  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer. //格式化时间和日期并插入临时缓冲区。
  char buf[50];   //定义一个字符型数组用来存放日期数据(用于串口打印)
  char DateBuf[12];  //定义一个字符型数组用来存放日期数据(由于1602每行只能打印16个字符,所以日期和时间分两个数组存)
  char TimeBuf[10];//定义一个字符型数组用来存放时间数据(由于1602每行只能打印16个字符,所以日期和时间分两个数组存)
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",day.c_str(),t.yr, t.mon, t.date,t.hr, t.min, t.sec);
   //把日期和时间数据存放到数组中
  snprintf(DateBuf,sizeof(DateBuf),"%04d-%02d-%02d",t.yr, t.mon, t.date); //把日期数据存放到数组中
  snprintf(TimeBuf,sizeof(TimeBuf),"%02d:%02d:%02d",t.hr, t.min, t.sec);  //把时间数据存放到数组中     
  Serial.println(buf);  //串口打印输出日期与时间
  lcd.setCursor(2,0);   //设置1602的光标到第0行第2个字符处
  lcd.print(DateBuf);   //在LCD光标所在处打印DateBuf数组里的数据
  lcd.setCursor(3,1);   //设置1602的光标到第1行第3个字符处
  lcd.print(TimeBuf);   //在LCD光标所在处打印TimeBuf数组里的数据
}

}  
void setup() {
    
    
  lcd.init(); //初始化LCD
  lcd.backlight();  //打开LCD背光
  Serial.begin(9600); //初始化串口
  rtc.writeProtect(false);
  rtc.halt(false);

  // Make a new time object to set the date and time.
  // Wednesday, March 2, 2023 at 16:49:46.
  Time t(2023, 3, 2, 16, 49, 46, Time::kWednesday);

  // Set the time and date on the chip.
  rtc.time(t);
}

// 每秒钟循环并打印一次时间。
void loop() {
    
    
  printTime();
  delay(1000);
}

Experimental results

insert image description here

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/129427203