DS1302 Real Time Clock
Chip schematic:
Pin Diagram:
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
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:
Register 0:
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:
"Bit4~Bit6" indicates the tens digit of the minute
"Bit0~Bit3" represents the unit digit of the minute
Register 2:
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:
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.
The numbers we display on the computer actually correspond to ASCII code table values.
Timing diagram:
The rising arrow refers to DS1302
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
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
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Program-controlled constant current source simulation is incorrect
- EEWORLD University Hall----Live Replay: NXP LPC5500 Series MCU
- [RVB2601 Creative Application Development] + Smart Alarm Clock
- What technologies are there in sensor networks?
- MicroPython Flip Computer
- First picture of the start-up - high voltage motor drive
- [RVB2601 Creative Application Development] VI. Sound Playback Test of RVB2601
- MicroPython adds instructions for pyboard.py
- SD/MMC SPI mode command set
- DS90UB954 debugging problem help