楼主这个程序构架不合理,改一下试试。
- #include "reg51.h"
- #include <intrins.h>
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit beep=P1^5;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- u8 val = 30;
- u8 ershi= 20;
- u8 shiwu= 15;
- bit flag=0;
- u8 code smgduan[18]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x38};
- u8 data dis_buf[8]={0,0,0x40,0,0,0x40,0,0};
- void delay(u16 i)
- {
- while(i--);
- }
- void Timer0Init()
- {
- TMOD|=0X01;
- TH0=0XFC;
- TL0=0X18;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void display()
- {
- static int i;
- P0=0x00;
- switch(i)
- {
- case(0):LSA=0;LSB=0;LSC=0;break;
- case(1):LSA=1;LSB=0;LSC=0;break;
- case(2):LSA=0;LSB=1;LSC=0;break;
- case(3):LSA=1;LSB=1;LSC=0;break;
- case(4):LSA=0;LSB=0;LSC=1;break;
- case(5):LSA=1;LSB=0;LSC=1;break;
- case(6):LSA=0;LSB=1;LSC=1;break;
- case(7):LSA=1;LSB=1;LSC=1;break;
- }
- P0=dis_buf[i];
- i++;
- i%=8;
- }
- void main()
- {
- Timer0Init();
- while(1)
- {
- dis_buf[7]=smgduan[val/10];
- dis_buf[6]=smgduan[val%10];
- dis_buf[4]=smgduan[ershi/10];
- dis_buf[3]=smgduan[ershi%10];
- dis_buf[1]=smgduan[shiwu/10];
- dis_buf[0]=smgduan[shiwu%10];
- if(flag==1)
- {
- flag=0;
- beep=0;
- delay(50000);
- beep=1;
- }
- }
- }
- void Timer0() interrupt 1
- {
- static int i;
- TH0=0XFC;
- TL0=0X18;
- i++;
- if(i>=1000)
- {
- i=0;
- if(val>0)
- {
- val--;
- if(val==0)
- flag=1;
- }
- if(ershi>0)
- {
- ershi--;
- if(ershi==0)
- flag=1;
- }
- if(shiwu>0)
- {
- shiwu--;
- if(shiwu==0)
- flag=1;
- }
- }
- display();
- }
复制代码 |