(7) DS1302 real-time clock

DS1302 is a low-power real-time clock chip with trickle current charging capability launched by DALLAS Company of the United States. It can time the year, month, day, week, hour, minute, second, and has various functions such as leap year compensation

RTC (Real Time Clock): Real-time clock is an integrated circuit, usually called a clock chip, such as DS1302, DS3231, DS12C887

Please add a picture description

Please add a picture description

Please add a picture description

Please add a picture description

sbit DS1302_SCLK = P3^6; // 时钟
sbit DS1302_IO = P3^4; // 输入输出
sbit DS1302_CE = P3^5; // 使能开关

void DS1302_Init(){
    
    
	DS1302_CE = 0;
	DS1302_SCLK = 0;
}

/**
	* @brief 单字节写入
	* @param 
	* @retval 
	*/
void DS1302_WriteByte(unsigned char command, Data){
    
    
	unsigned char i;
	DS1302_CE = 1; // 开启使能开关
	
	for(i = 0;i < 8;i++){
    
    
		
		DS1302_IO = command & (0x01 << i); // 取出命令字第i位
		DS1302_SCLK = 1; // 写入一位命令字
		DS1302_SCLK = 0; // 恢复
	}
	
	for(i = 0;i < 8;i++){
    
    
		DS1302_IO = Data & (0x01 << i); // 取出数据第i位
		DS1302_SCLK = 1; // 写入一位数据
		DS1302_SCLK = 0; // 恢复
	}
	
	DS1302_CE = 0; // 关闭
}

/**
	* @brief 单字节读取
	* @param 
	* @retval 
	*/
unsigned char DS1302_ReadByte(unsigned char command){
    
    
	unsigned char i, Data = 0x00;
    command |= 0x01; // 保证是读操作,这样可以直接传入define的值
    
	DS1302_CE = 1;
	
	for(i = 0;i < 8;i++){
    
    
		
		DS1302_IO = command & (0x01 << i); // 取出命令字第i位
		DS1302_SCLK = 0; // 
		DS1302_SCLK = 1; // 
		
	}
	
	for(i = 0;i < 8;i++){
    
    
		DS1302_SCLK = 1;
		DS1302_SCLK = 0; // 读取到IO
		
		if(DS1302_IO){
    
    
			Data |= (0x01 << i); // 若IO非0,将Data的i位置1
		} 
	}
	
	DS1302_CE = 0;
	DS1302_IO = 0; // IO清零
	return Data;
}

Please add a picture description

Please add a picture description

Note, remember to insert the yellow jumper caps on the left two to use the LCD

void main(){
    
    
	unsigned char Sec = 0x00;
	LCD_Init();
	DS1302_Init();
	DS1302_WriteByte(0x8e,0x00); // 在写寄存器之前必须先将写保护位给清零
	DS1302_WriteByte(0x80, 0x00); // 写入00秒
	
	while(1){
    
    
		Sec = DS1302_ReadByte(0x81);
		LCD_ShowNum(1, 1, Sec, 2);
		Delay(50);
	}
}

In DS1302, the time is not expressed in binary, but in BCD code:

BCD code (Binary Coded Decimal‎), using 4 binary numbers to represent 1 decimal number

  • Example: 0001 0011 means 13, 1000 0101 means 85, 0001 1010 is illegal, the high 8 bits and low 8 bits represent the tens and ones of the decimal system respectively
  • In hexadecimal: 0x13 means 13, 0x85 means 85, 0x1A is illegal

BCD code to decimal: DEC=BCD / 16 * 10 + BCD % 16; (2-digit BCD)

Decimal to BCD code: BCD = DEC / 10 * 16 + DEC % 10; (2-digit BCD)

//寄存器写入地址/指令定义
#define DS1302_SECOND		0x80
#define DS1302_MINUTE		0x82
#define DS1302_HOUR			0x84
#define DS1302_DATE			0x86
#define DS1302_MONTH		0x88
#define DS1302_DAY			0x8A
#define DS1302_YEAR			0x8C
#define DS1302_WP			0x8E
/**
	* @brief 将time数组里的时间写入时钟
	* @param 
	* @retval 
	*/
void DS1302_SetTime(){
    
    
	DS1302_WriteByte(DS1302_WP, 0x00); // 关闭写保护
	
	DS1302_WriteByte(DS1302_YEAR, time[0]/10*16 + time[0]%10); // 十进制转BCD后写入
	DS1302_WriteByte(DS1302_MONTH, time[1]/10*16 + time[1]%10);
	DS1302_WriteByte(DS1302_DATE, time[2]/10*16 + time[2]%10);
	DS1302_WriteByte(DS1302_HOUR, time[3]/10*16 + time[3]%10);
	DS1302_WriteByte(DS1302_MINUTE, time[4]/10*16 + time[4]%10);
	DS1302_WriteByte(DS1302_SECOND, time[5]/10*16 + time[5]%10);
	DS1302_WriteByte(DS1302_DAY, time[6]/10*16 + time[6]%10);
	
	DS1302_WriteByte(DS1302_WP, 0x80);
}

/**
	* @brief 读出时钟的时间
	* @param 
	* @retval 
	*/
void DS1302_ReadTime(){
    
    
	unsigned char Temp;
	Temp=DS1302_ReadByte(DS1302_YEAR);
	time[0]=Temp/16*10+Temp%16;//BCD码转十进制后读取
	Temp=DS1302_ReadByte(DS1302_MONTH);
	time[1]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_DATE);
	time[2]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_HOUR);
	time[3]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_MINUTE);
	time[4]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_SECOND);
	time[5]=Temp/16*10+Temp%16;
	Temp=DS1302_ReadByte(DS1302_DAY);
	time[6]=Temp/16*10+Temp%16;
}

Guess you like

Origin blog.csdn.net/Falling_Asteroid/article/details/130736964