📄 ds1302.lst
字号:
C51 COMPILER V7.00 DS1302 07/18/2007 12:16:44 PAGE 1
C51 COMPILER V7.00, COMPILATION OF MODULE DS1302
OBJECT MODULE PLACED IN DS1302.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE DS1302.c ROM(COMPACT) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include "Stdafx.h"
2 #include "DS1302.h"
3 /*********************************************************************
4 DS1302 C51 Driver
5 *********************************************************************/
6 sbit DS1302_SCLK = P4^1; /*实时时钟时钟线引脚 */
7 sbit DS1302_SDA = P4^0; /*实时时钟数据线引脚 */
8 sbit DS1302_RST = P4^2; /*实时时钟复位线引脚 */
9
10 #define DS1302_SEC_Reg 0x80
11 #define DS1302_MIN_Reg 0x82
12 #define DS1302_HR_Reg 0x84
13 #define DS1302_DATE_Reg 0x86
14 #define DS1302_MONTH_Reg 0x88
15 #define DS1302_DAY_Reg 0x8a
16 #define DS1302_YEAR_Reg 0x8c
17 #define DS1302_CONTROL_Reg 0x8e
18 #define DS1302_CHARGER_Reg 0x90
19 #define DS1302_CLKBURST_Reg 0xbe
20 #define DS1302_RAMBURST_Reg 0xfe
21 #define DS1302_RAM_BASE_ADDR 0xc0 //RAM起始地址
22 #define DS1302_WP_DIS RTC_Write(DS1302_CONTROL_Reg,0x00) //enable write
23 #define DS1302_WP_EN RTC_Write(DS1302_CONTROL_Reg,0x80) //disable write
24 #define DS1302_CHG_EN RTC_Write(DS1302_CHARGER_Reg,0xa9) //使能涓流充电
25 #define DS1302_CHG_DIS RTC_Write(DS1302_CHARGER_Reg,0x0) //禁止涓流充电
26 #define DS1302_WAIT //_nop_(),_nop_()
27 #define DS1302_RAM_BURST_WRITE RTC_Write_Byte(DS1302_RAMBURST_Reg);
28 #define DS1302_RAM_BURST_READ RTC_Write_Byte(DS1302_RAMBURST_Reg+1);
29 //
30
31 #define Set_CLK DS1302_SCLK=1
32 #define Clr_CLK DS1302_SCLK=0
33 #define Set_SDA DS1302_SDA=1
34 #define Clr_SDA DS1302_SDA=0
35 #define Set_RST DS1302_RST=1
36 #define Clr_RST DS1302_RST=0
37
38 /*
39 #define Set_CLK RTC_PORT|=1<<DS1302_SCLK
40 #define Clr_CLK RTC_PORT&=~(1<<DS1302_SCLK)
41 #define Set_SDA RTC_PORT|=1<<DS1302_SDA
42 #define Clr_SDA RTC_PORT&=~(1<<DS1302_SDA)
43 #define Set_RST RTC_PORT|=1<<DS1302_RST
44 #define Clr_RST RTC_PORT&=~(1<<DS1302_RST)
45 */
46
47 /********************************************************************
48 *
49 * 名称: RTC_Write_Byte
50 * 说明:
51 * 功能: 往DS1302写入1Byte数据
52 * 调用:
53 * 输入: Dat 写入的数据
54 * 返回值: 无
55 ***********************************************************************/
C51 COMPILER V7.00 DS1302 07/18/2007 12:16:44 PAGE 2
56 void RTC_Write_Byte(uchar Dat)
57 {
58 1 uchar i;
59 1 for(i=8; i>0; i--)
60 1 {
61 2 (Dat&0x1)?(Set_SDA):(Clr_SDA);
62 2 Set_CLK;
63 2 DS1302_WAIT;
64 2 Clr_CLK;
65 2 DS1302_WAIT;
66 2 Dat>>= 1;
67 2 }
68 1 }
69 /********************************************************************
70 *
71 * 名称: RTC_Write
72 * 说明: 先写地址,后写命令/数据
73 * 功能: 往DS1302写入数据
74 * 调用: RTC_Write_Byte()
75 * 输入: Addr: DS1302地址, Dat: 要写的数据
76 * 返回值: 无
77 ***********************************************************************/
78 void RTC_Write(uchar Addr, uchar Dat)
79 {
80 1 Clr_RST;
81 1 DS1302_WAIT;
82 1 Clr_CLK;
83 1 DS1302_WAIT;
84 1 Set_RST;
85 1 DS1302_WAIT;
86 1 RTC_Write_Byte(Addr); /* 地址,命令 */
87 1 RTC_Write_Byte(Dat); /* 写1Byte数据*/
88 1 Set_CLK;
89 1 DS1302_WAIT;
90 1 Clr_RST;
91 1 }
92 /********************************************************************
93 *
94 * 名称: uchar RTC_Read_Byte
95 * 说明:
96 * 功能: 从DS1302读取1Byte数据
97 * 调用:
98 * 输入:
99 * 返回值: dat
100 ***********************************************************************/
101 uchar RTC_Read_Byte(void)
102 {
103 1 uchar i,dat=0;
104 1 Clr_SDA; //set sda to input mode
105 1 for(i=8; i>0; i--)
106 1 {
107 2 dat>>=1;
108 2 if (DS1302_SDA) dat|= 0x80; //load into MSB
109 2 Set_CLK;
110 2 DS1302_WAIT;
111 2 Clr_CLK;
112 2 DS1302_WAIT;
113 2 }
114 1 return(dat);
115 1 }
116 /********************************************************************
117 *
C51 COMPILER V7.00 DS1302 07/18/2007 12:16:44 PAGE 3
118 * 名称: RTC_Read
119 * 说明: 先写地址,后读命令/数据
120 * 功能: 读取DS1302某地址的数据
121 * 调用: RTC_Write_Byte() , RTC_Read_Byte()
122 * 输入: ucAddr: DS1302地址
123 * 返回值: ucDa :读取的数据
124 ***********************************************************************/
125 uchar RTC_Read(uchar Addr)
126 {
127 1 uchar Dat;
128 1 Addr|=1; //写命令加1作为读命令
129 1 Clr_RST;
130 1 DS1302_WAIT;
131 1 Clr_CLK;
132 1 DS1302_WAIT;
133 1 Set_RST;
134 1 DS1302_WAIT;
135 1 RTC_Write_Byte(Addr); /* 地址,命令 */
136 1 DS1302_WAIT;
137 1 Dat = RTC_Read_Byte(); /* 读1Byte数据 */
138 1 Set_CLK;
139 1 DS1302_WAIT;
140 1 Clr_RST;
141 1 return(Dat);
142 1 }
143
144
145 void RTC_Init(void)
146 {
147 1 Clr_CLK;
148 1 Clr_RST;
149 1 DS1302_WP_DIS;
150 1 DS1302_CHG_EN; //允许涓流充电
151 1 RTC_Write(DS1302_SEC_Reg,RTC_Read(DS1302_SEC_Reg)&0x7f); //24小时制
152 1 DS1302_WP_EN;
153 1 }
154
155 /********************************************************************
156 *
157 * 名称: RTC_Write_Burst
158 * 说明: 先写地址,后写数据(时钟多字节方式)
159 * 功能: 往DS1302写入时钟数据(多字节方式)
160 * 调用: RTC_Write_Byte()
161 * 输入: pDat: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年 控制
162 * 8Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B 1B
163 * 返回值: 无
164 ***********************************************************************/
165 #if 0
void RTC_Write_Burst(uchar *pDat)
{
uchar i;
DS1302_WP_DIS;/* 控制命令,WP=0,写操作?*/
Clr_RST;
DS1302_WAIT;
Clr_CLK;
DS1302_WAIT;
Set_RST;
DS1302_WAIT;
RTC_Write_Byte(DS1302_CLKBURST_Reg); /* 0xbe:时钟多字节写命令 */
for (i=8;i>0;i--) /*8Byte = 7Byte 时钟数据 + 1Byte 控制*/
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -