题目2_时钟.LST
资源名称:89s52_rtc.rar [点击查看]
上传用户:wzx8880901
上传日期:2009-04-09
资源大小:37k
文件大小:31k
源码类别:
嵌入式/单片机编程
开发平台:
C/C++
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 1
- C51 COMPILER V8.05a, COMPILATION OF MODULE 题目2_时钟
- OBJECT MODULE PLACED IN 题目2_时钟.OBJ
- COMPILER INVOKED BY: C:KeilC51BINC51.EXE 题目2_时钟.c DEBUG OBJECTEXTEND
- line level source
- 1 #include <reg52.h>
- 2 #define uchar unsigned char
- 3 #define uint unsigned int
- 4 sbit p34=P2^2;
- 5 sbit p35=P2^1;
- 6 sbit p36=P2^0;
- 7 sbit dula=P2^7;
- 8 sbit wei1=P2^6;
- 9 sbit wei2=P2^5;
- 10 bit runnian,dayue,flag;
- 11 uchar code tabledu[]={
- 12 0x3f,0x06,0x5b,0x4f,
- 13 0x66,0x6d,0x7d,0x07,
- 14 0x7f,0x6f,0x00};
- 15 uchar code tablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- 16 uchar nian,yue,ri,shi,fen,miao,xq,cont,day,days,s,tt,a;
- 17 uint temp;
- 18 int week(int y,int m,int d) ;
- 19 void init();
- 20 void display1();
- 21 void display2();
- 22 void delay(uchar z);
- 23 void time();
- 24 void year();
- 25 void key();
- 26 void jia();
- 27 void jian();
- 28 void xiaoy();
- 29 void xiaoy1();
- 30 void dxyue();
- 31
- 32 void main()
- 33 {
- 34 1 init(); //初始化
- 35 1 dxyue();
- 36 1 while(1)
- 37 1 {
- 38 2 time();
- 39 2 display1();
- 40 2 display2();
- 41 2 key();
- 42 2 }
- 43 1 }
- 44
- 45 void key()
- 46 {
- 47 1 if(p34==0) //选择更改位。
- 48 1 {
- 49 2 delay(20); //去抖
- 50 2 if(p34==0)
- 51 2 {
- 52 3 while(!p34)
- 53 3 {
- 54 4 display1(); //显示子函数打包
- 55 4 display2();
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 2
- 56 4 }
- 57 3 delay(10);
- 58 3 s++;
- 59 3 cont=0; //保证按键按下时间过长时,松开后,能马上闪烁而不用等待.
- 60 3 if(s==7)
- 61 3 {
- 62 4 s=0;
- 63 4 }
- 64 3 }
- 65 2 }
- 66 1 if(p35==0) //加位
- 67 1 {
- 68 2 if(s==0) // 没有按p34的情况下,按P35或P36的无效。
- 69 2 {}
- 70 2 else
- 71 2 { delay(10);
- 72 3 if(p35==0)
- 73 3 {
- 74 4 while(!p35)
- 75 4 {
- 76 5 display1(); //显示子函数打包
- 77 5 display2();
- 78 5 }
- 79 4 jia();
- 80 4 }
- 81 3 }
- 82 2 }
- 83 1 if(p36==0) //减位
- 84 1 {
- 85 2 if(s==0) // 没有按p34的情况下,按P35或P36的无效。
- 86 2 {}
- 87 2 else
- 88 2 { delay(10);
- 89 3 if(p36==0)
- 90 3 {
- 91 4 while(p36==0)
- 92 4 {
- 93 5 display1(); //显示子函数打包
- 94 5 display2();
- 95 5 }
- 96 4 jian();
- 97 4 }
- 98 3 }
- 99 2 }
- 100 1 }
- 101 void jian() //减
- 102 {
- 103 1 if(s==1) //年
- 104 1 {
- 105 2 nian--; xq=week(nian,yue,ri);
- 106 2 if(nian==0xff) //2099年
- 107 2 {
- 108 3 nian=99;
- 109 3 }
- 110 2 year();
- 111 2 }
- 112 1 if(s==2) //月
- 113 1 {
- 114 2 yue--; xq=week(nian,yue,ri);
- 115 2 if(yue==0)
- 116 2 {
- 117 3 yue=12;
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 3
- 118 3 }
- 119 2 dxyue();
- 120 2 }
- 121 1 if(s==3) //日
- 122 1 {
- 123 2 ri--; xq=week(nian,yue,ri);
- 124 2 if(ri==0)
- 125 2 {
- 126 3 if(yue==2)
- 127 3 {
- 128 4 if(runnian)
- 129 4 {
- 130 5 ri=29;
- 131 5 }
- 132 4 else
- 133 4 {
- 134 5 ri=28;
- 135 5 }
- 136 4 }
- 137 3 else
- 138 3 {
- 139 4 if(dayue)
- 140 4 {
- 141 5 ri=31;
- 142 5 }
- 143 4 else
- 144 4 {
- 145 5 ri=30;
- 146 5 }
- 147 4 }
- 148 3 }
- 149 2
- 150 2 }
- 151 1 if(s==4)
- 152 1 {
- 153 2 shi--;
- 154 2 if(shi==0xff)
- 155 2 {
- 156 3 shi=23;
- 157 3 }
- 158 2 }
- 159 1 if(s==5)
- 160 1 {
- 161 2 fen--;
- 162 2 if(fen==0xff)
- 163 2 {
- 164 3 fen=59;
- 165 3 }
- 166 2 }
- 167 1 if(s==6)
- 168 1 {
- 169 2 miao--;
- 170 2 if(miao==0xff)
- 171 2 {
- 172 3 miao=59;
- 173 3 }
- 174 2 }
- 175 1 }
- 176 void jia()
- 177 {
- 178 1 if(s==1)
- 179 1 {
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 4
- 180 2 nian++; xq=week(nian,yue,ri);
- 181 2 year();
- 182 2 }
- 183 1 if(s==2)
- 184 1 {
- 185 2 yue++; xq=week(nian,yue,ri);
- 186 2 if(yue==13)
- 187 2 {
- 188 3 yue=1;
- 189 3 }
- 190 2 dxyue();
- 191 2 }
- 192 1 if(s==3)
- 193 1 {
- 194 2 ri++; xq=week(nian,yue,ri);
- 195 2 if(yue==2)
- 196 2 {
- 197 3
- 198 3 if(ri>=day)
- 199 3 {
- 200 4 ri=1;
- 201 4 }
- 202 3 }
- 203 2 else
- 204 2 {
- 205 3 if(ri>=days)
- 206 3 {
- 207 4 ri=1;
- 208 4 }
- 209 3 }
- 210 2 }
- 211 1 if(s==4)
- 212 1 {
- 213 2 shi++;
- 214 2 if(shi==24)
- 215 2 {
- 216 3 shi=0;
- 217 3 }
- 218 2 }
- 219 1 if(s==5)
- 220 1 {
- 221 2 fen++;
- 222 2 if(fen==60)
- 223 2 {
- 224 3 fen=0;
- 225 3 }
- 226 2 }
- 227 1 if(s==6)
- 228 1 {
- 229 2 miao++;
- 230 2 if(miao==60)
- 231 2 {
- 232 3 miao=0;
- 233 3 }
- 234 2 }
- 235 1 }
- 236 void init()
- 237 {
- 238 1 flag=1;
- 239 1 TMOD=0x01;
- 240 1 TH0=0X4C;
- 241 1 TL0=0X00;
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 5
- 242 1 EA=1;
- 243 1 ET0=1;
- 244 1 TR0=1;
- 245 1 nian=8;
- 246 1 yue=11;
- 247 1 ri=22;
- 248 1 shi=20;
- 249 1 fen=30;
- 250 1 year();
- 251 1 s=0;
- 252 1 P0=0;
- 253 1 dula=wei1=wei2=0;
- 254 1
- 255 1 }
- 256 void xiaoy()
- 257 {
- 258 1 //------------------------------
- 259 1 P0=0;
- 260 1 dula=1;
- 261 1 P0=0;
- 262 1 dula=0;
- 263 1 P0=0xff;
- 264 1 wei2=1;
- 265 1 P0=tablewe[7];
- 266 1 wei2=0;
- 267 1 delay(1);
- 268 1 //------------------------------
- 269 1 }
- 270 void xiaoy1()
- 271 {
- 272 1 //------------------------------
- 273 1 P0=0;
- 274 1 dula=1;
- 275 1 P0=0;
- 276 1 dula=0;
- 277 1 P0=0xff;
- 278 1 wei1=1;
- 279 1 P0=tablewe[7];
- 280 1 wei1=0;
- 281 1 delay(1);
- 282 1 //------------------------------
- 283 1 }
- 284 void display2() //时分秒
- 285 {
- 286 1
- 287 1 uchar x,y;
- 288 1 //----------------------------------
- 289 1 x=shi%100/10; //时十位
- 290 1 y=shi%10; //时个位
- 291 1 P0=0x00;
- 292 1 if(s==4) //校对时间时闪烁用。
- 293 1 {
- 294 2 if(cont==12)
- 295 2 {
- 296 3 cont=0;
- 297 3 flag=~flag;
- 298 3
- 299 3 }
- 300 2 if(flag)
- 301 2 {
- 302 3 x=0x0a;
- 303 3 y=0x0a;
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 6
- 304 3
- 305 3 }
- 306 2 }
- 307 1 P0=0;
- 308 1 dula=1;
- 309 1 P0=tabledu[x];
- 310 1 dula=0;
- 311 1 P0=0xff;
- 312 1 wei2=1;
- 313 1 P0=tablewe[0];
- 314 1 wei2=0;
- 315 1 delay(2);
- 316 1 //--------------------------
- 317 1 xiaoy(); //消隐
- 318 1 //--------------------------
- 319 1
- 320 1 P0=0; //时个
- 321 1 dula=1;
- 322 1 P0=tabledu[y];
- 323 1 dula=0;
- 324 1 P0=0xff;
- 325 1 wei2=1;
- 326 1 P0=tablewe[1];
- 327 1 wei2=0;
- 328 1 delay(2);
- 329 1 //--------------------------
- 330 1 xiaoy(); //消隐
- 331 1 //--------------------------
- 332 1
- 333 1 x=fen%100/10; //分十位
- 334 1 y=fen%10; //分个位
- 335 1 if(s==5)
- 336 1 {
- 337 2 if(cont==12)
- 338 2 {
- 339 3 cont=0;
- 340 3 flag=~flag;
- 341 3
- 342 3 }
- 343 2 if(flag)
- 344 2 {
- 345 3 x=0x0a;
- 346 3 y=0x0a;
- 347 3
- 348 3 }
- 349 2 }
- 350 1 P0=0;
- 351 1 dula=1;
- 352 1 P0=tabledu[x];
- 353 1 dula=0; P0=0xff;
- 354 1 wei2=1;
- 355 1 P0=tablewe[2];
- 356 1 wei2=0;
- 357 1 delay(2);
- 358 1 //--------------------------
- 359 1 xiaoy(); //消隐
- 360 1 //--------------------------
- 361 1
- 362 1 //分个位
- 363 1 P0=0;
- 364 1 dula=1;
- 365 1 P0=tabledu[y];
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 7
- 366 1 dula=0; P0=0xff;
- 367 1 wei2=1;
- 368 1 P0=tablewe[3];
- 369 1 wei2=0;
- 370 1 delay(2);
- 371 1 //--------------------------
- 372 1 xiaoy(); //消隐
- 373 1 //--------------------------
- 374 1 x=miao%100/10; //秒十位
- 375 1 y=miao%10;
- 376 1
- 377 1 //秒十位
- 378 1 if(s==6)
- 379 1 {
- 380 2 if(cont==12)
- 381 2 {
- 382 3 cont=0;
- 383 3 flag=~flag;
- 384 3
- 385 3 }
- 386 2 if(flag)
- 387 2 {
- 388 3 x=0x0a;
- 389 3 y=0x0a;
- 390 3 }
- 391 2 }
- 392 1 P0=0;
- 393 1 dula=1;
- 394 1 P0=tabledu[x];
- 395 1 dula=0;
- 396 1 P0=0xff;
- 397 1 wei2=1;
- 398 1 P0=tablewe[4];
- 399 1 wei2=0;
- 400 1 delay(2);
- 401 1 //--------------------------
- 402 1 xiaoy(); //消隐
- 403 1 //--------------------------
- 404 1
- 405 1 P0=0; //秒个位
- 406 1 dula=1;
- 407 1 P0=tabledu[y];
- 408 1 dula=0;
- 409 1
- 410 1 P0=0xff;
- 411 1 wei2=1;
- 412 1 P0=tablewe[5];
- 413 1 wei2=0;
- 414 1 delay(2);
- 415 1 P0=0;
- 416 1 //--------------------------
- 417 1 xiaoy(); //消隐
- 418 1 //--------------------------
- 419 1 //----------------------------------
- 420 1 P0=0; //星期显示
- 421 1 dula=1;
- 422 1 P0=tabledu[xq];
- 423 1 dula=0;
- 424 1
- 425 1 P0=0xff;
- 426 1 wei2=1;
- 427 1 P0=tablewe[6];
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 8
- 428 1 wei2=0;
- 429 1 delay(2);
- 430 1 P0=0;
- 431 1 //--------------------------------
- 432 1 P0=0; //无效作用位
- 433 1 dula=1;
- 434 1 P0=0;
- 435 1 dula=0;
- 436 1
- 437 1 P0=0xff;
- 438 1 wei2=1;
- 439 1 P0=tablewe[7];
- 440 1 wei2=0;
- 441 1 delay(2);
- 442 1 P0=0;
- 443 1 //-----------------
- 444 1 }
- 445
- 446 void display1() //年月日
- 447 {
- 448 1 uchar x,y;
- 449 1 //----------------------------------
- 450 1 x=nian%100/10; //年十位
- 451 1 y=nian%10; //年个位
- 452 1 P0=0x00;
- 453 1 if(s==1) //校对时间时闪烁用。
- 454 1 {
- 455 2 if(cont==12)
- 456 2 {
- 457 3 cont=0;
- 458 3 flag=~flag;
- 459 3
- 460 3 }
- 461 2 if(flag)
- 462 2 {
- 463 3 x=0x0a;
- 464 3 y=0x0a;
- 465 3 }
- 466 2 }
- 467 1 P0=0;
- 468 1 dula=0;
- 469 1 dula=1;
- 470 1 P0=tabledu[x];
- 471 1 dula=0;
- 472 1
- 473 1 P0=0xff;
- 474 1 wei1=0;
- 475 1 wei1=1;
- 476 1 P0=tablewe[0];
- 477 1 wei1=0;
- 478 1 delay(2);
- 479 1 //--------------------------
- 480 1 xiaoy1(); //消隐
- 481 1 //--------------------------
- 482 1
- 483 1 dula=0;
- 484 1 P0=0; //年个
- 485 1 dula=1;
- 486 1 P0=tabledu[y];
- 487 1 dula=0;
- 488 1 P0=0xff;
- 489 1 wei1=1;
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 9
- 490 1 P0=tablewe[1];
- 491 1 wei1=0;
- 492 1 delay(2);
- 493 1 //--------------------------
- 494 1 xiaoy1(); //消隐
- 495 1 //--------------------------
- 496 1 //----------------------------------
- 497 1 //----------------------------------
- 498 1 x=yue%100/10; //月十位
- 499 1 y=yue%10; //月个位
- 500 1 if(s==2)
- 501 1 {
- 502 2 if(cont==12)
- 503 2 {
- 504 3 cont=0;
- 505 3 flag=~flag;
- 506 3
- 507 3 }
- 508 2 if(flag)
- 509 2 {
- 510 3 x=0x0a;
- 511 3 y=0x0a;
- 512 3
- 513 3 }
- 514 2 }
- 515 1 P0=0;
- 516 1 dula=1;
- 517 1 P0=tabledu[x];
- 518 1 dula=0; P0=0xff;
- 519 1 wei1=1;
- 520 1 P0=tablewe[2];
- 521 1 wei1=0;
- 522 1 delay(2);
- 523 1 //--------------------------
- 524 1 xiaoy1(); //消隐
- 525 1 //--------------------------
- 526 1
- 527 1 //月个位
- 528 1 P0=0;
- 529 1 dula=1;
- 530 1 P0=tabledu[y];
- 531 1 dula=0; P0=0xff;
- 532 1 wei1=1;
- 533 1 P0=tablewe[3];
- 534 1 wei1=0;
- 535 1 delay(2);
- 536 1 //--------------------------
- 537 1 xiaoy1(); //消隐
- 538 1 //--------------------------
- 539 1 //----------------------------------
- 540 1 //----------------------------------
- 541 1 x=ri%100/10; //日十位
- 542 1 y=ri%10;
- 543 1
- 544 1 //日个位
- 545 1 if(s==3)
- 546 1 {
- 547 2 if(cont==12)
- 548 2 {
- 549 3 cont=0;
- 550 3 flag=~flag;
- 551 3
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 10
- 552 3 }
- 553 2 if(flag)
- 554 2 {
- 555 3 x=0x0a;
- 556 3 y=0x0a;
- 557 3
- 558 3 }
- 559 2 }
- 560 1 P0=0;
- 561 1 dula=1;
- 562 1 P0=tabledu[x];
- 563 1 dula=0; P0=0xff;
- 564 1 wei1=1;
- 565 1 P0=tablewe[4];
- 566 1 wei1=0;
- 567 1 delay(2);
- 568 1 //--------------------------
- 569 1 xiaoy1(); //消隐
- 570 1 //--------------------------
- 571 1
- 572 1 P0=0; //日个位
- 573 1 dula=1;
- 574 1 P0=tabledu[y];
- 575 1 dula=0;
- 576 1 P0=0xff;
- 577 1 wei1=1;
- 578 1 P0=tablewe[5];
- 579 1 wei1=0;
- 580 1 P0=0;
- 581 1 delay(2);
- 582 1 //--------------------------
- 583 1 xiaoy1(); //消隐
- 584 1 //--------------------------
- 585 1 //----------------------------------
- 586 1 P0=0; //第一锁存器无效作用位
- 587 1 dula=1;
- 588 1 P0=0 ;
- 589 1 dula=0;
- 590 1 P0=0xff;
- 591 1 wei1=1;
- 592 1 P0=tablewe[6];
- 593 1 wei1=0;
- 594 1 P0=0;
- 595 1 delay(2);
- 596 1
- 597 1 //---------------------
- 598 1
- 599 1 }
- 600
- 601
- 602
- 603
- 604 void year() //平润年
- 605 {
- 606 1 uint aaa;
- 607 1 aaa=2000+nian; //为了计算闰年。
- 608 1 if(((aaa%4 == 0)&&(aaa%100!=0)) || (aaa%400==0))
- 609 1 {
- 610 2 runnian=1;
- 611 2 day=30; //闰年
- 612 2 }
- 613 1 else
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 11
- 614 1 {
- 615 2 runnian=0;
- 616 2 day=29; //平年
- 617 2 }
- 618 1 }
- 619
- 620 void dxyue() //大小月
- 621 {
- 622 1
- 623 1 if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
- 624 1 {
- 625 2 days=32; //31天
- 626 2 }
- 627 1 else
- 628 1 {
- 629 2 days=31; //30天
- 630 2 }
- 631 1 }
- 632 void time()
- 633 {
- 634 1 if(cont==40) //一秒钟加一。不是很精确。
- 635 1 {
- 636 2 cont=0;
- 637 2 miao++;
- 638 2 if(miao==60)
- 639 2 {
- 640 3 miao=0;
- 641 3 fen++;
- 642 3 if(fen==60)
- 643 3 {
- 644 4 fen=0;
- 645 4 shi++;
- 646 4 if(shi==24)
- 647 4 {
- 648 5 shi=0;
- 649 5 ri++;
- 650 5 xq=week(nian,yue,ri);
- 651 5 dxyue();
- 652 5 if(yue!=2)
- 653 5 {
- 654 6 if(ri==days)
- 655 6 {
- 656 7 ri=1;
- 657 7 yue++;
- 658 7 xq=week(nian,yue,ri);
- 659 7 if(yue==13)
- 660 7 {
- 661 8 yue=1;
- 662 8 nian++;
- 663 8 xq=week(nian,yue,ri);
- 664 8 year();
- 665 8 }
- 666 7 }
- 667 6 }
- 668 5 else
- 669 5 {
- 670 6 if(ri==day)
- 671 6 {
- 672 7 ri=1;
- 673 7 yue++;
- 674 7 }
- 675 6 }
- C51 COMPILER V8.05a 题目2_时钟 09/19/2009 01:34:58 PAGE 12
- 676 5 }
- 677 4 }
- 678 3 }
- 679 2 }
- 680 1
- 681 1 }
- 682 void dingshi() interrupt 1
- 683 {
- 684 1 TH0=0x4C;
- 685 1 TL0=0x00;
- 686 1 cont++;
- 687 1 }
- 688 void delay(uchar z)
- 689 {
- 690 1 uint x,y;
- 691 1 for(x=z;x>0;x--)
- 692 1 for(y=20;y>0;y--);
- 693 1 }
- 694 int week(int y,int m,int d)
- 695 {
- 696 1 int m_d[12]={2,5,5,1,3,6,1,4,7,2,5,7};
- 697 1 int i,j,k=0;
- 698 1 int s=2;
- 699 1 int w=0;
- 700 1 y=2000+y;
- 701 1 if (((y%4==0)&&(y%100!=0))||(y%400)==0)
- 702 1 {
- 703 2 if (m<=2)
- 704 2 j=y-1;
- 705 2 else
- 706 2 j=y;
- 707 2 }
- 708 1 else
- 709 1 j=y;
- 710 1
- 711 1 for (i=1904;i<=y;i=i+4)
- 712 1 {
- 713 2 if (i%100!=0)
- 714 2 k++;
- 715 2 else if (i%400==0)
- 716 2 k++;
- 717 2 }
- 718 1 w=((j-1904+s+k)+m_d[m-1]+d)%7;
- 719 1 return (w);
- 720 1 }
- MODULE INFORMATION: STATIC OVERLAYABLE
- CODE SIZE = 1759 ----
- CONSTANT SIZE = 43 ----
- XDATA SIZE = ---- ----
- PDATA SIZE = ---- ----
- DATA SIZE = 15 38
- IDATA SIZE = ---- ----
- BIT SIZE = 3 ----
- END OF MODULE INFORMATION.
- C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)