[C51 self-study notes] Real-time clock chip DS1302

Publisher:RadiantSmileLatest update time:2022-04-22 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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();

}

 

}

Keywords:C51 Reference address:[C51 self-study notes] Real-time clock chip DS1302

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

Latest Microcontroller Articles
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号