|
完整代码
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1 = P3^0;
- sbit key2 = P3^1;
- sbit key3 = P3^2;
- sbit smg1=P2^0;
- sbit smg2=P2^1;
- sbit beep=P1^0;
- sbit led=P1^1;
- uchar disp[]={0,0};
- uchar scan;
- uchar i,i1,Second_Counts;
- uchar flag=1;
- uchar DSY_CODE[]=
- {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
- };
- void DelayMS(uint ms)
- {
- uchar t;
- while(ms--)
- {
- for(t=0;t<120;t++);
- }
- }
- void delay_ms(unsigned int ms) // ms延时函数 (AT89C51 @ 11.0592MHz)
- {
- unsigned int i;
- unsigned char j;
- for(i=0;i<ms;i++)
- {
- for(j=0;j<200;j++);
- for(j=0;j<102;j++);
- }
- }
- void Timer0Init()
- {
- TMOD=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
- TH0 = (65535-50000)/256;
- TL0 = (65535-50000)%256;
- ET0=1;//打开定时器0中断允许
- EA=1;//打开总中断
- TR0=0;//关闭定时器
- }
- void Timer0() interrupt 1
- {
- TH0 = (65535-50000)/256;
- TL0 = (65535-50000)%256; //50ms
- if(flag==1)
- {
- if(++i==20)
- {
- i = 0;
- ++Second_Counts;
-
- if(Second_Counts > 99) Second_Counts = 0;
- }
- }
- else if(flag==2)
- {
- i1++;
- if(i1==5)
- {
- led=!led;
- P2=0xff;
- }
- if(i1==10)
- {
- i1=0;
- led=!led;
- P2=0x00;
- }
- }
-
- }
- void scanner(void) //数码管显示函数
- {
- uchar i;
- scan=0x01;
-
- if(flag==2&&i1<5)
- {
- for(i=0;i<2;i++) //显示ADC获取的电压
- {
- P2=scan;
- P0=DSY_CODE[disp[i]];
- delay_ms(5);
- scan<<=1; //数码管移位
- }
- }
- if(flag==1)
- {
- for(i=0;i<2;i++) //显示ADC获取的电压
- {
- P2=scan;
- P0=DSY_CODE[disp[i]];
- delay_ms(5);
- scan<<=1; //数码管移位
- }
- }
- }
- void main()
- {
- i = 0;
- Second_Counts = 0;
- Timer0Init();
- led=0;
- beep=0;
-
- while(1)
- {
- scanner();
-
- if(key1==0)
- {
- delay_ms(10);
- if(key1==0)
- {
- flag=1;
- TR0=1;
- }
- while(!key1);
- }
-
- if(key2==0)
- {
- delay_ms(10);
- if(key2==0)
- {
- flag=2;
- }
- while(!key2);
- }
-
- if(key3==0)
- {
- delay_ms(10);
- if(key3==0)
- {
- Second_Counts=0;
- TR0=0;
- led=0;
- flag=1;
- }
- while(!key3);
- }
-
- disp[0]=Second_Counts%10;
- disp[1]=Second_Counts/10;
-
- if(flag==1)
- {
- if(disp[0]==9&&disp[1]==9)
- {
- TR0=0;
- beep=1;
- led=1;
- }
- else
- {
- beep=0;
- led=0;
- }
- }
- }
- }
复制代码
|
|