📄 lcd_clock_ds1302.lst
字号:
192
193 u8 ds1302ouputbyte(void) //实时时钟读取一字节(内部函数)
194 {
195 1 u8 i;
196 1 for(i=8; i>0; i--)
197 1 {
198 2 ACC = ACC >>1; //相当于汇编中的 RRC
199 2 ACC7 = ds1302_io;
200 2 ds1302_clk = 1;
201 2 ds1302_clk = 0;
202 2 }
203 1 return(ACC);
204 1 }
205
206 void write1302(u8 ucAddr, u8 ucDa) //ucAddr: DS1302地址, ucdata: 要写的数据
207 {
208 1 ds1302_rst = 0;
209 1 ds1302_clk = 0;
210 1 ds1302_rst = 1;
211 1 ds1302inputbyte(ucAddr); // 地址,命令
212 1 ds1302inputbyte(ucDa); // 写1Byte数据
213 1 ds1302_clk = 1;
214 1 ds1302_rst = 0;
215 1 }
216
217 u8 read1302(u8 ucAddr) //读取DS1302某地址的数据
218 {
219 1 u8 ucdata;
220 1 ds1302_rst = 0;
221 1 ds1302_clk = 0;
222 1 ds1302_rst = 1;
223 1 ds1302inputbyte(ucAddr|0x01); // 地址,命令
224 1 ucdata = ds1302ouputbyte(); // 读1Byte数据
225 1 ds1302_clk = 1;
226 1 ds1302_rst = 0;
227 1 return(ucdata);
228 1 }
229
230 void ds1302_setprotect(bit flag) //是否写保护
231 {
232 1 if(flag)
233 1 write1302(0x8E,0x10);
234 1 else
235 1 write1302(0x8E,0x00);
236 1 }
237
238 void ds1302_settime(u8 address, u8 Value) // 设置时间函数
239 {
240 1 ds1302_setprotect(0);
241 1 write1302(address, ((Value/10)<<4 | (Value%10)));
C51 COMPILER V7.20 LCD_CLOCK_DS1302 03/03/2008 10:13:50 PAGE 5
242 1 }
243
244 void ds1302_gettime(systemtime *time)
245 {
246 1 u8 readvalue;
247 1 readvalue = read1302(ds1302_second);
248 1 time->second = ((readvalue&0x70)>>4)*10+readvalue&0x0f;
249 1 readvalue = read1302(ds1302_minute);
250 1 time->minute = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
251 1 readvalue = read1302(ds1302_hour);
252 1 time->hour = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
253 1 readvalue = read1302(ds1302_day);
254 1 time->day = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
255 1 readvalue = read1302(ds1302_week);
256 1 time->week = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
257 1 readvalue = read1302(ds1302_month);
258 1 time->month = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
259 1 readvalue = read1302(ds1302_year);
260 1 time->year = ((readvalue&0x70)>>4)*10 + (readvalue&0x0F);
261 1 }
262
263 void datatostr(systemtime *time)
264 {
265 1 time->datestring[0] = time->year/10 + '0';
266 1 time->datestring[1] = time->year%10 + '0';
267 1 time->datestring[2] = '-';
268 1 time->datestring[3] = time->month/10 + '0';
269 1 time->datestring[4] = time->month%10 + '0';
270 1 time->datestring[5] = '-';
271 1 time->datestring[6] = time->day/10 + '0';
272 1 time->datestring[7] = time->day%10 + '0';
273 1 time->datestring[8] = '\0';
274 1 }
275
276 void timetostr(systemtime *time)
277 {
278 1 time->timestring[0] = time->hour/10 + '0';
279 1 time->timestring[1] = time->hour%10 + '0';
280 1 time->timestring[2] = ':';
281 1 time->timestring[3] = time->minute/10 + '0';
282 1 time->timestring[4] = time->minute%10 + '0';
283 1 time->timestring[5] = ':';
284 1 time->timestring[6] = time->second/10 + '0';
285 1 time->timestring[7] = time->second%10 + '0';
286 1 time->datestring[8] = '\0';
287 1 }
288
289 void initial_ds1302(void)
290 {
291 1 u8 second=read1302(ds1302_second);
292 1 if(second&0x80)
293 1 ds1302_settime(ds1302_second,0);
294 1 }
295
296 /********************************************************************************
297 void Burstwrite1302(u8 *pWClock) //往DS1302写入时钟数据(多字节方式)
298 {
299 u8 i;
300 write1302(0x8e,0x00); // 控制命令,WP=0,写操作?
301 ds1302_rst = 0;
302 ds1302_clk = 0;
303 ds1302_rst = 1;
C51 COMPILER V7.20 LCD_CLOCK_DS1302 03/03/2008 10:13:50 PAGE 6
304 ds1302inputbyte(0xbe); // 0xbe:时钟多字节写命令
305 for (i = 8; i>0; i--) //8Byte = 7Byte 时钟数据 + 1Byte 控制
306 {
307 ds1302inputbyte(*pWClock); // 写1Byte数据
308 pWClock++;
309 }
310 ds1302_clk = 1;
311 ds1302_rst = 0;
312 }
313
314 void Burstread1302(u8 *pRClock) //读取DS1302时钟数据(时钟多字节方式)
315 {
316 u8 i;
317 ds1302_rst = 0;
318 ds1302_clk = 0;
319 ds1302_rst = 1;
320 ds1302inputbyte(0xbf); // 0xbf:时钟多字节读命令
321 for (i=8; i>0; i--)
322 {
323 *pRClock = ds1302ouputbyte(); // 读1Byte数据
324 pRClock++;
325 }
326 ds1302_clk = 1;
327 ds1302_rst = 0;
328 }
329
330 void DS1302_timeStop(bit flag) // 是否将时钟停止
331 {
332 u8 Data;
333 Data=read1302(ds1302_second);
334 ds1302_setprotect(0);
335 if(flag)
336 write1302(ds1302_second, Data|0x80);
337 else
338 write1302(ds1302_second, Data&0x7F);
339 }
340 ********************************************************************************/
341 #endif
342
343
344
345
346 void delay(u16 count)
347 {
348 1 u16 i,j;
349 1 for(i=0;i<count;i++)
350 1 for(j=0;j<120;j++);
351 1 }
352
353 main()
354 {
355 1 systemtime currenttime;
356 1 lcd_initial();
357 1 initial_ds1302();
358 1
359 1 gotoxy(0,0);
360 1 print("Date: ");
361 1 gotoxy(0,1);
362 1 print("time: ");
363 1 while(1)
364 1 {
365 2 ds1302_gettime(¤ttime);
C51 COMPILER V7.20 LCD_CLOCK_DS1302 03/03/2008 10:13:50 PAGE 7
366 2 datatostr(¤ttime);
367 2 timetostr(¤ttime);
368 2 gotoxy(6,0);
369 2 print(currenttime.datestring);
370 2 gotoxy(6,1);
371 2 print(currenttime.timestring);
372 2
373 2 delay(300);
374 2 }
375 1 }
376
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 845 ----
CONSTANT SIZE = 14 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 26
IDATA SIZE = ---- ----
BIT SIZE = ---- 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -