Introduction:
The chip has a programmable calendar clock and 31 bytes of static RAM. The calendar clock can automatically compensate for leap years, has accurate timing, simple interface, easy use, wide operating voltage range, and low power consumption. The chip itself also has the function of trickle charging the backup battery, which can effectively extend the service life of the backup battery.
Connection diagram: an interface circuit between 8051 microcontroller and DS1302
The 3-wire serial communication method is used between the microcontroller and DS1302
RST is the communication permission signal, RST=1 allows communication, RST=0 prohibits communication.
IO is a bidirectional serial data transmission signal, and SCLK is a bit synchronization pulse signal of the serial data.
8051 acts as the host and realizes data transmission between the two chips by controlling the RST, SCLK and IO signals.
The X1 and X2 terminals of the DS1302 chip are connected to a 32.768KHz quartz crystal oscillator.
Vcc1 and Vcc2 are power pins. When powered by a single power supply, connect to Vcc1. When powered by dual power supplies, connect the main power supply to Vcc2 and the backup battery to Vcc1. (If a rechargeable nickel-cadmium battery is used, the internal trickle charger can be enabled to charge the battery when the main power supply is normal to extend the battery life. The backup battery can also be replaced by a supercapacitor of 1 microfarad or more. It should be noted that the backup battery voltage should be slightly lower than the main power supply operating voltage.)
Each transfer requires a command byte to initialize
7: must be 1; 0 does not allow writing
6: Determine whether to operate on the clock or on the ROM
1-5: Determine the internal register
0: Input and output
The least significant bit is first when transmitting
Timing diagram:
#include #include #define uchar unsigned char #define uint unsigned int //clock sbit sck=P1^0; sbit io=P1^1; sbit rst=P3^6; // Digital tube to prevent misoperation sbit smgen=P2^3; unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x10}; unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78}; uchar time_data[7]={10,6,4,17,11,58,30}; //year, week, month, day, hour, minute, second uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; uchar disp[8]; // single byte write void write_ds1302_byte(uchar dat); //add +data void write_ds1302(uchar add,uchar dat); //Single byte read uchar read_ds1302(uchar add); // set up void set_rtc(void); // Read time void read_rtc(void); // Data processing and time display void time_pros(void); void display(void); void delay_50us(uint t) { uchar j; for(;t>0;t--) for(j=19;j>0;j--); } void write_ds1302_byte(uchar dat) { //rst has been pulled high before the call uchar i; for(i=0;i<8;i++) { //sclk is set to low level sck=0; // low bit is transmitted first io=dat&0x01; dat=dat>>1; sck=1; } } void write_ds1302(uchar add,uchar dat) { // init rst=0; _nop_(); sck=0; _nop_(); // write rst=1; _nop_(); write_ds1302_byte(add); write_ds1302_byte(dat); rst=0; _nop_(); io=1; sck=1; } uchar read_ds1302(uchar add) { uchar i,value; rst=0; _nop_(); sck=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); for(i=0;i<8;i++) { value=value>>1; sck=0; if(io) value=value|0x80;//10000000 sck=1; } rst=0; _nop_(); sck=0; _nop_(); sck=1; io=1; return value; } void set_rtc(void) { //BCD code for synchronization uchar i,j; //Process bcd code for(i=0;i<7;i++) { j=time_data[i]/10; time_data[i]=time_data[i]%10; time_data[i]=time_data[i]+j*16; } write_ds1302(0x8e,0x00); //Remove write protection for(i=0;i<7;i++) { write_ds1302(write_add[i],time_data[i]); } write_ds1302(0x8e,0x80); //Add write protection } void read_rtc(void) { uchar i; for(i=0;i<7;i++) { time_data[i]=read_ds1302(read_add[i]); } } void time_pros(void) { disp[0]=time_data[6]%16; disp[1]=time_data[6]/16; disp[2]=16; disp[3]=time_data[5]%16; disp[4]=time_data[5]/16; disp[5]=16; disp[6]=time_data[4]%16; disp[7]=time_data[4]/16; } void display(void) { uchar i; for(i=0;i<8;i++) { P1=smg_du[disp[i]]; P2=smg_we[i]; delay_50us(20); } } void main(void) { // set_rtc(); //Automatic time synchronization while(1) { smgen=0; read_rtc(); smgen=1; time_pros(); display(); } }
Previous article:[C51 Getting Started Notes] Single Bus Protocol + DS18B20
Next article:[C51 self-study notes] D/A converter + DAC0832 chip + A/D converter + ADC0804 chip
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- C2000 Piccolo Workshop
- The SD card in my phone suddenly cannot be read, and it cannot be recognized by the card reader when I take it out.
- Many high-end TVs with 4K 120Hz resolution have failed due to a serious MediaTek chip bug
- Remote Voice Control Reference Design
- 【Silicon Labs Development Kit Review】+PG22 Development Kit Hardware Resources
- [RVB2601 Creative Application Development] 1: Unboxing and Environment Setup
- AD9106 Related Questions
- Fudan Micro FM33LC0 series development board information
- BlueNRG-2 OTA upgrade causes bricking issue
- Chapter 4: Use of GD32L23x Timer