?? ds1302.lst
字號:
145 3 default:
146 3 printf("Please Input the Right Option");
147 3 }
148 2 }
149 1 exit: printf("Exit the program");
150 1 }
151
152
153
154
155
156 void ResetDS1302() //復(fù)位子函數(shù)
157 {
158 1 SCLK = 0;
159 1 RSTB = 0; //復(fù)位
160 1 RSTB = 1;
161 1 }
162
163 uchar ReadByteDS1302() //字節(jié)讀取子函數(shù)
164 {
165 1 uchar i;
166 1 uchar RByte;
167 1 uchar TempByte;
168 1
169 1 RByte = 0x00; //初始化
170 1 I_O = 1;
171 1 for(i=0; i<8; ++i) //逐位讀取字節(jié)數(shù)據(jù)
172 1 {
173 2 SCLK = 1; //構(gòu)造時鐘
174 2 SCLK = 0;
175 2 TempByte = (uchar)I_O;
176 2 TempByte = TempByte <<7; //移位
177 2 RByte = RByte >> 1;
178 2 RByte |= TempByte;
179 2 }
C51 COMPILER V8.08 DS1302 08/29/2008 18:01:54 PAGE 4
180 1 return RByte; //返回結(jié)果
181 1 }
182
183 void WriteByteDS1302(uchar W_Byte) //字節(jié)寫入子函數(shù)
184 {
185 1 uchar i;
186 1 for(i = 0; i < 8; ++i) //循環(huán)逐位寫入
187 1 {
188 2 I_O = 0;
189 2 if(W_Byte & 0x01)
190 2 I_O = 1;
191 2 SCLK = 0; //時鐘操作
192 2 SCLK = 1;
193 2 W_Byte = W_Byte >>1; //移位
194 2 }
195 1 }
196
197 void SetYear() //年設(shè)置子函數(shù)
198 {
199 1 uchar year;
200 1 printf("\nPlease Enter the year (0-99): "); //輸入年
201 1 scanf("%bx", &year);
202 1 ResetDS1302(); //復(fù)位DS1302
203 1 WriteByteDS1302(0x06); //年寄存器地址
204 1 WriteByteDS1302(year); //寫入年
205 1 ResetDS1302(); //復(fù)位DS1302
206 1 }
207
208 void SetMonth() //月設(shè)置子函數(shù)
209 {
210 1 uchar month;
211 1 printf("\n Please Enter the month (1-12): "); //輸入月
212 1 scanf("%bx", &month);
213 1 ResetDS1302(); //復(fù)位DS1302
214 1 WriteByteDS1302(0x04); //月寄存器地址
215 1 WriteByteDS1302(month); //寫入月
216 1 ResetDS1302(); //復(fù)位DS1302
217 1 }
218
219 void SetDate() //日設(shè)置子函數(shù)
220 {
221 1 uchar date;
222 1 printf("\n Please Enter the date (1-31): "); //輸入日
223 1 scanf("%bx", &date);
224 1 ResetDS1302(); //復(fù)位DS1302
225 1 WriteByteDS1302(0x03); //日寄存器地址
226 1 WriteByteDS1302(date); //寫入日
227 1 ResetDS1302(); //復(fù)位DS1302
228 1 }
229
230 void SetDay() //星期設(shè)置子函數(shù)
231 {
232 1 uchar day;
233 1 printf("\n Please Enter the day (1-7): "); //輸入星期
234 1 scanf("%bx", &day);
235 1 ResetDS1302(); //復(fù)位DS1302
236 1 WriteByteDS1302(0x05); //星期寄存器地址
237 1 WriteByteDS1302(day); //寫入星期
238 1 ResetDS1302(); //復(fù)位DS1302
239 1 }
240
241 void SetHour() //小時設(shè)置子函數(shù)
C51 COMPILER V8.08 DS1302 08/29/2008 18:01:54 PAGE 5
242 {
243 1 uchar hour;
244 1 printf("\n Please Enter the hour (1-24): "); //輸入小時
245 1 scanf("%bx", &hour);
246 1 hour =hour & 0x3f; //設(shè)置時鐘為24小時方式
247 1 ResetDS1302(); //復(fù)位DS1302
248 1 WriteByteDS1302(0x02); //小時寄存器地址
249 1 WriteByteDS1302(hour); //寫入小時
250 1 ResetDS1302(); //復(fù)位DS1302
251 1 }
252
253 void SetMinute () //分鐘設(shè)置子函數(shù)
254 {
255 1 uchar minute;
256 1 printf("\n Please Enter the minute (0-59): "); //輸入分鐘
257 1 scanf("%bx", &minute);
258 1 ResetDS1302(); //復(fù)位DS1302
259 1 WriteByteDS1302(0x01); //分鐘寄存器地址
260 1 WriteByteDS1302(minute); //寫入分鐘
261 1 ResetDS1302(); //復(fù)位DS1302
262 1 }
263
264 void SetSecond () //秒設(shè)置子函數(shù)
265 {
266 1 uchar second;
267 1 printf("\n Please Enter the second (0-59): "); //輸入秒
268 1 scanf("%bx", &second);
269 1 ResetDS1302(); //復(fù)位DS1302
270 1 WriteByteDS1302(0x00); //秒寄存器地址
271 1 WriteByteDS1302(second); //寫入秒
272 1 ResetDS1302(); //復(fù)位DS1302
273 1 }
274
275 void DisableWrite () //寫保護(hù)子函數(shù)
276 {
277 1 ResetDS1302(); //復(fù)位DS1302
278 1 WriteByteDS1302(0x8e); //寫保護(hù)控制寄存器
279 1 WriteByteDS1302(0x80); //禁止寫入
280 1 ResetDS1302(); //復(fù)位DS1302
281 1 }
282
283 void EnableWrite () //寫允許子函數(shù)
284 {
285 1 ResetDS1302(); //復(fù)位DS1302
286 1 WriteByteDS1302(0x8e); //寫保護(hù)控制寄存器
287 1 WriteByteDS1302(0); //允許寫入
288 1 ResetDS1302(); //復(fù)位DS1302
289 1 }
290
291 void Charge () //充電控制子函數(shù)
292 {
293 1 ResetDS1302(); //復(fù)位DS1302
294 1 WriteByteDS1302(0x90); //涓流充電寄存器
295 1 WriteByteDS1302(0xab); //允許充電
296 1 ResetDS1302(); //復(fù)位DS1302
297 1 }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -