I Description
In daily life, calendar clocks are used in various places. Such as shopping malls, supermarkets, offices, homes, schools, etc. Compared with the traditional mechanical clock, the digital calendar clock has a series of advantages. Such as high precision, intuitive display, and long life. This blog introduces a calendar clock designed with a DS1302 rtc chip.
DS1302 RTC with Arduino Tutorial
Catalog
II System Hardware Design
2.1 Overall Structure
Take AT89C51 single-chip microcomputer as the main controller, and use DS1302 clock chip to design calendar clock. Its overall structure is shown in Figure 1.
Figure 1. Overall Structure of System
The designed calendar clock must not only display hours, minutes and seconds, but also display years, months, days and weeks. The core of the system is the AT89C51 microcontroller. Through the single-chip computer control DS1302 display calendar and time. And the output result is displayed on LCD1602 liquid crystal screen.
2.2 DS1302 Clock Module
The DS1302 clock chip has the characteristics of low power consumption and high performance. It can communicate with the microcontroller through a simple synchronous serial mode, and only requires three I/O lines. Namely reset (RST), I/O data line and serial clock (SCLK)
2.2.1 DS1302 Pins and Structure
Figure 2 shows the external pins and functions of DS1302.
Figure 2. DS1302 Pinout
- VCC2-main power supply pin; X1, X2-32.768kHz crystal oscillator pin; GND-ground;
- VCC1-battery pin; SCLK-serial clock; I/O-data input/output; RST-reset.
The internal structure of DS1302 is shown in Figure 3, which is mainly composed of the following parts: real-time clock, data memory RAM, oscillator circuit and frequency divider, input shift register, command and control logic and so on.
2.2.2 DS1302 Registers and control commands
The DS1302 clock chip has 7 registers related to the calendar clock, as shown in Table 1.
The communication signal between DS1302 and single-chip microcomputer is realized through simple synchronous serial communication.
According to the working timing requirements of DS1302, whether the single-chip microcomputer performs read operation communication from DS1302 or the single-chip computer performs write operation communication to DS1302, each communication is initiated by the single-chip computer first.
In other words, before executing the corresponding read or write operation, the microcontroller must write a byte of command word to DS1302. The eight-bit data of the byte command word is shown in Figure 4.
Figure 4. Command word structure of DS1302
2.3 LCD1602 LCD Module
The LCD1602 screen can display two lines of characters, 16 characters per line, for a total of 32 characters. There is an 80*8-bit display data memory DDRAM buffer in LCD1602. See Table 2 for the correspondence between character display bits and DDRAM address.
The address on the first line of DDRAM starts at 00H and ends at 27H. The addresses on the second line start at 40H and end at 67H, with 40 addresses per line. And LCD1602 displays 16 characters per line. Therefore, when writing a program, select the first 16 addresses of DDRAM. It is important to note that the second line address starts from 40H.
If you want to display a character in a certain row and certain column of the LCD1602 screen, write the ASCII code corresponding to this character into the corresponding DDRAM address of a certain row and certain column. At this time, you will find that the character cannot be displayed normally on the LCD screen. The reason is that 80H must be added to the address.
For example, to display the symbol "V" in the second row and second column of the voltage unit volts, first add 80H to the corresponding DDRAM address 41H in the second row and second column, that is, C1H. Then write the ASCII code 0x56 corresponding to the "V" character in the C1H address. Only then can it be displayed normally. The display of other characters can be deduced by analogy and will not be repeated here.
III System Software Design
The software program mainly completes the functions of data reading, conversion and liquid crystal display of the calendar clock.
3.1 Design of DS1302 Clock Subprogram
sbit RST=P1^0; // DS1302 reset port is defined in P1.0 pin
sbit SCLK=P1^1; // The DS1302 clock output port is defined on the P1.1 pin
sbit DATA=P1^2; // The DS1302 data output port is defined on the P1.2 pin
(1) The program that the single-chip microcomputer writes a byte of data to DS1302
void wright1302 (unsigned char date)
{Unsigneed char i;
SCLK = 0; //Be prepared for the rising edge to write data
Delaynus (2);
for (i=0; i<8; i++) // Write eight-bit data continuously
{DATA=date&0x01; //Write the bit0 data of date into DS1302
Delaynus (2);
SCLK = 1; // Write data on rising edge
Delaynus (2);
SCLK = 0; //
date>>=1; // move one place to the right}}
(2) The program for the single-chip microcomputer to read a byte of data from DS1302
unsigned char readd1302 (void)
{Unsigned char i, date;
Delaynus (2);
for (i=0; i<8; i++) // Continuously read eight-bit data
{Date>>= 1; // shift one bit to the right
if (DATA==1) // If the data read out is 1
date| = 0x80; // Take out 1 and write it in the highest bit of date
SCLK = 1; // Set SCLK to a high level, read out for the falling edge
Delaynus (2);
SCLK = 0; // Pull down SCLK to form the falling edge of the pulse
Delaynus (2);}
return date; //Return the read data}
3.2 LCD 1602 Liquid Crystal Display Subprogram Design
The driver program of LCD1602 LCD screen is relatively complicated to compile, so we must figure out the usage and meaning of each operation instruction of 1602. Mainly include the following:
- Display mode setting
- Display switch control
- Input mode control
- Read data from DDRAM
- Write data to DDRAM
- Clear screen, cursor home setting
- Data address pointer setting
- LCD's current busy work sign
- ...
Part of the code design is as follows:
void Lcd _ initial () //Initialize LCD
{E=0;
Lcd _ writecmd (0x38); // 16*2 display, 5*7 dot matrix
Msdelay (1);
Lcd _ writecmd (0x08); // display off
Msdelay (2);
Lcd _ writecmd (0x01); // display clear screen
Msdelay (2);
Lcd _ writecmd (0x06); //Set the cursor, after reading and writing a character, the cursor increases by 1
Msdelay (1);
Lcd _ writecmd (0x0c ); // display is on, no cursor is displayed
Msdelay (1);}
Figure 5. Hardware power supply diagram of DS1302 calendar clock
In the Medwin V3.0 development environment, use C51 language to compile the system program, compile and debug. And load the HEX hexadecimal file generated by compiling into the MCU chip. Start the simulation and you can see the simulation running effect of the DS1302 calendar clock design system based on 1602 LCD display. For example, the current time is 11:42:25 on May 28, 2019, and the simulation result is shown in Figure 6.
Figure 6. Simulation results of calendar clock
It can be seen from Figure 6 that the current date and time can be displayed on the LCD screen in real time and accurately.
IV Conclusion
Calendar clocks are everywhere in our lives. This text takes AT89C51 single-chip microcomputer as the main controller, and uses DS1302 real-time clock chip to design the calendar clock system. In addition, the hardware circuit was designed in the Proteus simulation software, and the corresponding C51 program was written in the Medwin V3.0 development environment. The joint use of these two softwares, Proteus and MedWinV3.0, greatly improves the efficiency of single-chip system design, reduces costs, and shortens the development cycle.
FAQ
DS1302 is a tickle-charge timekeeping chip which contains a real-time clock/calendar and 31 bytes of static RAM. DS1302 uses serial communication to interact with microcontrollers. Also, it automatically adjust the date for the month with fewer days.
|
A real-time clock (RTC) is a computer clock (most often in the form of an integrated circuit) that keeps track of the current time. Although the term often refers to the devices in personal computers, servers and embedded systems, RTCs are present in almost any electronic device which needs to keep accurate time.
|
Wiring It Up. 5V is used to power to the RTC chip when you want to query it for the time. If there is no 5V signal, the chip goes to sleep using the coin cell for backup. Connect GND to common power/data ground. Connect the SCL pin to the I2C clock SCL pin on your Arduino. ... Connect the SDA pin to the I2C data SDA pin on your Arduino.
|
A real-time clock (RTC) is a computer clock, usually in the form of an integrated circuit that is solely built for keeping time. Naturally, it counts hours, minutes, seconds, months, days and even years.
|
- How does the DS1302 work?
The DS1302 trickle-charge timekeeping chip contains a real-time clock/calendar and 31 bytes of static RAM. It communicates with a microprocessor via a simple serial interface. The real-time clock/calendar provides seconds, minutes, hours, day, date, month, and year information.
|
- What is the use of RTC module?
RTC is an electronic device in the form of an Integrated Chip (IC) available in various packaging options. The purpose of an RTC or a real-time clock is to provide precise time and date which can be used for various applications.
|