#include #include #define PA XBYTE[0x0000] #define PB XBYTE[0x2000] #define PC XBYTE[0x4000] #define CMD XBYTE[0x6000] sbit up=P1^0; //定义特殊功能器的位变量 sbit down=P1^1; sbit FMQ=P1^3; bit flag_TR=0; unsigned char flag_SET=0,shi=0,fen=0,miao=0,temp=0,i; unsigned char shu[10]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 }; void init() //初始化 { IE=0x87; //开总中断允许,开外部中断0,1,定时中断允许T0; TMOD=0x01; //TO,T1工作在定时方式,T0工作在方式1,T1工作在方式0 TH0=0x4c; //设置初值 TL0=0X00; TCON=0x05; //设定外部中断0,1为跳沿触发方式 CMD=0x80; //设定数据指针起始值 } void delay(unsigned int m) //延迟 { unsigned char count; while(m!=0) { for(count=0;count<239;count++){} m--; } } void display(unsigned char shi,unsigned char fen,unsigned char miao) //展示时分秒 { unsigned char shi0,shi1,fen0,fen1,miao0,miao1; shi0=shi/10; shi1=shi%10; //时的高位,低位 fen0=fen/10; fen1=fen%10; //分的表示 miao0=miao/10; miao1=miao%10; //秒的表示 PA=0x80; //选择时分秒代表的数码管 PB=shu[shi0]; delay(5); PA=0x40; if(flag_SET==3&&!flag_TR) PB=shu[shi1]&0x7f; //保留shi1后七位 else PB=shu[shi1]; delay(5); PA=0x20; PB=shu[fen0]; delay(5); PA=0x10; if(flag_SET==2&&!flag_TR) PB=shu[fen1]&0x7f; else PB=shu[fen1]; delay(5); PA=0x08; PB=shu[miao0]; delay(5); PA=0x04; if(flag_SET==1&&!flag_TR) PB=shu[miao1]&0x7f; else PB=shu[miao1]; delay(5); } void main() //主程序 { init(); //初始化 while(1) { display(shi,fen,miao); //显示时分秒 if(flag_SET) //设定时间 { TR0=0; //关中断,停止时间自动增加 if(flag_SET==1&&up==0)//设置秒的加 { while(up==0); miao++; if(miao==60)miao=0; //设置上限为60秒 } if(flag_SET==1&&down==0)//设置秒的减 { while(down==0); miao--; if(miao>60)miao=59; //设置0秒前为59 } if(flag_SET==2&&up==0) //设置分的加 { while(up==0); fen++; if(fen==60)fen=0; //设置上限为60分 } if(flag_SET==2&&down==0)//设置分的减 { while(down==0); fen--; if(fen>60)fen=59; //设置0分的前一位为59 } if(flag_SET==3&&up==0) //设置时的加 { while(up==0); shi++; if(shi==24)shi=0; //设置时的上限为24 } if(flag_SET==3&&down==0)//设置时的减 { while(down==0); shi--; if(shi>24)shi=23; //设置0时的前一位是23时 } } if(flag_TR==1) //判断如果按键按下,继续开始时间递增 TR0=1; else TR0=0; if(flag_TR==1&&fen==0&&miao==0) //报时 { for(i=0;i