改这样楼主试试。
- #include<stc15.h>
- /*共阴数码管编码*/
- unsigned char code tab1[]={
- 0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f};
- /*共阳数码管编码
- unsigned char code tab1[]={
- 0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e};
- */
- /*位选编码*/
- unsigned char code tab2[]={
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- unsigned char dis_buf[8]={0,0,0x40,0,0,0x40,0,0};
- //sbit duanxuan=P1^0;
- /*按键位定义*/
- sbit key0=P1^0;
- sbit key1=P1^1;
- sbit key2=P1^2;
- sbit key3=P1^3;
- sbit key4=P1^4;
- sbit key5=P1^5;
- sbit key6=P1^6;
- sbit key7=P1^7;
- /*秒、分、时的初始化*/
- unsigned int tt=0;
- unsigned char i=0;
- char sec=0,fen=0,shi=12;
- bit sign=1;
- /*延时函数*/
- void delay(unsigned int n)
- {
- while(n--);
- }
- /*定时器0初始化函数*/
- void chushihua() //1毫秒@12.000MHz
- {
- AUXR &= 0x7F; //定时器时钟12T模式
- TMOD &= 0xF0; //设置定时器16位自动重载模式
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- TR0 = 1; //定时器0开始计时
- EA=1;
- ET0=1;
- }
- /*按键设定函数*/
- void sheding()
- {
- if(key0==0)
- {
- delay(3500);
- if(key0==0)
- {
- sign=0;
- }
- }
- if(key1==0)
- {
- delay(3500);
- if(key1==0)
- {
- sec++;
- if(sec>59)
- sec=0;
- while(!key1);
- }
- }
- if(key2==0)
- {
- delay(3500);
- if(key2==0)
- {
- fen++;
- if(fen>59)
- fen=0;
- while(!key2);
- }
- }
- if(key3==0)
- {
- delay(3500);
- if(key3==0)
- {
- shi++;
- if(shi>23)
- shi=0;
- while(!key3);
- }
- }
- if(key5==0)
- {
- delay(3500);
- if(key5==0)
- {
- sec--;
- if(sec<0)
- sec=59;
- while(!key5);
- }
- }
- if(key6==0)
- {
- delay(3500);
- if(key6==0)
- {
- fen--;
- if(fen<0)
- fen=59;
- while(!key6);
- }
- }
- if(key7==0)
- {
- delay(3500);
- if(key7==0)
- {
- shi--;
- if(shi<0)
- shi=23;
- while(!key7);
- }
- }
- if(key4==0)
- {
- delay(3500);
- if(key4==0)
- {
- sign=1;
- }
- }
- }
- /*数据分解*/
- void timedeal()
- {
- dis_buf[0]=tab1[sec%10];
- dis_buf[1]=tab1[sec/10];
- dis_buf[3]=tab1[fen%10];
- dis_buf[4]=tab1[fen/10];
- dis_buf[6]=tab1[shi%10];
- dis_buf[7]=tab1[shi/10];
- }
- /*显示函数*/
- void display()
- {
- P6=0x00;//消影
- P7=tab2[i];//送位码
- P6=dis_buf[i];//送段码
- i++;
- i%=8;
- }
- /*主函数*/
- void main()
- {
- chushihua();
- while(1)
- {
- sheding();
- timedeal();
- }
- }
- /*定时器0中断函数*/
- void time0() interrupt 1
- {
- if(sign)
- {
- tt++;
- if(tt>=1000)
- {
- tt=0;
- sec++;
- if(sec>=60)
- {
- sec=0;
- fen++;
- if(fen>=60)
- {
- fen=0;
- shi++;
- if(shi>=24)
- {
- shi=0;
- }
- }
- }
- }
- }
- display();
- }
复制代码 |