參考DS1302數(shù)據(jù)手冊(cè),盡管數(shù)據(jù)手冊(cè)是英文,但是很有幫助,再結(jié)合網(wǎng)上眾多的代碼寫(xiě)出了下面這個(gè)例子,除了涓流充電(trickle charger)功能沒(méi)使用外,內(nèi)部的RAM和寄存器功能都使用到了,包括多字節(jié)方式(burst mode)。自認(rèn)為我寫(xiě)的比網(wǎng)上大多數(shù)程序清晰,當(dāng)然已經(jīng)硬件測(cè)試過(guò)了。
1 #include
2
3 /*DS1302部分*/
4
5 //全部為寫(xiě)入地址,讀取地址需要+1
6 #define YEAR 0x8c
7 #define MON 0x88
8 #define DATE 0x86
9 #define DAY 0x8a
10 #define HOUR 0x84
11 #define MIN 0x82
12 #define SEC 0x80
13 #define WP 0x8e
14 #define RAM(n) (0xc0+2*(n))
15 #define CLBT 0xbe
16 #define RAMBT 0xfe
17
18 //3個(gè)通信引腳
19 sbit SCLK=P2^1;
20 sbit IO=P2^0;
21 sbit RST=P2^4; //有的使能端叫CE
22
23 unsigned char readbyte(void);
24 void writebyte(unsigned char dat);
25 void write1302(unsigned char addr,unsigned char dat);
26 unsigned char read1302(unsigned char addr);
27
28
29 /*數(shù)碼管部分,6位共陰接在P0*/
30 sbit we=P2^7;
31 sbit du=P2^6;
32 void show(void);
33 void delayms(unsigned time);
34
35 unsigned char code table[] = {
36 0x3f , 0x06 , 0x5b , 0x4f,
37 0x66 , 0x6d , 0x7d , 0x07,
38 0x7f , 0x6f , 0x77 , 0x7c,
39 0x39 , 0x5e , 0x79 , 0x71,
40 0x00 };
41
42 unsigned char num[6]={0}; //用作數(shù)碼管顯示的數(shù)組
43
44 /*鍵盤(pán)部分,4個(gè)獨(dú)立鍵盤(pán)接在P3.4-P3.7*/
45 void keyscan(void);
46 bit showdate=0;
47 bit showday=0;
48
49 void main(void)
50 {
51 unsigned char temp;
52
53 write1302(WP,0x00); //允許寫(xiě)入,關(guān)閉寫(xiě)保護(hù)
54
55 //clock burst 方式賦初值
56 RST=0;
57 SCLK=0;
58 RST=1;
59 writebyte(CLBT);
60
61 writebyte(0x30);//秒分時(shí)
62 writebyte(0x59);
63 writebyte(0x23);
64
65 writebyte(0x28);//日月
66 writebyte(0x02);
67
68 writebyte(0x02);//星期
69
70 writebyte(0x32);//年
71
72 writebyte(0x00);
73 writebyte(0x00);
74 RST=0;
75
76 // 另一種賦初值方法
77 // write1302(HOUR,0x23);
78 // write1302(MIN,0x59);
79 // write1302(SEC,0x30);
80
81 while(1)
82 {
83 keyscan();
84 if(showdate)
85 {
86 temp=read1302(YEAR+1);
87 num[5]=temp>>4;
88 num[4]=temp&0x0f;
89 temp=read1302(MON+1);
90 num[3]=temp>>4;
91 num[2]=temp&0x0f;
92 temp=showday?read1302(DAY+1):read1302(DATE+1);
93 num[1]=temp>>4;
94 num[0]=temp&0x0f;
95 }
96 else
97 {
98 temp=read1302(HOUR+1);
99 num[5]=temp>>4;
100 num[4]=temp&0x0f;
101 temp=read1302(MIN+1);
102 num[3]=temp>>4;
103 num[2]=temp&0x0f;
104 temp=read1302(SEC+1);
105 num[1]=temp>>4;
106 num[0]=temp&0x0f;
107 }
108 show();
109 }
110
111 }
112
113
114 unsigned char readbyte(void)
115 {
116 unsigned char i,ret;
117 for(i=0;i<8;i++)
118 {
119 ret>>=1;
120 SCLK=1;
121 SCLK=0;
122 ret|=IO?0x80:0x00;
123 }
124 return ret;
125 }
126
127 void writebyte(unsigned char dat)
128 {
129 unsigned char i;
130 for(i=0;i<8;i++)
131 {
132 IO=dat&1;
133 SCLK=0;
134 SCLK=1;
135 dat>>=1;
136 }
137 }
138
139 void write1302(unsigned char addr,unsigned char dat)
140 {
141 RST=0;
142 SCLK=0;
143 RST=1;
144 writebyte(addr);
145 writebyte(dat);
146 RST=0;
147 }
148
149 unsigned char read1302(unsigned char addr)
150 {
151 unsigned char ret;
152 RST=0;
153 SCLK=0;
154 RST=1;
155 writebyte(addr);
156 ret=readbyte();
157 RST=0;
158 return ret;
159 }
160
161 void show(void)
162 {
163 unsigned char i;
164
165 for(i=0;i<6;i++)
166 {
167 P0=0xff;
168 we=1;
169 we=0;
170
171 P0=table[num[i]];
172 du=1;
173 du=0;
174
175 P0=~(0x20>>i);
176 we=1;
177 we=0;
178
179 delayms(1);
180 }
181 }
182
183 void delayms(unsigned time)
184 {
185 unsigned i,j;
186
187 for(i=time;i>0;i--)
188 for(j=110;j>0;j--)
189 ;
190 }
191
192 void keyscan(void)
193 {
194 unsigned char temp;
195 unsigned char reg[9];
196
197 if((P3|0x0f)!=0xff)
198 {
199 delayms(10);
200 if((temp=P3|0x0f)!=0xff)
201 {
202 while((P3|0x0f)!=0xff);
203 switch(temp)
204 {
205 case 0xef: //顯示時(shí)間還是日期
206 showdate=~showdate;
207 break;
208 case 0xdf: //日期最后兩位顯示日還是星期
209 showday=~showday;
210 break;
211 case 0xbf: //將時(shí)鐘寄存器保存到RAM
212 RST=0;
213 SCLK=0;
214 RST=1;
215 writebyte(CLBT+1);
216 for(temp=0;temp<9;temp++)
217 reg[temp]=readbyte();
218 RST=0;
219
220 //從20號(hào)RAM開(kāi)始存入,起始RAM號(hào)隨意,只要不超過(guò)范圍即可
221 for(temp=0;temp<9;temp++)
222 write1302(RAM(20+temp),reg[temp]);
223
224 /* 也可以用 RAM burst 寫(xiě)入
225 RST=0;
226 SCLK=0;
227 RST=1;
228 writebyte(RAMBT);
229 for(temp=0;temp<9;temp++)
230 writebyte(reg[temp]);
231 RST=0;
232 */
233
234 break;
235 case 0x7f: //從RAM恢復(fù)到時(shí)鐘寄存器
236 /* 對(duì)應(yīng)的 RAM burst 讀取
237 RST=0;
238 SCLK=0;
239 RST=1;
240