- #include <reg52.h>
- typedef unsigned int u16; //对数据类型进行声明定义
- typedef unsigned char u8;
- u8 shi=12,feng=00,miao=10;
- u8 shi1=00,feng1=00,miao1=00;
- u8 a,b;
- u16 yangshi=8000;
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- u16 cnt=0,cntt=0;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit keymode=P3^1;//调时时间
- sbit keyclock=P3^0;//调闹钟
- sbit keyup=P3^2;//时间加
- sbit keydown=P3^3;//时间减
- void delay(u16 i)
- {
- while(i--);
- }
- void Timer0Init() //100微秒@11.0592MHz
- {
-
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x02; //设置定时器模式
- TL0 = 0xA4; //设置定时初始值
- TH0 = 0xA4; //设置定时重载值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- EA=1;
- ET0=1;
- }
- void Timer0 () interrupt 1
- {
- TL0 = 0xA4; //设置定时初始值
- TH0 = 0xA4; //设置定时重载值
- cnt++;
- if(cnt==100)
- {
- cnt=0;
- cntt++;
- }
- if(cntt==100)
- {
- cntt=0;
- miao++;
- }
- if(miao==60)
- {
- miao=0;
- feng++;
- }
- if(feng==60)
- {
- feng=0;
- shi++;
- }
- if(shi==24)
- {
- shi=0;
-
- }
- }
- void display0()
- {
- LSA=1;LSB=1;LSC=1;P0=smgduan[shi/10];delay(100);P0=0x00;
- LSA=0;LSB=1;LSC=1;P0=smgduan[shi%10];delay(100);P0=0x00;
- LSA=0;LSB=1;LSC=0;P0=0x40;delay(100);P0=0x00;
- LSA=0;LSB=0;LSC=1;P0=smgduan[feng/10];delay(100);P0=0x00;
- LSA=1;LSB=1;LSC=0;P0=smgduan[feng%10];delay(100);P0=0x00;
- LSA=1;LSB=0;LSC=1;P0=0x40;delay(100);P0=0x00;
- LSA=1;LSB=0;LSC=0;P0=smgduan[miao/10];delay(100);P0=0x00;
- LSA=0;LSB=0;LSC=0;P0=smgduan[miao%10];delay(100);P0=0x00;
-
-
-
- }
- void displaykeyclock()
- {
-
- if(b==1)
- {
- LSA=1;LSB=1;LSC=1;P0=smgduan[shi1/10];delay(100);P0=0x00;
- LSA=0;LSB=1;LSC=1;P0=smgduan[shi1%10];delay(100);P0=0x00;
- }
-
- LSA=1;LSB=0;LSC=1;P0=0x40;delay(100);P0=0x00;
- if(b==2)
- {
- LSA=0;LSB=0;LSC=1;P0=smgduan[feng1/10];delay(100);P0=0x00;
- LSA=1;LSB=1;LSC=0;P0=smgduan[feng1%10];delay(100);P0=0x00;
- }
- LSA=0;LSB=1;LSC=0;P0=0x40;delay(100);P0=0x00;
- if(b==3)
- {
- LSA=1;LSB=0;LSC=0;P0=smgduan[miao1/10];delay(100);P0=0x00;
- LSA=0;LSB=0;LSC=0;P0=smgduan[miao1%10];delay(100);P0=0x00;
- }
-
-
- else if((a==0)&&(b==0))
- {
- display0();
-
- }
- }
- void displaykeymode()
- {
- if(a==1)
- {
- LSA=1;LSB=1;LSC=1;P0=smgduan[shi/10];delay(100);P0=0x00;
- LSA=0;LSB=1;LSC=1;P0=smgduan[shi%10];delay(100);P0=0x00;
- }
- LSA=1;LSB=0;LSC=1;P0=0x40;delay(100);P0=0x00;
- if(a==2)
- {
- LSA=0;LSB=0;LSC=1;P0=smgduan[feng/10];delay(100);P0=0x00;
- LSA=1;LSB=1;LSC=0;P0=smgduan[feng%10];delay(100);P0=0x00;
- }
- LSA=0;LSB=1;LSC=0;P0=0x40;delay(100);P0=0x00;
- if(a==3)
- {
- LSA=1;LSB=0;LSC=0;P0=smgduan[miao/10];delay(100);P0=0x00;
- LSA=0;LSB=0;LSC=0;P0=smgduan[miao%10];delay(100);P0=0x00;
- }
-
- else if((a==0)&&(b==0))
- {
- display0();
-
- }
- }
- void key_mode()
- {
- if(!keymode)
- {
- delay(1000);
- if(!keymode)
- { b=0;
- a++;
- if(a>3)
- {
- a=0;
- }
-
- }
- while(!keymode);
- }
- }
-
-
-
- void key_clock()
- {
- if(!keyclock)
- {
- delay(1000);
- if(!keyclock)
- { a=0;
- b++;
- if(b>3)
- {
-
- b=0;
- }
- }
- while(!keyclock);
- }
- }
- void keymodeset()
- {
- if(!keyup)
- {
- delay(1000);
- if(!keyup)
- {
-
- if(a == 1) {shi++;} if(shi>23){shi=0;}
- else if(a == 2) {feng++;} if(feng>59){feng=0;}
- else if(a == 3) {miao++;} if(miao>59){miao=0;}
- }
- while(!keyup);
- }
- if(!keydown)
- {
- delay(1000);
- if(!keydown)
- {
- if(a == 1) {if(shi != 0) shi--; else shi = 23;}
- else if(a == 2) {if(feng != 0) feng--; else feng = 59;}
- else if(a == 3) {if(miao != 0) miao--; else miao = 59;}
- }
- while(!keydown);
- }
- }
- void keyclockset()
- {
- if(!keyup)
- {
- delay(1000);
- if(!keyup)
- {
-
- if(b==1) {shi1++;} if(shi1>23){shi1=0;}
- else if(b==2) {feng1++;} if(feng1>59){feng1=0;}
- else if(b==3) {miao1++;} if(miao1>59){miao1=0;}
- }
- while(!keyup);
- }
- if(!keydown)
- {
- delay(1000);
- if(!keydown)
- {
- if(b == 1) {if(shi1 != 0) shi1--; else shi1 = 23;}
- else if(b == 2) {if(feng1 != 0) feng1--; else feng1 = 59;}
- else if(b == 3) {if(miao1 != 0) miao1--; else miao1 = 59;}
- }
- while(!keydown);
- }
- }
- void keyclockscan()
- {
- key_clock();
- if(b==1)
- {
-
- displaykeyclock();
- keyclockset();
- }
- else
- if(b==2)
- {
-
- displaykeyclock();
- keyclockset();
- }
- else
- if(b==3)
- {
-
- displaykeyclock();
- keyclockset();
- }
- }
- void keymodescan()
- {
- key_mode();
- if(a==1)
- {
-
- displaykeymode();
- keymodeset();
- }
- else
- if(a==2)
- {
-
- displaykeymode();
- keymodeset();
- }
- else
- if(a==3)
- {
-
-
- displaykeymode();
- keymodeset();
-
- }
- }
- void main()
- {
- Timer0Init();
- while(1)
- {
-
- displaykeyclock();
-
- keyclockscan();
-
- displaykeymode();
- keymodescan();
-
-
- }
- }
复制代码
|