📄 ds1302.lst
字号:
C51 COMPILER V7.50 DS1302 04/09/2007 18:39:43 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE DS1302
OBJECT MODULE PLACED IN DS1302.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE DS1302.C BROWSE DEBUG OBJECTEXTEND
line level source
1 /**************************************************************************
2
3
4
5 File Name: DS1302.h
6 Author: liao xu ming
7 Created: 2007/3/23
8 Modified: 2007/4/8
9 Revision: 1.0
10
11 ***************************************************************************/
12
13 #include"DS1302.h"
14 #include"clock.h"
15
16 sbit DS1302_CLK = P3^1; //实时时钟时钟线引脚
17 sbit DS1302_IO = P3^2; //实时时钟数据线引脚
18 sbit DS1302_RST = P3^3; //实时时钟复位线引脚
19 sbit ACC0 = ACC^0;
20 sbit ACC7 = ACC^7;
21
22
23
24 #define AM(X) X
25 #define PM(X) (X+12) // 转成24小时制
26 #define DS1302_SECOND 0x80
27 #define DS1302_MINUTE 0x82
28 #define DS1302_HOUR 0x84
29 #define DS1302_WEEK 0x8A
30 #define DS1302_DAY 0x86
31 #define DS1302_MONTH 0x88
32 #define DS1302_YEAR 0x8C
33 #define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏
34
35 void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
36 {
37 1 unsigned char i;
38 1 ACC = d;
39 1 for(i=8; i>0; i--)
40 1 {
41 2 DS1302_IO = ACC0; //相当于汇编中的 RRC
42 2 DS1302_CLK = 1;
43 2 DS1302_CLK = 0;
44 2 ACC = ACC >> 1;
45 2 }
46 1 }
47
48 unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
49 {
50 1 unsigned char i;
51 1 for(i=8; i>0; i--)
52 1 {
53 2 ACC = ACC >>1; //相当于汇编中的 RRC
54 2 ACC7 = DS1302_IO;
55 2 DS1302_CLK = 1;
C51 COMPILER V7.50 DS1302 04/09/2007 18:39:43 PAGE 2
56 2 DS1302_CLK = 0;
57 2 }
58 1 return(ACC);
59 1 }
60
61 void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
62 {
63 1 DS1302_RST = 0;
64 1 DS1302_CLK = 0;
65 1 DS1302_RST = 1;
66 1 DS1302InputByte(ucAddr); // 地址,命令
67 1 DS1302InputByte(ucDa); // 写1Byte数据
68 1 DS1302_CLK = 1;
69 1 DS1302_RST = 0;
70 1 }
71
72 unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
73 {
74 1 unsigned char ucData;
75 1 DS1302_RST = 0;
76 1 DS1302_CLK = 0;
77 1 DS1302_RST = 1;
78 1 DS1302InputByte(ucAddr|0x01); // 地址,命令
79 1 ucData = DS1302OutputByte(); // 读1Byte数据
80 1 DS1302_CLK = 1;
81 1 DS1302_RST = 0;
82 1 return(ucData);
83 1 }
84
85 void DS1302_SetProtect(bit flag) //是否写保护
86 {
87 1 if(flag)
88 1 Write1302(0x8E,0x10); //Bit 7 of the control register is the write-protect bit.
89 1 else //Before any write operation to the clock or RAM, bit 7 must be 0.
90 1 Write1302(0x8E,0x00); //When high,the write protect bit prevents a write operation to any other regist
-er.
91 1 }
92
93 void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数
94 {
95 1 DS1302_SetProtect(0);
96 1 Write1302(Address, ((Value/10)<<4 | (Value%10)));
97 1 }
98
99
100 void DateToStr()
101 { BurstRead1302(ClockRtc);
102 1 DateString[0]=(((ClockRtc[6]&0X70)>>4)*10+(ClockRtc[6]&0X0F))/10+'0';
103 1 DateString[1]=(((ClockRtc[6]&0X70)>>4)*10+(ClockRtc[6]&0X0F))%10+'0';
104 1 DateString[2]='/'; //可否省掉
105 1
106 1 DateString[3]=(((ClockRtc[4]&0X70)>>4)*10+(ClockRtc[4]&0X0F))/10+'0';
107 1 DateString[4]=(((ClockRtc[4]&0X70)>>4)*10+(ClockRtc[4]&0X0F))%10+'0';
108 1 DateString[5]='/';
109 1
110 1 DateString[6]=(((ClockRtc[3]&0X70)>>4)*10+(ClockRtc[3]&0X0F))/10+'0';
111 1 DateString[7]=(((ClockRtc[3]&0X70)>>4)*10+(ClockRtc[3]&0X0F))%10+'0';
112 1 DateString[8]='\0';;
113 1
114 1 TimeString[0]=(((ClockRtc[2]&0X70)>>4)*10+(ClockRtc[2]&0X0F))/10+'0';
115 1 TimeString[1]=(((ClockRtc[2]&0X70)>>4)*10+(ClockRtc[2]&0X0F))%10+'0';
116 1 TimeString[2]=':';
C51 COMPILER V7.50 DS1302 04/09/2007 18:39:43 PAGE 3
117 1
118 1 TimeString[3]=(((ClockRtc[1]&0X70)>>4)*10+(ClockRtc[1]&0X0F))/10+'0';
119 1 TimeString[4]=(((ClockRtc[1]&0X70)>>4)*10+(ClockRtc[1]&0X0F))%10+'0';
120 1 TimeString[5]=':';
121 1
122 1 TimeString[6]=(((ClockRtc[0]&0X70)>>4)*10+(ClockRtc[0]&0X0F))/10+'0';
123 1 TimeString[7]=(((ClockRtc[0]&0X70)>>4)*10+(ClockRtc[0]&0X0F))%10+'0';
124 1 TimeString[8]='\0';
125 1 }
126
127
128 void Initial_DS1302(void)
129 {
130 1 unsigned char Second=Read1302(DS1302_SECOND);
131 1 if(Second&0x80) //Bit 7 of the seconds register is defined as the clock halt flag.
132 1 DS1302_SetTime(DS1302_SECOND,0);//When this bit is written to logic 0, the clock will start.
133 1 } //When this bit is set to logic 1, the clock
134 //oscillator is stopped and
135 //the DS1302 is placed into a low-power standby
136 //mode with a current drain of less
137 //than 100 nanoamps.
138
139 /*******************************************************************************/
140 void BurstWrite1302(unsigned char ClockRtc[]) //往DS1302写入时钟数据(多字节方式)
141 {
142 1 unsigned char i;
143 1 unsigned char * pWClock;
144 1 pWClock=ClockRtc;
145 1 DS1302_SetProtect(0);
146 1 // Write1302(0x8e,0x00); // 控制命令,WP=0,写操作 Before any write operation to the clock or RA
-M, bit 7 must be 0.
147 1 DS1302_RST = 0; //
148 1 DS1302_CLK = 0;
149 1 DS1302_RST = 1;
150 1 DS1302InputByte(0xbe); // 0xbe:时钟多字节写命令
151 1 for (i = 8; i>0; i--) //8Byte = 7Byte 时钟数据 + 1Byte 控制
152 1 {
153 2 DS1302InputByte(*pWClock); // 写1Byte数据
154 2 pWClock++;
155 2 }
156 1 DS1302_CLK = 1;
157 1 DS1302_RST = 0;
158 1 }/***/
159
160 void BurstRead1302(unsigned char ClockRtc[]) //读取DS1302时钟数据(时钟多字节方式)
161 {
162 1 unsigned char i; //
163 1 unsigned char *pWClock;
164 1 pWClock=ClockRtc;
165 1 DS1302_RST = 0;
166 1 DS1302_CLK = 0;
167 1 DS1302_RST = 1;
168 1 DS1302InputByte(0xbf); // 0xbf:时钟多字节读命令
169 1 for (i=8; i>0; i--)
170 1 {
171 2 *pWClock = DS1302OutputByte(); // 读1Byte数据
172 2 pWClock++;
173 2 }
174 1 DS1302_CLK = 1;
175 1 DS1302_RST = 0;
176 1 }
177
C51 COMPILER V7.50 DS1302 04/09/2007 18:39:43 PAGE 4
178
179 void Delay1ms(unsigned int count)
180 {
181 1 unsigned int i,j;
182 1 for(i=0;i<count;i++)
183 1 for(j=0;j<120;j++);
184 1 }
185
186 /*
187 void DS1302_TimeStop(bit flag) // 是否将时钟停止
188 {
189 unsigned char Data;
190 Data=Read1302(DS1302_SECOND);
191 DS1302_SetProtect(0);
192 if(flag)
193 Write1302(DS1302_SECOND, Data|0x80);//Bit 7 of the seconds register is defined as the clock halt flag.
194 else //When this bit is set to logic 1, the clock
195 Write1302(DS1302_SECOND, Data&0x7F);// oscillator is stopped and the DS1302 is placed
196 // into a low-power standby mode with a current drain of less
197 //than 100 nanoamps
198 //When this bit is written to logic 0, the clock will start.
199 }
200 ********************************************************************************/
201 //#endif
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 493 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -