断断续续,续续断断,总算是将电子钟做好了 ,哦 不该叫电子钟的,太俗气!哈哈
这周事情太多,进度比较慢,拖到现在才完成。
尽可能的把走时精度调到了最高(不借助于时钟芯片),现在是一天误差在3秒左右,基本达到了设计要求。
做闹钟的时候纠结了很久,板子上有三个按键,在设计时钟和闹钟切换设置的时候有点吃紧,功力还是不够啊,以后要多多努力!
不多说,上图啦
程序代码如下,没有精简,编译大小970字节,想到哪写到哪的,没有优化
- #include
- typedef unsigned char uint8;
- typedef unsigned int uint16;
- uint16 count=0;
- uint8 sec,min,hour,min2,min1,h2,h1;
- uint8 j=0;
- uint16 i=0; //闹钟设置延时
- uint8 alARMflg=0,clock=0,beepclock=0;
- uint8 beepmin=0,beephour=7; //闹钟初始时间
- sfr IPH=0xb7;
- ***it beep=P2^0; //蜂鸣器
- ***it con1=P2^4; //数码管位选
- ***it con2=P2^3;
- ***it con3=P2^2;
- ***it con4=P2^1;
- ***it key1=P1^0; //按键
- ***it key2=P1^1;
- ***it key3=P1^2;
- uint8 code ledcode[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- void delay(uint8 n) //误差 0us //1ms
- {
- unsigned char a,b,c;
- for(c=n;c>0;c--)
- for(b=142;b>0;b--)
- for(a=2;a>0;a--);
- }
- void Inittimer1(void) //计时时钟
- {
- TMOD |= 0x10;
- TMOD &= 0xdf;
- TH1 = 0xb1; //b1 //20ms 校准后的值,不是算出的初始值
- TL1 = 0xea; //ea
- EA = 1;
- ET1 = 1;
- TR1 = 1;
- }
- void InitTimer0(void) //显示时钟
- {
- TMOD |= 0x01;
- TMOD &= 0xfd;
- TH0 = 0xfc; //1ms
- TL0 = 0x18;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void show() //时钟显示
- {
- if(j==4) j=0;
- min2=min%10;
- min1=min/10%10;
- h2=hour%10;
- h1=hour/10%10;
- switch(j)
- {
- case 0: con1=0;con2=con3=con4=1;P0=ledcode[min2];break;
- case 1: con2=0;con1=con3=con4=1;P0=ledcode[min1];break;
- case 2: con3=0;con2=con1=con4=1;P0=ledcode[h2];break;
- case 3: con4=0;con2=con3=con1=1;P0=ledcode[h1];break;
- }
- j++;
-
- }
- void showalarm() //闹钟显示
- {
- if(j==4) j=0;
- min2=beepmin%10;
- min1=beepmin/10%10;
- h2=beephour%10;
- h1=beephour/10%10;
- switch(j)
- {
- case 0: con1=0;con2=con3=con4=1;P0=ledcode[min2];break;
- case 1: con2=0;con1=con3=con4=1;P0=ledcode[min1];break;
- case 2: con3=0;con2=con1=con4=1;P0=ledcode[h2];break;
- case 3: con4=0;con2=con3=con1=1;P0=ledcode[h1];break;
- }
- j++;
-
- }
- void alarm() //闹钟
- {
- uint8 i;
- for (i = 0; i < 5; i++)
- {
- beep = !beep;
- delay(50);
- }
- beep = 1; //关闭蜂鸣器
- delay(100);
- }
- void main(void)
- {
- PT1=1;
- IPH=0x08; //timer1中断优先级最高,提高走时精度
- AUXR=0x01; //减小电磁干扰
- InitTimer1();
- InitTimer0();
- while(1)
- {
- if((key1==0)&&(beepclock==0)) //时间设定
- {
- delay(20);
- if(key1==0)
- {
- count=0;
- sec=0;
- TR0=~TR0;
- TR1=~TR1;
- if(TR1==1) clock=0;
- if(TR1==0) clock=1;
- alarm();
- while(key1==0);
- }
- }
- if((key2==0)&&(TR1==1)&&(TR0==1)) //消铃
- {
- delay(20);
- if(key2==0)
- {
- alarmflg=0;
- while(key2==0);
- }
- }
- if((key3==0)&&(TR1==1)&&(beepclock==0)) //设定闹钟
- {
- delay(20);
- if(key3==0)
- {
- TR0=0;
- beepclock=1;
- showalarm();
- while(key3==0);
- }
- }
- if(TR0==0)
- {
- if(key2==0)
- {
- delay(20);
- if(key2==0)
- {
- if(clock==1) //时钟调整模式
- {
- show();
- }
- if(beepclock==1) //闹钟调整模式
- {
- showalarm();
- }
- while(key2==0);
- }
- }
- if(key3==0)
- {
- delay(20);
- if(key3==0)
- {
- if(clock==1) //时钟设置
- {
- j--;
- switch(j)
- {
- case 0:min++;break;
- case 1:min+=10;break;
- case 2:hour++;break;
- case 3:hour+=10;break;
- }
- if(min>=60) min=0;
- if(hour>=24) hour=0;
- show();
- }
- if(beepclock==1)
- { //闹钟设置
- j--;
- switch(j)
- {
- case 0:beepmin++;break;
- case 1:beepmin+=10;break;
- case 2:beephour++;break;
- case 3:beephour+=10;break;
- }
- if(beepmin>=60) beepmin=0;
- if(beephour>=24) beephour=0;
- showalarm();
- }
-
- while(key3==0);
- }
- }
- }
- if((alarmflg==1)&&(TR1==1))
- {
- alarm();
- }
- }
- }
- void Timer0Interrupt(void) interrupt 1
- {
- TH0 = 0xfc;
- TL0 = 0x18;
- show();
- }
- void Timer1Interrupt(void) interrupt 3
- {
- TH1 = 0xb1;
- TL1 = 0xea;
- count++;
- if(count>=50) //1s
- {
- count=0;
- sec++;
- if(sec>=60)
- {
- sec=0;
- min++;
- if(min>=60)
- {
- min=0;
- hour++;
- if(hour>=24)
- {
- hour=0;
- }
- }
- }
- if((hour==beephour)&&(min==beepmin)&&(sec==1))
- {
- alarmflg=1;
- }
- }
- if(beepclock==1) //闹钟调整状态延时
- {
- i++;
- if(i>=750) //15s
- {
- TR0=1;
- i=0;
- beepclock=0;
- }
- }
-
- }
复制代码
0
评分
-
查看全部评分
|
|
|
|