|

- //計時程序,要求有兩個定時器,要有聲音(蜂鳴器就行),顯示(數碼管)
- //2016 04 29 E:\DPJ_C\OTHER\30S倒計時\30s.c
- #include <reg52.h>
- #define uchar unsigned char
- sbit BZ = P1^2;
- sbit P20=P2^0;
- sbit P21=P2^1;
- uchar a=30,b,c,time,flag;
- uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay_ms(unsigned int ms) //1ms延時
- {
- while(ms--)
- for(b=123;b>0;b--);
- }
- void disp();
- void t0intinit()
- {
- TMOD=0x11;
- TH0=0x3c;
- TL0=0xb0;
- TH1=0xfe;
- TL1=0x0c;
- EA=1;
- ET0=1;
- ET1=1;
- TR0=1;
- }
- void main(void)
- {
- t0intinit();
- while(1)
- {
- disp();
- while(flag)
- {
- disp();
- TR1=1;
- if(c==3) //蜂鳴3s后停止
- {
- flag=0;
- TR0=0;
- TR1=0;
- }
- }
- }
- }
- void disp( )
- {
- P0=tab[a%10];
- P21=0;
- delay_ms(1);
- P21=1;
- P0=tab[a/10];
- P20=0;
- delay_ms(1);
- P20=1;
- }
- void t0int(void) interrupt 1
- {
- TH0=0x3c;
- TL0=0xb0;
- time++;
- if(time==20)
- {
- time=0;
- if(a!=0)
- a--;
- else
- {
- flag=1;
- c++;
- }
- }
- }
- void t1int(void) interrupt 3 //定時器T1作1kz蜂鳴
- {
- TH1=0xfe; //0.5ms定時
- TL1=0x0c;
- if(flag==1)
- BZ=~BZ;
- }
復制代碼
|
|