时钟芯片DS1302的驱动程序
扫描二维码
随时随地手机看文章
1 ds1302的程序,不是很简洁,占的程序空间有点长
2
3 程序:
4
5 //////////////////////////////////
6 //根据自己的接线改管脚定义
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 //函数声明
20 oid DS1302_init();//ds1302初始化
21 void DS1302_write(unsigned char DS1302_temp); //写入一个字节
22 uchar DS1302_read(void); //读一字节
23 void DS1302_write_time(int command,int dat);//写入second or minute or hour...... or 写入一个命令
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() //长延时
28 void short_ds1302_delay();//短延时
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) //写入一个字节
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) //读一字节
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 /////////写操作设置,当a=0x80时禁止写操作,a=0x00时允许////////////////
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 /////////读操作设置,当a=0x80时禁止读操作,a=0x00时允许////////////////
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; //充电设置
142 short_ds1302_delay();
143 DS1302_cs=1;
144 DS1302_write(0x90);
145 DS1302_data=1;
146 // DS1302_write(0xa6); //允许充电,一个二极管 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 //读取时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 写入一个命令
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 }