|
為啥數(shù)碼管不能正確顯示啊(代碼借鑒了論壇里的)
9F2F1D058C458BE42DA9236ED9BCCDEE.png (116.35 KB, 下載次數(shù): 0)
下載附件
2024-12-11 20:59 上傳
#include <REGX51.H>
sbit RED_A=P0^0;
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3;
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
unsigned char seg[]={0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90};
unsigned char wei[]={0xFE,0xFD};
unsigned int count0=0,timeleft=5,Time_Count=0,
Flash_Count=0,Operation_Type=1;
void inittimer()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
{
for(j=0;j<120;j++);
}
}
void display(unsigned int time, unsigned char port)
{
unsigned char tens=time/10;
unsigned char units=time%10;
P3 = wei[0];
if (port==1)
{
P1=seg[tens];
}
else
{
P2=seg[tens];
}
delay(1);
P3 = wei[1];
if (port==1)
{
P1 = seg[units];
}
else
{
P2 = seg[units];
}
delay(1);
}
void main()
{
inittimer();
while(1){};
}
void T0_INT() interrupt 1
{
TL0=(65536-50000)%256;
TH0=(65536-50000)/256;
count0++;
if(count0>=20)
{
if (timeleft>0)
{
timeleft--;
}
count0=0;
}
switch(Operation_Type)
{
case 1:
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
if(++Time_Count!=100)
return;
display(timeleft,2);
Time_Count=0;
Operation_Type=2;
break;
case 2:
if(++Time_Count!=10)
return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10)
return;
display(timeleft,2);
Flash_Count=0;
Operation_Type=3;
break;
case 3:
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
if(++Time_Count!=100)
return;
display(timeleft,1);
Time_Count=0;
Operation_Type=4;
break;
case 4:
if(++Time_Count!=10)
return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_B=1;
if(++Flash_Count!=10)
return;
display(timeleft,1);
Flash_Count=0;
Operation_Type=1;
break;
}
}
|
|