蓝桥杯ds1302

#define DectoBCD(time) time/10*16+time%10
#define BcdtoDec(time) time/16*10+time%16

sbit SCK=P1^7;		
sbit IO=P2^3;		
sbit CE = P1^3;   // DS1302复位		

void set_ds1302_time(unsigned char sec,unsigned char min,unsigned char hour)
{
   Write_Ds1302_Byte(0x8e,0x00);  //关闭写保护
   Write_Ds1302_Byte(0x80,DectoBCD(sec));  //读0x81
   Write_Ds1302_Byte(0x82,DectoBCD(min));  //读0x83
   Write_Ds1302_Byte(0x84,DectoBCD(hour)); //读0x85
   Write_Ds1302_Byte(0x8e,0x80);  //开启写保护
}

void read_ds1302(void)
{
   if(TH2<0xf0)
  {
       hour=Read_Ds1302_Byte ( 0x85 );
       min=Read_Ds1302_Byte ( 0x83 );
       sec=Read_Ds1302_Byte ( 0x81 );
       sec=BcdtoDec(sec);
       min=BcdtoDec(min);
       hour=BcdtoDec(hour);
  }
}

void ds1302_stop(void);
{ 
   unsigned char tmep;
   temp = Read_Ds1302_Byte ( 0x81);
   Write_Ds1302_Byte(0x8e,0x00);  //关闭写保护
   Write_Ds1302_Byte(0x81,temp|0x80);  //关闭写保护
   Write_Ds1302_Byte(0x8e,0x80);  //关闭写保护
}

void ds1302_start(void)
{
   unsigned char tmep;
   temp = Read_Ds1302_Byte ( 0x81);
   Write_Ds1302_Byte(0x8e,0x00);  //关闭写保护
   Write_Ds1302_Byte(0x81,temp& 0x7f);  //关闭写保护
   Write_Ds1302_Byte(0x8e,0x80);  //关闭写保护
}
 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值