DS1302时钟模块使用讲解附带完整程序
生活随笔
收集整理的這篇文章主要介紹了
DS1302时钟模块使用讲解附带完整程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AT24C02時鐘模塊使用附帶完整程序
- DS1302引腳說明
- DS1302相關寄存器
- 時序說明
- 代碼講解
- DS1302初始化
- 讀取當前時間
- 參考程序
DS1302引腳說明
| Vcc2 | 主電源 |
| Vcc1 | 后備電源(斷電后保證時鐘正常運行) |
| x1,x2 | 外接32.768KHZ晶振 |
| GND | 接地 |
| RST | 復位引腳(低電平有效) |
| I/O | 數據輸入/輸出引腳 |
| SCLK | 串行時鐘輸入引腳 |
參考電路:
如果是直接買的時鐘模塊的話,會直接引出VCC,GND,CLK,DAT,RST這四個引腳
DS1302相關寄存器
DS1302內部含有8位控制寄存器用于存放DS1302控制命令字,例:如果為0x81就是讀秒寄存器,如果為0x80就是對秒寄存寫入數據,初始化時需要令wp為0,才可以寫入初始時間。
時序說明
1.DS1302是通過串行總線跟單片機通信的,當進行一次讀寫操作是最少得讀寫兩個字節,第一個字節就是控制字節,就是一個命令,告訴DS1302是讀還是寫操作,是對RAM還對CLOK寄存器操作。第二個字節就是要讀寫的數據了。
2.單字節讀寫:只有在SCLK為低電平時才能將RST置為高電平。所以在進行操作之前先將SCLK置低電平,然后將RST置為高電平,接著開始在IO上面放入要傳輸的電平信號,然后跳變SCLK。數據在SCLK上升沿時,DS1302讀寫數據,在SCLK下降沿時,DS1302放在數據到IO上
代碼講解
完整程序已附帶在最后。
DS1302初始化
//執行寫操作的地址 秒 分 時 日 月 周 年 u8 code write_addr[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //2020年10月16日 周5 23時59分55秒; u8 timer[]={0x55,0x59,0x23,0x16,0x10,0x05,0x20}; void ds1302_init() {u8 i;Write_Ds1302_Byte(0x8e,0x00);//0x8e是寫的地址,0x00是寫入的數據for(i=0;i<7;i++){Write_Ds1302_Byte( write_addr[i],timer[i]);}}初始化函數的功能開啟對時鐘寄存器的寫操作,寫入初始時間。
讀取當前時間
//執行讀操作的地址 秒 分 時 日 月 周 年 u8 code read_addr[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; void ds1302_read() {u8 i;for(i=0;i<7;i++){timer[i] = Read_Ds1302_Byte(read_addr[i]);}}通過for循環分別讀取秒,分,時等寄存器的值存放在timer[]中,如果要用數碼管顯示的話十位是除以16,個位是對16取余
參考程序
ds1302.c文件
#include <reg52.h> #include <intrins.h>sbit SCK=P1^7;//CLK sbit SDA=P2^3;//DAT 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); }ds1302.h文件
#ifndef __DS1302_H #define __DS1302_Hvoid Write_Ds1302(unsigned char temp); void Write_Ds1302_Byte( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302_Byte( unsigned char address ); #endif主程序
#include "stc15f2k60s2.h" #include "ds1302.h"#define A P25 #define B P26 #define C P27typedef unsigned char u8; typedef unsigned char u16;void hc138_init(u8 n); void delay(u16 n); void smg_display(u8 n, u8 num); void ds1302_init(); void ds1302_read(); void diplay();u8 code read_addr[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; u8 code write_addr[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //2020年10月16日 周5 23時59分55秒; u8 timer[]={0x55,0x59,0x23,0x16,0x10,0x05,0x20}; u8 smg[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};void main() {ds1302_init();while(1){ds1302_read();diplay();} }void delay(u16 n) {u16 i,j;for(i=n;i>0;i--)for(j=110;j>0;j--); }void smg_display(u8 n, u8 num) { //在第n個數碼管上顯示num }void ds1302_init() {u8 i;Write_Ds1302_Byte(0x8e,0x00);//0x8eê?D′μ?μ??·,0x00ê?D′μ?êy?Yfor(i=0;i<7;i++){Write_Ds1302_Byte( write_addr[i],timer[i]);}}void ds1302_read() {u8 i;for(i=0;i<7;i++){timer[i] = Read_Ds1302_Byte(read_addr[i]);}}void diplay() {smg_display(1, timer[2]/16);delay(10);smg_display(2, timer[2]%16);delay(10);smg_display(3, timer[1]/16);delay(10);smg_display(4, timer[1]%16);delay(10);smg_display(5, timer[6]/16);delay(10);smg_display(6, timer[6]%16);delay(10);smg_display(7, timer[0]/16);delay(10);smg_display(8, timer[0]%16);delay(10); }總結
以上是生活随笔為你收集整理的DS1302时钟模块使用讲解附带完整程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序 mpvue 生命周期一览
- 下一篇: modscan32为主设备