Giao tiếp Module RTC đồng hồ thời gian thực PCF8563 với Arduino

Tính năng mô-đun RTC PCF8563

PCF8563T là một mạch thời gian thực cung cấp chức năng ngày, tháng, năm và thời gian thực. Với pin được gắn trên bo mạch có khả năng làm việc độc lập ngay cả khi MCU ngừng hoạt động.

Mạch thời gian thực này có các chân kết nối hỗ trợ giao diện I2C, thuận tiện cho việc kết nối với MCU, có thể kết nối nhiều mô-đun trên I2C bus trong cùng một thời điểm. Mach hoạt động ổn định và chính xác hơn so với chip DS1302 hoặc DS1307.

Thông số kỹ thuật

  • Chip: PCF8563
  • Điện áp làm việc: 3,3V/5V
  • Tần số xung nhịp tối đa: 400KHz
  • Giao tiếp: I2C

Sơ đồ chân

PIN Description
INT Interrupt output
COT Clock output
VCC 3.3V/5V
GND Ground
SDA I2C Data input
SCL I2C Clock pin

Kết nối mô-đun RTC PCF8563 với Arduino

Bước 1: Kết nối mạch

PCF8563T Arduino
VCC 3.3V/5V
GND GND
SDA A4
SCL A5

Bước 2: Cài đặt thư viện

  1. Mở Arduino IDE và chọn "Sketch" -> "Include Library" -> "Manage Libraries...".
  2. Trong ô tìm kiếm, nhập "Rtc_Pcf8563".
  3. Chọn thư viện "Rtc_Pcf8563 by Joe Robertson" và nhấn nút "Install".

Bước 3: Code

Tải đoạn mã sau lên Arduino. Sau đó mở Serial Monitor.

 /*  
src: https://www.akysu.com/2024/01/giao-tiep-module-rtc-pcf8563-voi-arduino.html
*/

#include <Wire.h>
#include <Rtc_Pcf8563.h>

//init the real-time clock
Rtc_Pcf8563 rtc;
 
 
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  
  //clear out all the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century, year
  rtc.setDate(11, 2, 1, 20, 24);
  //hr, min, sec
  rtc.setTime(11, 12, 0);
}
                                                                                
void loop()
{

  Serial.print("Time:");
  //Serial.print(rtc.formatTime(RTCC_TIME_HM));
  Serial.print(rtc.formatTime());
  
  Serial.print("\t Date:");
  //Serial.println(rtc.formatDate(RTCC_DATE_ASIA));
  Serial.println(rtc.formatDate());

  delay(1000);   
}

Đầu ra như sau.

Keywords

#######################################
# Syntax Coloring Map For Rtc_Pcf8563
####################################### 
# Datatypes (KEYWORD1)
#######################################

Rtc_Pcf8563 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

initClock       KEYWORD2
clearStatus     KEYWORD2
getDate         KEYWORD2
setDate         KEYWORD2
getTime         KEYWORD2
setTime         KEYWORD2
setAlarm        KEYWORD2
clearAlarm      KEYWORD2
getStatus1      KEYWORD2
getStatus2      KEYWORD2
getSecond       KEYWORD2
getMinute       KEYWORD2
getHour         KEYWORD2
getDay          KEYWORD2
getWeekday      KEYWORD2
getMonth        KEYWORD2
getYear         KEYWORD2
formatTime      KEYWORD2
formatDate      KEYWORD2
alarmEnabled    KEYWORD2
alarmActive     KEYWORD2
enableAlarm     KEYWORD2
resetAlarm      KEYWORD2
setSquareWave   KEYWORD2
clearSquareWave KEYWORD2
getAlarmMinute  KEYWORD2
getAlarmHour    KEYWORD2
getAlarmDay     KEYWORD2
getAlarmWeekday KEYWORD2
version         KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
RTCC_DATE_WORLD  LITERAL1
RTCC_DATE_ASIA   LITERAL1
RTCC_DATE_US     LITERAL1
RTCC_TIME_HMS    LITERAL1
RTCC_TIME_HM     LITERAL1

Rtc_Pcf8563.h

/*****
 *  NAME
 *    External Real Time Clock support routines
 *  AUTHOR
 *    Joe Robertson, jmr
 *    orbitalair@gmail.com
 *    
 *  CREATION DATE
 *    1/2/10,  init - built off of pic rtc code
 *  NOTES
 *  HISTORY
 *    1/2/10  ported to arduino compiler, jmr
 *    2/14/10 added 3 world date formats, jmr
 *    28/02/2012 A. Pasotti
 *            fixed a bug in RTCC_ALARM_AF,
 *            added a few (not really useful) methods
 *    12/26/12 added version method to be able to track builds. jmr
 *             fix for arduino 1.0.1. Wire.
 *    
 *  TODO
 *
 ******
 *  Robodoc embedded documentation.
 *  http://www.xs4all.nl/~rfsber/Robo/robodoc.html
 */
/*  Copyright (c) 2006-2020 Joe Robertson. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * or https://www.gnu.org/licenses/lgpl-3.0.html
 */
 
   
#ifndef Rtc_Pcf8563_H
#define Rtc_Pcf8563_H

#include "Arduino.h"
#include "Wire.h"

#define RTCC_VERSION  "Pcf8563 v1.0.3"

/* the read and write values for pcf8563 rtcc */
/* these are adjusted for arduino */
#define RTCC_R  0xa3
#define RTCC_W  0xa2

#define RTCC_SEC            1
#define RTCC_MIN            2
#define RTCC_HR             3
#define RTCC_DAY            4
#define RTCC_WEEKDAY    5
#define RTCC_MONTH      6
#define RTCC_YEAR       7
#define RTCC_CENTURY    8

/* register addresses in the rtc */
#define RTCC_STAT1_ADDR         0x0
#define RTCC_STAT2_ADDR         0x01
#define RTCC_SEC_ADDR           0x02
#define RTCC_MIN_ADDR           0x03
#define RTCC_HR_ADDR            0x04
#define RTCC_DAY_ADDR           0x05
#define RTCC_WEEKDAY_ADDR       0x06
#define RTCC_MONTH_ADDR         0x07
#define RTCC_YEAR_ADDR          0x08
#define RTCC_ALRM_MIN_ADDR      0x09
#define RTCC_SQW_ADDR           0x0D

/* setting the alarm flag to 0 enables the alarm.
 * set it to 1 to disable the alarm for that value.
 */
#define RTCC_ALARM              0x80
#define RTCC_ALARM_AIE          0x02
#define RTCC_ALARM_AF           0x08 // 0x08 : not 0x04!!!!
/* optional val for no alarm setting */
#define RTCC_NO_ALARM               99

#define RTCC_CENTURY_MASK   0x80

/* date format flags */
#define RTCC_DATE_WORLD         0x01
#define RTCC_DATE_ASIA          0x02
#define RTCC_DATE_US                0x04
/* time format flags */
#define RTCC_TIME_HMS               0x01
#define RTCC_TIME_HM                0x02

/* square wave contants */
#define SQW_DISABLE     B00000000
#define SQW_32KHZ       B10000000
#define SQW_1024HZ      B10000001
#define SQW_32HZ        B10000010
#define SQW_1HZ         B10000011


/* arduino class */
class Rtc_Pcf8563 {
    public:
        Rtc_Pcf8563();

        void initClock();   /* zero out all values, disable all alarms */
        void clearStatus(); /* set both status bytes to zero */

        void getDate();         /* get date vals to local vars */
        void setDate(byte day, byte weekday, byte month, byte century, byte year);
        void getTime();    /* get time vars + 2 status bytes to local vars */
        void getAlarm();
        void setTime(byte sec, byte minute, byte hour);
        byte readStatus2();
        boolean alarmEnabled();
        boolean alarmActive();

        void enableAlarm(); /* activate alarm flag and interrupt */
        void setAlarm(byte min, byte hour, byte day, byte weekday); /* set alarm vals, 99=ignore */
        void clearAlarm();  /* clear alarm flag and interrupt */
        void resetAlarm();  /* clear alarm flag but leave interrupt unchanged */
        void setSquareWave(byte frequency);
        void clearSquareWave();

        byte getSecond();
        byte getMinute();
        byte getHour();
        byte getDay();
        byte getMonth();
        byte getYear();
        byte getWeekday();
        byte getStatus1();
        byte getStatus2();

        byte getAlarmMinute();
        byte getAlarmHour();
        byte getAlarmDay();
        byte getAlarmWeekday();

        /*get a output string, these call getTime/getDate for latest vals */
        char *formatTime(byte style=RTCC_TIME_HMS);
        /* date supports 3 styles as listed in the wikipedia page about world date/time. */
        char *formatDate(byte style=RTCC_DATE_US);

        char *version();
        
    private:
        /* methods */
        byte decToBcd(byte value);
        byte bcdToDec(byte value);
        /* time variables */
        byte hour;
        byte minute;
        byte sec;
        byte day;
        byte weekday;
        byte month;
        byte year;
        /* alarm */
        byte alarm_hour;
        byte alarm_minute;
        byte alarm_weekday;
        byte alarm_day;
        /* support */
        byte status1;
        byte status2;
        byte century;

        char strOut[9];
        char strDate[11];

        int Rtcc_Addr;
};

#endif

Nhận xét

Mới hơn Cũ hơn