串口上升时间标准_DS1302 串口更新时间

这篇博客主要介绍了DS1302实时时钟的串口通信协议,包括数据的读取和写入过程。通过示例代码展示了如何在下降沿时进行数据交换,并详细解释了BCD码的处理方法,用于设置和读取时间数据。
摘要由CSDN通过智能技术生成

沿时。。DS1302读取数据

//_nop_() ;

SCLK = 0; //DS1302在下降沿时,放置数据

//_nop_();

}

_nop_();

//输出数据;dat

for(i=0; i&lt8; i++)

{

Temp = IO;//从最低位开始接收

dat = (dat &gt&gt 1) | (Temp&lt&lt7);

SCLK= 1;

//_nop_();

SCLK= 0;//DS1302在下降沿时,放置数据

//_nop_();

}

RST = 0; // 停止DS1302数据传送

_nop_();//以下为DS1302复位的稳定时间,必须的。

SCLK = 1;

_nop_();

IO = 0;

_nop_();

IO = 1;

_nop_();

return dat;

}

/*------------------------------------------------

向DS1302写入时钟数据

------------------------------------------------ */

void Ds1302_Write_Time()

{

uchar i,tmp;

for (i=0 ;i&lt8 ;i++)

{

tmp = Time_Data[i]/10;

Time_Buf[i] = Time_Data[i]%10;

Time_Buf[i] = Time_Buf[i] + tmp*16;

}

//BCD码处理,,,将十六进制数据转化为十进制

Ds1302_Write_Byte( 0x8e , 0x00 );//关闭写保护,可以可以写入数据

Ds1302_Write_Byte( 0x80 , 0x80 ); //暂停

Ds1302_Write_Byte( 0x8a , Time_Buf[7] ); // 星期

Ds1302_Write_Byte( 0x80 , Time_Buf[6] ); // 秒

Ds1302_Write_Byte( 0x82 , Time_Buf[5] ); // 分

Ds1302_Write_Byte( 0x84 , Time_Buf[4] ); // 时

Ds1302_Write_Byte( 0x8a , Time_Buf[7] ); // 星期

Ds1302_Write_Byte( 0x86 , Time_Buf[3] ); // 日

Ds1302_Write_Byte( 0x88 , Time_Buf[2] ); // 月

Ds1302_Write_Byte( 0x8c , Time_Buf[1] ); // 年

Ds1302_Write_Byte( 0x8e , 0x80 ); //打开写保护,不能写入数据

}

/*------------------------------------------------

读出DS1302时钟数据

------------------------------------------------*/

void Ds1302_Read_Time( )

{

uchar i,tmp;

Time_Buf[1]= Ds1302_Read_Byte(0x8d) ;// 年

Time_Buf[2]= Ds1302_Read_Byte(0x89) ;//月

Time_Buf[3]= Ds1302_Read_Byte(0x87) ;//日

Time_Buf[4]= Ds1302_Read_Byte(0x85) ;//时

Time_Buf[5]= Ds1302_Read_Byte(0x83) ;//分

Time_Buf[6]= (Ds1302_Read_Byte(0x81))&0x7f ;//秒

Time_Buf[7]= Ds1302_Read_Byte(0x8b) ; //周

for (i=0 ;i&lt8 ;i++)

{

tmp = Time_Buf[i]/16;

Time_Data[i] = Time_Buf[i]%16;

Time_Data[i] = Time_Data[i] + tmp*10;

}

//BCD码处理,,,将十进制数据转化为十六进制

}

/*------------------------------------------------

初始化DS1302

------------------------------------------------*/

void Ds1302_Init(void)

{

RST=0;//RST脚置低

SCLK=0;//SCK脚置低

Ds1302_Write_Byte(0x80,0x00);

}

///

__DS1302_H__/

//

#ifndef __DS1302_H__

#define __DS1302_H__

#include&ltreg52.h&gt

#include&ltintrins.h&gt

//---重定义关键词---//

#ifndef uchar

#define uchar unsigned char

#endif

#ifndef uint

#define uint unsi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值