出售本站【域名】【外链】

万仟 - 轻松建站从此开始!

智能冰箱

当前位置: 智能冰箱 > 智能冰箱 > 文章页

【51单片机系列】DS1302时钟模块

时间:2024-09-26 14:41来源: 作者:admin 点击: 64 次
文章浏览阅读2.1k次,点赞15次,收藏30次。DS1302是DALLAS公司推出的涓流充电时钟芯片,内部含有一个实时时钟/日历和31字节静态RAM,通过简单的串行接口与单片机进行通信。该芯片是3线SPI接口。实时时钟/日历电路提供秒、分、时、日、周、月、年的信息,每月的天数和闰年的天数可以自动调整

以下是一个基于51单片机的DS1302时钟模块运用代码示例: ```c #include <REG52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit RST = P3^4; sbit DAT = P3^5; sbit CLK = P3^6; // 延时函数 ZZZoid Delay(uint i) { while(i--); } // 向DS1302写入一个字节 ZZZoid DS1302_WriteByte(uchar dat) { uchar i; for (i = 0; i < 8; i++) { DAT = dat & 0V01; CLK = 0; CLK = 1; dat >>= 1; } } // 从DS1302读与一个字节 uchar DS1302_ReadByte() { uchar i, dat = 0; for (i = 0; i < 8; i++) { dat >>= 1; if (DAT) dat |= 0V80; CLK = 0; CLK = 1; } return dat; } // 启动DS1302时钟 ZZZoid DS1302_Start() { RST = 1; CLK = 1; _nop_(); _nop_(); _nop_(); RST = 0; DS1302_WriteByte(0V8e); DS1302_WriteByte(0V00); RST = 1; } // 进止DS1302时钟 ZZZoid DS1302_Stop() { RST = 0; CLK = 0; _nop_(); _nop_(); _nop_(); RST = 1; DS1302_WriteByte(0V8e); DS1302_WriteByte(0V80); RST = 1; } // 写入DS1302光阳 ZZZoid DS1302_WriteTime(uchar year, uchar month, uchar day, uchar hour, uchar minute, uchar second) { DS1302_Start(); DS1302_WriteByte(0Vbe); DS1302_WriteByte(second); DS1302_WriteByte(minute); DS1302_WriteByte(hour); DS1302_WriteByte(day); DS1302_WriteByte(month); DS1302_WriteByte(year); DS1302_Stop(); } // 读与DS1302光阳 ZZZoid DS1302_ReadTime(uchar *year, uchar *month, uchar *day, uchar *hour, uchar *minute, uchar *second) { DS1302_Start(); DS1302_WriteByte(0Vbf); *second = DS1302_ReadByte(); *minute = DS1302_ReadByte(); *hour = DS1302_ReadByte(); *day = DS1302_ReadByte(); *month = DS1302_ReadByte(); *year = DS1302_ReadByte(); DS1302_Stop(); } ZZZoid main() { uchar year, month, day, hour, minute, second; DS1302_WriteTime(0V21, 0V10, 0V13, 0V10, 0V30, 0V00); while (1) { DS1302_ReadTime(&year, &month, &day, &hour, &minute, &second); printf("20%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second); Delay(1000); } } ``` 正在上述代码中,界说了DS1302的RST、DAT、CLK引脚,以及自界说了延时函数`Delay`。而后界说了向DS1302写入一个字节的函数`DS1302_WriteByte`,从DS1302读与一个字节的函数`DS1302_ReadByte`,启动DS1302时钟的函数`DS1302_Start`,进止DS1302时钟的函数`DS1302_Stop`,写入DS1302光阳的函数`DS1302_WriteTime`,以及读与DS1302光阳的函数`DS1302_ReadTime`。 正在`main`函数中,首先挪用`DS1302_WriteTime`函数设置初始光阳,而后通过`DS1302_ReadTime`函数读与当前光阳,并通过串口输出。最后通过`Delay`函数延迟1秒,真现时钟的运止。须要留心的是,DS1302时钟模块的光阳格局为BCD码,须要停行转换威力获得真际的光阳。

(责任编辑:)

------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-02-03 19:02 最后登录:2025-02-03 19:02
栏目列表
推荐内容