#include <REGX52.H>
#include <INTRINS.H>
unsigned int a,b,i,j;
sbit BEEP=P2^5;
unsigned char s[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay_ms(unsigned int n){
unsigned int a,b;
for(a = n; a > 0; a--)
for(b = 114; b > 0; b--);
}
void show_s(a){
for(i=0;i<6;i++){
for(j=0;j<9;j++){
P2_4=0;P2_3=0;P2_2=1;
P0=s[j];
delay_ms(1);
}
P2_4=0;P2_3=0;P2_2=0;
P0=s[i];
delay_ms(1);
P0=0x00;
}
}
void show_f(b){
for(i=0;i<=1;i++){
P2_4=0;P2_3=1;P2_2=0;
P0=s[i];
delay_ms(1);
if(i==1){
BEEP=!BEEP;
break;
}
}
P2_4=0;P2_3=1;P2_2=1;
P0=s[0];
delay_ms(1);
P0=0x00;
}
void Timer1Init(unsigned char us){
EA=1;
ET1=1;
TMOD = 0x01;
//TL0低8位,TH0高8位
TL1=(64535-us)%256;
TH1=(64535-us)/256;
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
//中断配置
PT1=0;
}
void main()
{
a=0;
b=0;
Timer1Init(50000);
while(1)
{
show_s(a);
show_f(b);
}
}
void Timer1_Routine() interrupt 1{
static unsigned int s = 0;
TL1=(64535-50000)%256;
TH1=(64535-50000)/256;
s++;
if(s==50000){
s=0;
for(b=0;b<=1;b++){
for(a=0;a<60;a++){
}
a=0;
if(b==1&&a==30){
BEEP=!BEEP;
delay_ms(1);
break;
}
}
}
}
想要实现的功能:用动态方式和定时器1,在数码管的后两位显示出秒表,前两位显示分表,设定时间在数码管显示为0130即累计的时间为1分30秒时蜂鸣器叫。
问题:51单片机数码管一直在乱闪,没有实现计时功能