蓝桥杯单片机ds1302

在这里插入图片描述

在这里插入图片描述

1.设置时钟
1)运行写

Write_Ds1302_Byte(0x8E, 0);

2)写时

temp = ((pucRtc[0] / 10) << 4) + pucRtc[0] % 10;
Write_Ds1302_Byte(0x84, temp);

3)写分

temp = ((pucRtc[1] / 10) << 4) + pucRtc[1] % 10;
Write_Ds1302_Byte(0x82, temp);

4)写秒

temp = ((pucRtc[2] / 10) << 4) + pucRtc[2] % 10;
Write_Ds1302_Byte(0x80, temp);

5)禁止写

Write_Ds1302_Byte(0x8E, 0x80);

函数为

void Set_RTC(unsigned char* pucRtc)
{
    unsigned char temp;
    Write_Ds1302_Byte(0x8E, 0); // WP=0:允许写操作
    temp = ((pucRtc[0] / 10) << 4) + pucRtc[0] % 10;
    Write_Ds1302_Byte(0x84, temp); // 设置时
    temp = ((pucRtc[1] / 10) << 4) + pucRtc[1] % 10;
    Write_Ds1302_Byte(0x82, temp); // 设置分
    temp = ((pucRtc[2] / 10) << 4) + pucRtc[2] % 10;
    Write_Ds1302_Byte(0x80, temp); // 设置秒
    Write_Ds1302_Byte(0x8E, 0x80); // WP=1:禁止写操作
}

2.读时钟

void Read_RTC(unsigned char* pucRtc)
{
    unsigned char temp;
    temp = Read_Ds1302_Byte(0x85); // 读取时
    pucRtc[0] = (temp >> 4) * 10 + (temp & 0xf);
    temp = Read_Ds1302_Byte(0x83); // 读取分
    pucRtc[1] = (temp >> 4) * 10 + (temp & 0xf);
    temp = Read_Ds1302_Byte(0x81); // 读取秒
    pucRtc[2] = (temp >> 4) * 10 + (temp & 0xf);
}

3.数据包中给的代码

#include <reg52.h>
#include <intrins.h>

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位												

void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ccsu_cw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值