- /*
- *平臺:Keil U4+STC89C52
- *名稱:/*
- *平臺:Keil U4+STC89C52
- *名稱:P1.0輸出1HZ方波,送入定時器T0,計的數通過數碼管的方式顯示出來,
- 最多顯示到99,超過以后重新回到0。
- *日期:2017.11.19*/
- #include<reg52.h>
- #define WEI0 P1=0xfd; //定義位選引腳
- #define WEI1 P1=0xfb;
- int m=1;
- unsigned int count;//用來計數
- //unsigned int i,j;
- //定義一個數組,將0~9對應的編碼存入數組
- unsigned char n[] = {0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90};
- sbit LED=P1^0;
- void timer0();//函數聲明
- /*定時器T0,工作方式2,200微秒發起一次中斷*/
- void timer0()
- {
- TMOD=0x02;
- TH0=0x48;//初始化TH0和TL0
- TL0=0x48; //每隔200微秒發起一次中斷
- ET0=1;//開啟T0中斷
- EA=1;//開啟總中斷
- TR0=1;//打開T0定時器
- count=0;//count初始值為零
- }
- void SMG_display(int a,int b);//聲明顯示函數
- void SMG_display(int a,int b)
- {
- switch(m)
- {
- case 0x01: P0=0xff;WEI1;P0=n[a];m++; break;
- case 0x02: P0=0xff;WEI0;P0=n[ b];m=1; break;
- default: break;
- }
- }
- void main()
- {
- int a,b;
- LED=1; //關閉LED
- timer0();//
- while(1)
- {
- for(b=0;b<10;b++)
- {
- for(a=0;a<10;)
- {
- SMG_display(a,b);
- if(count<=2500)
- {
- LED=0;
- }
- if(count>2500&&count<=4999)
- {
- LED=1;
- }
- if(count==5000)
- {
- LED=0;
- count=0;
- a++;
- }
- }
- }
- P0=1; //關閉數碼管
- }
- }
- void timer() interrupt 1
- {
- count++;//每次中斷count+1
- }
復制代碼
|