|
- /***********************************************************
- 名称:基于AT89S52数码管系统测试程序
- 要求:流水灯电路测试,数码管电路测试,独立式键盘电路测试,蜂鸣器电路测试
- 作者:刘杨
- 时间:2012年12月12日
- 电路描述:S1到S8一键一功能;P3口连接LED流水灯;数码管驱动电路采用74HC573;P1口连接独立式键盘;P0口,P2口分别控制数码管位和段;
- ************************************************************/
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit S1=P1^0; //流水灯顺时针流水
- sbit S2=P1^1; //流水灯逆时针
- sbit S3=P1^2; //数码管显示数字0-7
- sbit S4=P1^3; //显示数字钟
- sbit S5=P1^4; //流水灯逆时针
- sbit S6=P1^5; //数码管显示数字0-7
- sbit S7=P1^6;
- sbit S8=P1^7; //显示数字钟
- sbit P3_1=P3^1; //蜂鸣器报警控制端
- uchar code discode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF};
- uchar code discode0[]={0x92,0x86,0xC6,0xC6,0xC1,0x92};
- uchar code discode1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x3F};
- uchar disbitcode[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- uchar disbuf[8]={0,0,10,0,0,10,0,0};
- uchar disbuf0[8]={0,0,10,0,0,10,0,0};
- uchar disbuf1[8]={0,0,10,0,0,10,0,0};//显示缓冲区
- uchar cent,second,minite,hour,second1,minite1,hour1;
- /*函数申明*/
- void display(void);
- void ini(void);
- void t0(void);
- /*************延时程序**************/
- void delay(uint m)
- {
- while(--m);
- }
- /*************初始化程序**************/
- void ini(void)
- {
- TMOD=0x01;
- TH0=(65536-46082)/256;
- TL0=(65536-46082)%256;
- ET0=1;
- EA=1;
- }
- /************缓存程序****************/
- void hccx()
- {
- disbuf1[0]=disbuf[0]=second%10;
- disbuf1[1]=disbuf[1]=second/10;
- disbuf1[3]=disbuf[3]=minite%10;
- disbuf1[4]=disbuf[4]=minite/10;
- disbuf1[6]=disbuf[6]=hour%10;
- disbuf1[7]=disbuf[7]=hour/10;
-
- }
- /***********暂停程序****************/
- void zd (void)
- {
- uchar i;
- //hccx();
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf[i]];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /************闹钟初始化程序*********/
- void naozhong (void)
- {
- uchar i;
- disbuf0[0]=second1%10; //更新显示缓冲区
- disbuf0[1]=second1/10;
- disbuf0[3]=minite1%10;
- disbuf0[4]=minite1/10;
- disbuf0[6]=hour1%10;
- disbuf0[7]=hour1/10;
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf0[i]];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /***********开机显示画面********/
- void shuma()
- {
- while((S1==1)&&(S2==1))
- {
- P0=0X8C;
- P2=0X01;
- }
- }
- /*************显示程序**************/
- void display(void)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf[i]];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /*******设置成功画面***********/
- void succer(void)
- {
- uchar i;
- disbuf0[0]=second1%10; //更新显示缓冲区
- disbuf0[1]=second1/10;
- disbuf0[3]=minite1%10;
- disbuf0[4]=minite1/10;
- disbuf0[6]=hour1%10;
- disbuf0[7]=hour1/10;
- for(i=0;i<6;i++)
- {
- P0=discode0[i];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /**************闹钟显示程序*********/
- void naoxian(void)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P0=0xff;
- P0=discode1[disbuf[i]];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /***********调整显示程序**********/
- void tzcx0()
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf1[i]];
- P2=disbitcode[i];
- delay(100);
- }
- }
- /*************中断程序**************/
- void t0(void) interrupt 1 using 0 //用第0组寄存器
- {
- cent++;
- if(cent==20)
- {
- cent=0;
- second++;
- if(second==60)
- {
- second=0;
- minite++;
- if(minite==60)
- {
- minite=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- disbuf[0]=second%10; //更新显示缓冲区
- disbuf[1]=second/10;
- disbuf[3]=minite%10;
- disbuf[4]=minite/10;
- disbuf[6]=hour%10;
- disbuf[7]=hour/10;
- }
- TH0=(65536-46082)/256;
- TL0=(65536-46082)%256;
- }
- /************按键扫描程序*****************/
- uchar keyscan()
- {
- uchar tmp; //列号,行号
- P1=0xff; //发全行0扫描码
- if((P1&0xff)!=0xff) //若有键按下
- {
- delay(1000); // 延时去抖
- if((P1&0xff)!=0xff) //有键按下
- {
- tmp=(P1&0xff);
- do{}while((P1&0xff)!=0xff);
- tmp=~tmp;
- return (tmp); //还回键值
- }
- }
- return (0); //无键按下,还回0
- }
- /**************主程序************/
- void main(void)
- {
- uchar i,a=0,b=0,c=0,d=0,e=0,f=0,v,nu=0,num=0,n=0;
- ini();
- shuma();
- v=keyscan();
- if(v==1||v==2||v==16||v==32||v==64||v==128)
- {
- while(1)
- {
- v=keyscan();
- switch(v)
- {
- case 1:
- P3=0xff;
- P2=0x00;
- TR0=1; /*开始时钟测试*/
- while(v==1||v==0)
- {
-
- delay(10);
- display();
- v=keyscan(); //调键扫描程序
- }
- break;
- case 2:
- while(v==2||v==0) /*暂停测试*/
- {
- ET0=0;
- delay(10);
- hccx();
- zd();
- v=keyscan(); //调键扫描程序
- }
- break;
-
- case 16:
- while(v==16||(v==0)) /*调整测试*/
- {
- if(num<3) num++;
- else {num=1;}
- v=0;
- while((num==1)&&(v==0))
- {
- hccx();
- while(a==0)
- {
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf1[i]];
- P2=disbitcode[i];
- delay(100);
- }
-
- a=keyscan();
-
- }
- while(a==4) /*数码管显示数字0-7测试*/
- {
- second++;
- if(second==60)
- {
- second=0;
- minite++;
- if(minite==60)
- {
- minite=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- hccx();
- tzcx0(); //扫描间隙延时
- a=keyscan(); //调键扫描程序
- }
- while(a==8) /*显示数字钟测试*/
- {
- second--;
- if(second<=0)
- {
- second=59;
- minite--;
- if(minite<=0)
- {
- minite=59;
- hour--;
- if(hour<=0)
- {
- hour=23;
- }
- }
- }
- hccx();
- tzcx0(); //扫描间隙延时
- a=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
-
- while(num==2&&v==0)
- {
- hccx();
- while(b==0)
- {
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf1[i]];
- P2=disbitcode[i];
- delay(100);
- }
-
- b=keyscan();
- }
- while(b==4) /*数码管显示数字0-7测试*/
- {
- minite++;
- if(minite==60)
- {
- minite=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- hccx();
- tzcx0(); //扫描间隙延时
- b=keyscan(); //调键扫描程序
- }
- while(b==8) /*显示数字钟测试*/
- {
- minite--;
- if(minite<=0)
- {
- minite=59;
- hour--;
- if(hour<=0)
- {
- hour=23;
- }
- }
-
- hccx();
- tzcx0(); //扫描间隙延时
- b=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
- while(num==3&&v==0)
- {
-
- hccx();
- while(c==0)
- {
- for(i=0;i<8;i++)
- {
- P0=discode[disbuf1[i]];
- P2=disbitcode[i];
- delay(100);
- }
-
- c=keyscan();
- }
- while(c==4) /*数码管显示数字0-7测试*/
- {
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- hccx();
- tzcx0(); //扫描间隙延时
- c=keyscan(); //调键扫描程序
- }
- while(c==8) /*显示数字钟测试*/
- {
- hour--;
- if(hour<=0)
- {
- hour=23;
- }
- hccx();
- tzcx0(); //扫描间隙延时
- c=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
- }
- break;
- case 32: /*数码管各段循环测试*/
- while(v==32||v==0)
- {
- ET0=1;
- num=0;
- a=b=c=0;
- display();
- v=keyscan(); //调键扫描程序
- }
- break;
-
- case 64:
- while(v==64||v==0)
- {
- if(nu<3) nu++;
- else {nu=1;}
- v=0;
- while(nu==1&&v==0)
- {
- while(d==0)
- {
- naozhong();
- d=keyscan();
- }
-
- while(d==4) /*数码管显示数字0-7测试*/
- {
- second1++;
- if(second1==60)
- {
- second1=0;
- minite1++;
- if(minite1==60)
- {
- minite1=0;
- hour1++;
- if(hour1==24)
- {
- hour1=0;
- }
- }
- }
- naozhong();
- //naoxian(); //扫描间隙延时
- d=keyscan(); //调键扫描程序
- }
- while(d==8) /*显示数字钟测试*/
- {
- second1--;
- if(second1<=0)
- {
- second1=59;
- minite1--;
- if(minite1<=0)
- {
- minite1=59;
- hour1--;
- if(hour1<=0)
- {
- hour1=23;
- }
- }
- }
- naozhong();
- //naoxian(); //扫描间隙延时
- d=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
-
- while(nu==2&&v==0)
- {
- while(e==0)
- {
- naozhong();
- e=keyscan();
- }
-
- while(e==4) /*数码管显示数字0-7测试*/
- {
- minite1++;
- if(minite1==60)
- {
- minite1=0;
- hour1++;
- if(hour1==24)
- {
- hour1=0;
- }
- }
- naozhong();
- // naoxian(); //扫描间隙延时
- e=keyscan(); //调键扫描程序
- }
- while(e==8) /*显示数字钟测试*/
- {
- minite1--;
- if(minite1<=0)
- {
- minite1=59;
- hour1--;
- if(hour1<=0)
- {
- hour1=23;
- }
- }
- naozhong();
- //naoxian(); //扫描间隙延时
- e=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
-
- while(nu==3&&v==0)
- {
- while(f==0)
- {
- naozhong();
- f=keyscan();
- }
- while(f==4) /*数码管显示数字0-7测试*/
- {
- hour1++;
- if(hour1==24)
- {
- hour1=0;
- }
-
- naozhong();
- //naoxian(); //扫描间隙延时
- f=keyscan(); //调键扫描程序
- }
- while(f==8) /*显示数字钟测试*/
- {
- hour1--;
- if(hour1<=0)
- {
- hour1=23;
- }
-
- naozhong();
- //naoxian(); //扫描间隙延时
- f=keyscan(); //调键扫描程序
- }
- v=keyscan();
- }
-
- }
-
- break;
-
- case 128:
- P3=0XFF;
- while(v==128||v==0)
- {
- if(n<2) n++;
- else { n=1;}
- v=0;
- while(n==1&&v==0)
- {
- succer();
- v=keyscan();
-
- }
- while(n==2&&v==0)
- {
- hccx();
- if(( disbuf0[0]==disbuf[0])&&(disbuf0[1]==disbuf[1])&&(disbuf0[3]==disbuf[3])&&(disbuf0[4]==disbuf[4])&&(disbuf0[6]==disbuf[6])&&(disbuf0[7]==disbuf[7]))
- P3_1=0;
- naoxian();
- v=keyscan();
- }
-
- }
- break;
- }
- }
- }
- }
-
复制代码
|
-
-
闹钟.zip
48.65 KB, 下载次数: 4, 下载积分: 黑币 -5
|