時(shí)鐘芯片DS1302的驅(qū)動(dòng)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
1 ds1302的程序,不是很簡(jiǎn)潔,占的程序空間有點(diǎn)長(zhǎng)
2
3 程序:
4
5 //////////////////////////////////
6 //根據(jù)自己的接線改管腳定義
7 sbit DS1302_cs=P1^2;
8 sbit DS1302_data=P1^1;
9 sbit DS1302_clk=P1^0;
10 sbit ACC0=ACC^0;
11 sbit ACC7=ACC^7;
12 //////////////////////////////////
13 #define uchar unsigned char
14 #define uint unsigned int
15
16 uchar year,month,date,hour,minute,second,week;//定義變量
17
18 //////////////////////////////////
19 //函數(shù)聲明
20 oid DS1302_init();//ds1302初始化
21 void DS1302_write(unsigned char DS1302_temp); //寫入一個(gè)字節(jié)
22 uchar DS1302_read(void); //讀一字節(jié)
23 void DS1302_write_time(int command,int dat);//寫入second or minute or hour...... or 寫入一個(gè)命令
24 uchar DS1302_read_time(int command);//讀取second or minute or hour......
25 void DS1302_readdate();
26 void DS1302_writedate();
27 void long_ds1302_delay() //長(zhǎng)延時(shí)
28 void short_ds1302_delay();//短延時(shí)
29 //////////////////////////////////
30
31
32 void short_ds1302_delay()
33 {
34 uchar i;
35 for(i=0;i<5;i++);
36 }
37
38 void long_ds1302_delay()
39 {
40 uchar i;
41 for(i=0;i<25;i++);
42 }
43
44 void DS1302_write(unsigned char DS1302_temp) //寫入一個(gè)字節(jié)
45 {
46 uchar i,temp;
47 long_ds1302_delay();
48 temp=DS1302_temp;
49 for(i=0;i<8;i++)
50 {
51 DS1302_data=temp&0x01;
52 short_ds1302_delay();
53 DS1302_clk=1;
54 short_ds1302_delay();
55 DS1302_clk=0;
56 temp=temp>>1;
57 }
58 }
59
60 unsigned char DS1302_read(void) //讀一字節(jié)
61 {
62 uchar i,temp;
63 long_ds1302_delay();
64 temp=0;
65 for(i=0;i<8;i++)
66 {
67 temp=temp>>1;
68 if(DS1302_data==1)
69 temp=temp+0x80;
70 DS1302_clk=1;
71 short_ds1302_delay();
72 DS1302_clk=0;
73 short_ds1302_delay();
74 }
75 return temp;
76 }
77 /*
78 /////////寫操作設(shè)置,當(dāng)a=0x80時(shí)禁止寫操作,a=0x00時(shí)允許////////////////
79
80 void DS1302_write_able(unsigned char a)
81 {
82 DS1302_cs=0;
83
84 DS1302_clk=0;
85 short_ds1302_delay();
86 DS1302_cs=1;
87 DS1302_write(0x8e);
88 DS1302_data=1;
89 DS1302_write(a);
90 DS1302_cs=0;
91 short_ds1302_delay();
92 DS1302_clk=1;
93 }
94
95 /////////讀操作設(shè)置,當(dāng)a=0x80時(shí)禁止讀操作,a=0x00時(shí)允許////////////////
96
97 void DS1302_read_able(unsigned char a)
98 {
99 DS1302_cs=0;
100
101 DS1302_clk=0;
102 short_ds1302_delay();
103 DS1302_cs=1;
104 DS1302_write(0x8f);
105 DS1302_data=1;
106 DS1302_write(a);
107 DS1302_cs=0;
108 short_ds1302_delay();
109 DS1302_clk=1;
110 }
111 */
112 ////////////////////ds1302初始化////////////////////////
113 void DS1302_init()
114 {
115 uchar i;
116 DS1302_write_time(0x8e,0x00); //允許寫操作
117
118 DS1302_cs=0;
119
120 DS1302_clk=0;
121 short_ds1302_delay();
122 DS1302_cs=1;
123 DS1302_write(0x81);
124 DS1302_data=1;
125 i=DS1302_read();
126 DS1302_cs=0;
127 short_ds1302_delay();
128 DS1302_clk=1;
129 second=((i>>4)&0x07)*10+(i&0x0f);
130
131 DS1302_clk=0;
132 short_ds1302_delay();
133 DS1302_cs=1;
134 DS1302_write(0x80);
135 DS1302_data=1;
136 DS1302_write(0x7f&i);
137 DS1302_cs=0;
138 short_ds1302_delay();
139 DS1302_clk=1;
140
141 DS1302_clk=0; //充電設(shè)置
142 short_ds1302_delay();
143 DS1302_cs=1;
144 DS1302_write(0x90);
145 DS1302_data=1;
146 // DS1302_write(0xa6); //允許充電,一個(gè)二極管 4k電阻
147 DS1302_write(0x00); //禁止充電
148 DS1302_cs=0;
149 short_ds1302_delay();
150 DS1302_clk=1;
151
152 DS1302_write_time(0x8e,0x80); //禁止寫操作
153 }
154
155 unsigned char DS1302_read_time(int command)//讀取second or minute or hour......
156 {
157 uchar i;
158 DS1302_clk=0;
159 short_ds1302_delay();
160 DS1302_cs=1;
161 DS1302_write(command);
162 DS1302_data=1;
163 i=DS1302_read();
164 DS1302_cs=0;
165 short_ds1302_delay();
166 DS1302_clk=1;
167 return i;
168 }
169 void DS1302_readdate()
170 {
171 uchar i;
172 DS1302_cs=0;
173
174 //讀取second
175 i=DS1302_read_time(0x81);
176 second=((i>>4)&0x07)*10+(i&0x0f);
177
178 //讀取分minute
179 i=DS1302_read_time(0x83);
180 minute=((i>>4)&0x07)*10+(i&0x0f);
181
182 //讀取時(shí)hour
183 i=DS1302_read_time(0x85);
184 hour=((i>>4)&0x03)*10+(i&0x0f);
185
186 //讀取日date
187 i=DS1302_read_time(0x87);
188 date=((i>>4)&0x03)*10+(i&0x0f);
189
190 //讀取月份month
191 i=DS1302_read_time(0x89);
192 month=((i>>4)&0x01)*10+(i&0x0f);
193
194 //讀取周week
195 i=DS1302_read_time(0x8b);
196 week=i&0x07;
197
198 //讀取年份year
199 i=DS1302_read_time(0x8d);
200 year=((i>>4)&0x0f)*10+(i&0x0f);
201 }
202
203
204 void DS1302_write_time(int command,int dat)//寫入second or minute or hour...... or 寫入一個(gè)命令
205 {
206 DS1302_clk=0;
207 short_ds1302_delay();
208 DS1302_cs=1;
209 DS1302_write(command);
210 DS1302_data=1;
211 DS1302_write(dat);
212 DS1302_cs=0;
213 short_ds1302_delay();
214 DS1302_clk=1;
215 }
216
217 void DS1302_writedate()
218 {
219
220 DS1302_write_time(0x8e,0x00); //允許寫操作
221 DS1302_cs=0;
222 DS1302_write_time(0x80,0x7f&(((second/10)<<4)+(second%10))); //寫second
223 DS1302_write_time(0x82,0x7f&(((minute/10)<<4)+(minute%10))); //minute
224 DS1302_write_time(0x84,0x3f&(((hour/10)<<4)+(hour%10))); //hour
225 DS1302_write_time(0x86,0x3f&(((date/10)<<4)+(date%10))); //date
226 DS1302_write_time(0x88,0x1f&(((month/10)<<4)+(month%10))); //month
227 DS1302_write_time(0x8a,0x07&week); //week
228 DS1302_write_time(0x8c,((year/10)<<4)+(year%10)); //year
229 DS1302_write_time(0x8e,0x80); //禁止寫操作
230 }