找不出原因,請求教。
代碼如下:
- #include <reg52.h>
- #define DIG_PORT P0
- #define unchar unsigned char
- #define uint unsignde int
- sbit gA = P2^2; // 38譯碼器輸入口A
- sbit gB = P2^3; // 38譯碼器輸入口B
- sbit gC = P2^4; // 38譯碼器輸入口C
- int t,seccnt,msta,tzsta;
- int m=0,n,t;
- char shi,ge,s=30;
- unsigned char code gDuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f};
- void ini()//定時器
- {
- seccnt=0;
- msta=tzsta=0;
- TMOD=0x01;
- TH0=0xFF;
- TL0=0x9C;
- TR0=1; //開啟定時器0
- }
- void delay(int p)
- {
- while(p--);
- }
- void DigDisplayTest(t,n)
- {
- switch (t) //位選,選擇點亮哪個數碼管
- {
- case 0:
- gA = 1; gB = 1; gC = 1;
- break; //顯示第0位
- case 1:
- gA = 0; gB = 1; gC = 1;
- break; //顯示第1位
- case 2:
- gA = 1; gB = 0; gC = 1;
- break; //顯示第2位
- case 3:
- gA = 0; gB = 0; gC = 1;
- break; //顯示第3位
- case 4:
- gA = 1; gB = 1; gC = 0;
- break; //顯示第4位
- case 5:
- gA = 0; gB = 1; gC = 0;
- break; //顯示第5位
- case 6:
- gA = 1; gB = 0; gC = 0;
- break; //顯示第6位
- case 7:
- gA = 0; gB = 0; gC = 0;
- break; //顯示第7位
- default:
- break;
- }
- DIG_PORT = gDuanMa[n]; // 發送段碼
- delay(1000); // 間隔一段顯示時間
- DIG_PORT = 0x00; // 消隱,注意不要漏掉了
- }
- void work0()
- {
- shi=s/10;
- ge=s%10;
- if(m==10000)
- {
- s--;
- m=0;
- }
- DigDisplayTest(6,shi);
- DigDisplayTest(7,ge);
- }
- void main()
- {
- ini();//初始化
- while(1)//閉環
- {
- while(TF0==0);//啟動主循環
- TL0=0x9C;
- TH0=0xFF;
- TF0=0;
- m++;
- work0();
- }
復制代碼
|