|
这个程序数码管为啥不能显示啊 有没有大神救救我
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
//#define key_S 300
//#define key_L 6000
//#define key_M 3000
#define key_S 100
#define key_L 2000
#define key_M 1000
sbit K1 = P3^2;
sbit K2 = P3^3;
sbit K3 = P3^4;
sbit K4 = P3^5;
sbit wela1=P1^0;
sbit wela2=P1^1;
sbit wela3=P1^2;
sbit wela4=P1^3;
sbit Buzzer=P3^7;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0xbf,0x7f};
uint Cnt200us;
uchar hour=12,min=0,sec=0;
uchar hour4=0,min4=0;
uchar Choice=0,Choice4=0;
bit Buzzer_sign;
bit Twinkle,second=1;
void Timer_Init(void)
{
TMOD= 0x02;
TL0 = 0x38;
TH0 = 0x38;
TR0 = 1;
EA=1;
ET0=1;
}
void Timer_Init()
{
TMOD = 0x02;
TL0 = 0x48;
TH0 = 0x48;
TR0 = 1;
EA=1;
ET0=1;
}
void key_scan()
{
static bit key1_sign,key4_sign;
static uint count1,count2,count3=0,count4=0;
if(!K1)
{
count1++;
if((count1>=key_S)&&(key1_sign==0))
{
key1_sign=1;
Choice++;
if(Choice>=3)
{
Choice=0;
Cnt200us=0;
sec=0;
}
}
}
else
{
key1_sign=0;
count1=0;
}
if(!K2)
{
count2++;
if(count2>=key_L)
{
if(Choice==1)
{
hour++;
if(hour>=24)
hour=0;
}
if(Choice==2)
{
min++;
if(min>=60)
min=0;
}
if(Choice4==1)
{
hour4++;
if(hour4>=24)
hour4=0;
}
if(Choice4==2)
{
min4++;
if(min4>=60)
min4=0;
}
count2=key_M;
}
}
else
{
if(count2>key_S && count2<key_L)//??
{
if(Choice==1)
{
hour++;
if(hour>=24)
hour=0;
}
if(Choice==2)
{
min++;
if(min>=60)
min=0;
}
if(Choice4==1)
{
hour4++;
if(hour4>=24)
hour4=0;
}
if(Choice4==2)
{
min4++;
if(min4>=60)
min4=0;
}
}
count2=0;
}
if(!K3)
{
count3++;
if(count3>=key_L)
{
if(Choice==1)
{
hour--;
if(hour>=24)
hour=23;
}
if(Choice==2)
{
min--;
if(min>=60)
min=59;
}
if(Choice4==1)
{
hour4--;
if(hour4>=24)
hour4=23;
}
if(Choice4==2)
{
min4--;
if(min4>=60)
min4=59;
}
count3=key_M;
}
}
else
{
if(count3>key_S && count3<key_L)//??
{
if(Choice==1)
{
hour--;
if(hour>=24)
hour=23;
}
if(Choice==2)
{
min--;
if(min>=60)
min=59;
}
if(Choice4==1)
{
hour4--;
if(hour4>=24)
hour4=23;
}
if(Choice4==2)
{
min4--;
if(min4>=60)
min4=59;
}
}
count3=0;
}
if(!K4)
{
count4++;
if((count4>=key_S)&&(key4_sign==0))
{
key4_sign=1;
Choice4++;
if(Choice4>=4)
{
Choice4=0;
}
}
}
else
{
key4_sign=0;
count4=0;
}
}
void display()
{
static uchar num=0;
if((Choice4>0)&&(Choice4<3))
{
P0=0xff; //??
switch(num)
{
case 0:
P2=0x01;
P2=0xfe;
P0=table[hour4/10];
num++;
break;
case 1:
P2=0x02;
P2=0xfd;
if((Twinkle==1)&&(Choice4==1))
P0=table[hour4%10];
else
P0=table[hour4%10]&0x7f;
num++;
break;
case 2:
P2=0x04;
P2=0xfb;
P0=table[min4/10];
num++;
break;
case 3:
P2=0x08;
P2=0xf7;
if((Twinkle==1)&&(Choice4==2))
P0=table[min4%10];
else
P0=table[min4%10]&0x7f;
num=0;
break;
}
}
else
{
P0=0xff;
switch(num)
{
case 0:
P2=0x01;
P2=0xfe;
P0=table[hour/10];
num++;
break;
case 1:
P2=0x02;
P2=0xfd;
if((Twinkle==1)&&(Choice==1))
P0=table[hour%10];
else
P0=table[hour%10]&0x7f;
num++;
break;
case 2:
P2=0x04;
P2=0xfb;
P0=table[min/10];
num++;
break;
case 3:
P2=0x08;
P2=0xf7;
if(Choice4==3)
P0=table[min%10]&0x7f;
else if((Twinkle==1)&&(Choice==2))
P0=table[min%10]&0x7f;
else if(Choice==1)
P0=table[min%10]&0x7f;
else
P0=table[min%10];
num=0;
break;
}
}
}
void Buzzer_nz()
{
if((Choice4==3)&&(hour4==hour)&&(min4==min)&&(sec<30))
Buzzer_sign=1;
else Buzzer_sign=0;
if((second==1)&&(Buzzer_sign==1))//????1s
Buzzer=0;
else Buzzer=1;
}
void main(void)
{
Timer_Init();
while(1)
{
key_scan();
display();
Buzzer_nz();
}
}
void timer0() interrupt 1
{
Cnt200us++;
if((Cnt200us>1250 && Cnt200us<2500)||(Cnt200us>3750 && Cnt200us<5000))
Twinkle=1;
else Twinkle=0;
if(Cnt200us>=5000)
{
Cnt200us=0;
second=~second;
if(Choice==0)
{
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)
hour=0;
}
}
}
}
}
|
-
捕获.PNG
(331.26 KB, 下载次数: 25)
原理图
|