51 MCU Study Notes——13.1DS1302 Real-time Clock Principle

Publisher:云淡风轻2014Latest update time:2022-06-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

DS1302 Real Time Clock

Chip schematic:

insert image description here

Pin Diagram:

insert image description here

It should be noted that both VCC1 and VCC2 are connected to external capacitors, but the capacity is different. The capacitance value of the normal backup power supply VCC2 is 100 times that of VCC1. This is not difficult to understand. If the power is cut off, VCC2 will be able to provide power for a longer period of time.


VCC1 (main power supply) --------------Filtering function

VCC2 (backup power supply) -------- discharge to provide power when power is off

Connect the X1 and X2 pins to a crystal oscillator to obtain real-time time

insert image description here

Different pins have different functions. We only introduce some special pins here. If you need them, please refer to the official data sheet.

There are 8 registers inside DS1302, which are responsible for seconds, minutes, hours, days, weeks, months, years and a write protection.

We will introduce them in turn, first looking at the schematic diagram:

insert image description here
insert image description here

Register 0:

insert image description here

The "CH" bit in "Bit7" indicates that the clock starts timing, and 1 indicates that the clock stops timing.

"Bit4~Bit6" represents the tens digit of seconds

"Bit0~Bit3" represents the units digit of seconds


Register 1:

insert image description here

"Bit4~Bit6" indicates the tens digit of the minute

"Bit0~Bit3" represents the unit digit of the minute


Register 2:

insert image description here

Bit7:


1: represents the 12-hour system, divided into morning and afternoon

2: represents 24-hour system

Bit5: When the display is in 12-hour format:


0: indicates morning

1: Indicates afternoon

Bit4: Used together with Bit5 to indicate the tens digit of the hour


Register 7:

insert image description here

Bit7: Write protection bit. If it is 1, writing data is prohibited.

The data is stored in the clock chip in the form of "BCD" code. If we want to display the number "59" on the digital tube, we will display "5" and "9" on different digital tubes respectively, because the data inside the microcontroller is in binary form, that is, it is either 0 or 1.

insert image description here

The numbers we display on the computer actually correspond to ASCII code table values.

insert image description here

Timing diagram:

The rising arrow refers to DS1302

insert image description here
insert image description here
insert image description here

Single byte write/read operation timing diagram:

The first byte: indicates which register to read or write by writing.


Second byte: read and write data


Write Operation:


 MCU----》Data----》`I/O` port----`DS1302`


Read Operation:


 `DS1302`----》Data-----》`I/O` port----MCU


Write Operation:

When writing, the data sent by the microcontroller to the I/O port is binary, so it is necessary to detect the level state of the I/O port to determine whether it is low or high.

The write operation is when the SCLK is low and the microcontroller puts the data on the I/O port. When the SCLK rises, the DS1302 reads the data.


void DS1302Write(unsigned int reg,unsigned int data)

{

unsigned char detect;

DS1302RST = 1; //Only high level can trigger

for(detect = 0x01; detect!=0; detect<<=1) // write address

{

if((detect®)!=0)

{

DS1302DAT = 1;

}

else

{

DS1302DAT = 0; //IO status

}

DS1302CK = 1;

delay();

DS1302CK = 0;

delay();

}

for(detect = 0x01; detect!=0; detect<<1) // write address

{

if((detect&data)!=0)

{

DS1302DAT = 1; //IO port status

}

else

{

DS1302DAT = 0;

}

DS1302CK = 1;

delay();

DS1302CK = 0;

delay();

}

}


Read Operation:


The read operation is the microcontroller's judgment of the state of the I/O port. The microcontroller reads the binary data sent by DS1302 to the I/O port, so it is necessary to judge the high and low of the I/O port one by one.

The read operation is when the SCLK is high, the DS1302 puts data on the I/O port. When SCLK is set to low, the microcontroller can read data from the I/O port.


unsigned int DS1302Read(unsigned int addr) // write address only

{

DS1302RST = 1; // Turn on the main switch

unsigned int detect;

unsigned int dat = 0;

for(detect = 0x01; detect!=0; detect<<=1) // write address

{

if((detect&addr)!=0)

{

DS1302DAT = 1;

}

else

{

DS1302DAT = 0;

}

DS1302CK = 1;

delay();

DS1302CK = 0;

delauy();

}

for(detect = 0x01;detect!=0;detect<<=1)

{

if(DS1302DAT!=0)

{

that|=detect;//

}

DS1302CK = 1; //pull high

delay();

DS1302CK = 0; //Pull low to complete a bit transmission

delay();

}

DS1302RST = 0; //The total level is set low to complete the transmission

return dat; // return data

}


Summarize:

MCU read and write operations on DS1302

In fact, it is the microcontroller's judgment of the I/O port status.

Then pull the second bus high and low

Reference address:51 MCU Study Notes——13.1DS1302 Real-time Clock Principle

Previous article:51 MCU Study Notes——13.2DS1302 Real-time Clock Code Part (1)
Next article:51 MCU Study Notes——12.1 Simulation Method for Serial Communication

Recommended ReadingLatest update time:2024-11-16 17:52

PIC program for clock chip DS1302
  include p16f877.inc   CBLOCK 20H   SEC ;   MIN ;   HOU ;   DATE ;   MON ;  DAY ;   YERR ;   DDD ; write enable bit   TIME_TX ; 1302 send register   TIME_RX ; 1302 receive register   COUNT1 ;   COUNT2 ;   DELAY1   DELAY2   ENDC ;================================= ;1302 subroutine description ; DS1
[Microcontroller]
PIC16F877 drives DS1302 chip
It only took me one morning to modify the program from the Internet. It seems that modifying other people's programs is much faster than writing them from scratch! Below is the schematic diagram   The following is the program header file #ifndef DS1302_H #define DS1302_H #include "main.h" #define Time_
[Microcontroller]
ds1302 clock-AVR program code
#include  #include "DS1302.h" #include    void ds1302_init(void) {     PORTX=0x00;     DDRX=0x07; }   void write_ds1302_byte(unsigned char dat) {          unsigned char i;     for(i=0;i 8;i++)     {     SCK=0;     IO_out=dat&0x01;     that=that 1;      SCK=1;     } }   void write_ds1302(unsigned char add,unsigned char
[Microcontroller]
C51 driver for ds1302
#include "D:\reg51.h " //header file #define uchar unsigned char uchar settime ={1,2,3,4,5,6,7}; uchar readtime ;  //******************************************************************** sbit T_CLK = P1^1; //real-time clock clock line pin  sbit T_IO = P1^0; //real-time clock data line pin  sbit T_RST = P1^2; //real-tim
[Microcontroller]
PIC16F877A routine --- DS1302 program
#include       //Call header file //#include pic1687x.h __CONFIG(0x3F32); //Chip configuration word //__CONFIG(HS&UNPROTECT&PWRTEN&BORDIS&WDTDIS);//Chip configuration word //__CONFIG(0x3FFD&0x3FFF&0x3FF7&0x3FBF&0x3FFB);//Chip configuration word #define      uchar  unsigned      char #define uint    unsigned
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号