源代码
#include <reg52.h> sbit k0=P1^0; sbit k1=P1^1; sbit k2=P1^2; unsigned char second,minute,hour,i,k,l,j,x,y; unsigned int n; unsigned char code mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f}; timer0(void) interrupt 1 { n++; TH0=0x3c; TL0=0xb0; } void delay() { unsigned int time,p; for(time=30;time>0;time--) for(p=0;p<10;p++); } void xianshi() { if(n==20) { n=0; second++; while(second==60) { second=0; minute++; if(minute==60) {minute=0; hour++; if(hour==24) { hour=0; }}}} x=hour%10; y=hour/10; l=minute%10; j=minute/10; i=second%10; k=second/10;P0=0x00; P2=0x01; P0=mod; delay();P0=0x00; P2=0x02; P0=mod[k]; delay();P0=0x00; P2=0x04; P0=0x40; delay();P0=0x00; P2=0x08; P0=mod[l]; delay();P0=0x00; P2=0x10; P0=mod[j]; delay();P0=0x00; P2=0x20; P0=0x40; delay();P0=0x00; P2=0x40; P0=mod[x]; delay();P0=0x00; P2=0x80; P0=mod[y]; delay();P0=0x00; } void kongzhi() {if(k0==0) {TR0=0; while(k0==0) xianshi(); delay(); while(k0==1) {if(k1==0) {minute++; while(minute==60) {minute=0; }} while(k1==0) xianshi(); if(k2==0) {minute--; while(minute==-1) {minute=59; }} while(k2==0) xianshi(); xianshi(); } if(k0==0) {TR0=0; while(k0==0) xianshi(); delay(); while(k0==1) {if(k1==0) {hour++; while(hour==24) {hour=0; }} while(k1==0) xianshi(); if(k2==0) {hour--; while(hour==-1) {hour=23; }} while(k2==0) xianshi(); xianshi(); } if(k0==0) {while(k0==0) xianshi(); TR0=1; return; }}}} void main() { TMOD=0x01; TH0=0x3c; TL0=0xb0; EA=1; ET0=1; TR0=1; while(1) { xianshi(); kongzhi(); }}
|