51 drive DS1302 module display clock

Introduction to DS1302 Chip

insert image description here

  • Detailed description:
    The DS1302 trickle charge timing chip contains a real-time clock/calendar and 31 bytes of static RAM. It
    communicates with the microprocessor through a simple serial interface. This real-time clock/calendar provides year, month, day, hour, minute, and second information. For Automatic adjustment at the end of months with less than 31 days
    , as well as leap year correction. Thanks to an AM/PM indicator, the clock can work in 12-hour or 24
    -hour format.
    The use of synchronous serial communication simplifies the interface of the DS1302 to a microprocessor. Only three lines are required to communicate with the clock/RAM: CE, I/O (data line), and SCLK (serial clock). Data output is input to the clock/RAM 1 byte at a time or multiple pins
    in bursts define the DS1302 trickle The charge timing chip is up to 31 bytes. The DS1302 is designed to operate at very low power and maintain data and clock information at less than 1µW. The DS1302 is the successor to the DS1202. In addition to the basic timing functions of the DS1202, the DS1302 has additional Features such as dual-pin main and backup power supplies, programmable trickle charger VCC1, and additional 7-byte scratchpad.




  • Pin Definition and Application Circuit:
    insert image description here

  • Internal structure block diagram:
    insert image description here
    4. Register definition:
    insert image description here
    insert image description here
    The above figure shows the command word. The command word starts each data transfer. MSB (bit 7) must be logic 1. If it is 0, writing to DS1302 is prohibited. Bit 6 is in logic A 0 is specified as clock/calendar data, and a logic 1 is RAM data. Bits 1 to 5 represent the specified register for input and output. The LSB (bit 0) is a write operation (output) at a logic 0, and a read at a logic 1 Operation (input). The command word starts with the LSB (bit 0) and is always input.

  • Timing:
    insert image description here

Main program explanation

1. Pin definition:

sbit DS1302_SCLK=P3^6;
sbit DS1302_IO=P3^4;
sbit DS1302_CE=P3^5;

2. Write a byte

void DS1302_WriteByte(unsigned char Command,Data)
{
    
    
	unsigned char i;
	DS1302_CE=1;
	for(i=0;i<8;i++)
	{
    
    
		DS1302_IO=Command&(0x01<<i);
		DS1302_SCLK=1;
		DS1302_SCLK=0;
	}
	for(i=0;i<8;i++)
	{
    
    
		DS1302_IO=Data&(0x01<<i);
		DS1302_SCLK=1;
		DS1302_SCLK=0;
	}
	DS1302_CE=0;
}

3. Read a byte:

unsigned char DS1302_ReadByte(unsigned char Command)
{
    
    
	unsigned char i,Data=0x00;
	Command|=0x01;	//将指令转换为读指令
	DS1302_CE=1;
	for(i=0;i<8;i++)
	{
    
    
		DS1302_IO=Command&(0x01<<i);
		DS1302_SCLK=0;
		DS1302_SCLK=1;
	}
	for(i=0;i<8;i++)
	{
    
    
		DS1302_SCLK=1;
		DS1302_SCLK=0;
		if(DS1302_IO){
    
    Data|=(0x01<<i);}
	}
	DS1302_CE=0;
	DS1302_IO=0;	//读取后将IO设置为0,否则读出的数据会出错
	return Data;
}

Fourth, set the time,

void DS1302_SetTime(void)
{
    
    
	DS1302_WriteByte(DS1302_WP,0x00);
	DS1302_WriteByte(DS1302_YEAR,DS1302_Time[0]/10*16+DS1302_Time[0]%10);//十进制转BCD码后写入
	DS1302_WriteByte(DS1302_MONTH,DS1302_Time[1]/10*16+DS1302_Time[1]%10);
	DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
	DS1302_WriteByte(DS1302_HOUR,DS1302_Time[3]/10*16+DS1302_Time[3]%10);
	DS1302_WriteByte(DS1302_MINUTE,DS1302_Time[4]/10*16+DS1302_Time[4]%10);
	DS1302_WriteByte(DS1302_SECOND,DS1302_Time[5]/10*16+DS1302_Time[5]%10);
	DS1302_WriteByte(DS1302_DAY,DS1302_Time[6]/10*16+DS1302_Time[6]%10);
	DS1302_WriteByte(DS1302_WP,0x80);
}

5. Reading time:

void DS1302_ReadTime(void)
{
    
    
	unsigned char Temp;
	Temp=DS1302_ReadByte(DS1302_YEAR);
	DS1302_Time[0]=Temp/16*10+Temp%16;//BCD码转十进制后读取
	Temp=DS1302_ReadByte(DS1302_MONTH);
	DS1302_Time[1]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_DATE);
	DS1302_Time[2]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_HOUR);
	DS1302_Time[3]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_MINUTE);
	DS1302_Time[4]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_SECOND);
	DS1302_Time[5]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_DAY);
	DS1302_Time[6]=Temp/16*10+Temp%16;
}

6. Main program:

void main()
{
    
    
	LCD_Init();
	DS1302_Init();
	LCD_ShowString(1,1,"  -  -  ");//静态字符初始化显示
	LCD_ShowString(2,1,"  :  :  ");
	
	DS1302_SetTime();//设置时间
	
	while(1)
	{
    
    
		DS1302_ReadTime();//读取时间
		LCD_ShowNum(1,1,DS1302_Time[0],2);//显示年
		LCD_ShowNum(1,4,DS1302_Time[1],2);//显示月
		LCD_ShowNum(1,7,DS1302_Time[2],2);//显示日
		LCD_ShowNum(2,1,DS1302_Time[3],2);//显示时
		LCD_ShowNum(2,4,DS1302_Time[4],2);//显示分
		LCD_ShowNum(2,7,DS1302_Time[5],2);//显示秒
	}
}

Experimental phenomenon and precautions

  1. Experimental phenomenon:
    insert picture description here
    insert image description here
  2. Note:
    The specific definition is based on the schematic diagram of your own development board.
    If the 1602 does not display the clock, you can check whether the pin definition of the 1602 is the same as the schematic diagram of your own development board. The
    detailed program and information can be obtained by leaving a message below

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/123803765