利用锁存器控制9个数码管问题
共有3个锁存器,其中的一个用作段选,另外两个用作位选(因为共有9个数码管,而一个锁存器只有8个口,因此只能用两个),在实际中会出现重影现象,不知大家有没有碰到过。附上代码:#include<reg51rb2.h>
#include<DINGYI.h>
#define HIGH 195
#define LOW 165
sbit dula=P0^4;
sbit welaA=P0^2;
sbit welaBC=P0^1;
sbit PLED1=P1^0;
sbit FLED1=P1^1;
sbit PLED2=P1^2;
sbit FLED2=P1^3;
sbit PLED3=P1^4;
sbit FLED3=P1^5;
sbit CheckA=P3^2;
sbit CheckB=P3^3;
sbit CheckC=P3^4;
sbit BEEP=P3^7;
uint Time0=0,Time1=0,Time2=0;
uchar flag1=0,flag2=0,flag3=0;
uchar bai,shi,ge,bai1,shi1,ge1,bai2,shi2,ge2,bai3,shi3,ge3,num;
uchar num1=0,num2=0,num3=0;
uchar code table1[]={ //共阴极
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table[]={ //共阳极
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
// 蜂鸣器控制
void nbeep(uchar count)
{
uchar i;
for(i=0; i<count; i++)
{
BEEP = 1; delayms(50);
BEEP = 0; delayms(50);
}
}
void passbeep()
{
nbeep(3);
}
void failbeep()
{
BEEP=1;
delayms(200);
BEEP=0;
}
void display1(uchar bai,uchar shi,uchar ge)
{
dula=1;
P2=table[bai];
dula=0;
P2=0xff;
welaA=1;
P2=0x04;
welaA=0;
delayms(2);
dula=1;
P2=table[shi];
dula=0;
P2=0xff;
welaA=1;
P2=0x02;
welaA=0;
delayms(2);
dula=1;
P2=table[ge];
dula=0;
P2=0xff;
welaA=1;
P2=0x01;
welaA=0;
delayms(2);
}
void display2(uchar bai,uchar shi,uchar ge)
{
dula=1;
P2=table[bai];
dula=0;
P2=0xff;
welaBC=1;
P2=0x08;
welaBC=0;
delayms(2);
dula=1;
P2=table[shi];
dula=0;
P2=0xff;
welaBC=1;
P2=0x10;
welaBC=0;
delayms(2);
dula=1;
P2=table[ge];
dula=0;
P2=0xff;
welaBC=1;
P2=0x20;
welaBC=0;
delayms(2);
}
void display3(uchar bai,uchar shi,uchar ge)
{
dula=1;
P2=table[bai];
dula=0;
P2=0xff;
welaBC=1;
P2=0x04;
welaBC=0;
delayms(2);
dula=1;
P2=table[shi];
dula=0;
P2=0xff;
welaBC=1;
P2=0x02;
welaBC=0;
delayms(2);
dula=1;
P2=table[ge];
dula=0;
P2=0xff;
welaBC=1;
P2=0x01;
welaBC=0;
delayms(2);
}
void main()
{
P1=0x00;
P3=0xff;
P2=0xFF;
P0=0x00;
TMOD=0x11;
T2MOD=0x02;
TH0=0xD8;
TL0=0xF0;
TH1=0xD8;
TL1=0xF0;
TH2=0xD8;
TL2=0xF0;
EA=1;
ET0=1;
ET1=1;
ET2=1;
TR0=0;
TR1=0;
TR2=0;
while(1)
{
WDTRST=0x1E;
WDTRST=0xE1;
if(flag1==0)
{
if(CheckA==0)
{
flag1=1;
TR0=1;
Time0=0;
FLED1=0;
PLED1=0;
}
}
if(flag1==1)
{
if(CheckA==1||Time0>HIGH)
{
flag1=0;
TR0=0;
if(Time0>LOW&&Time0<HIGH)
{
PLED1=1;
passbeep();
}
else
{
FLED1=1;
failbeep();
}
}
}
if(flag2==0)
{
if(CheckB==0)
{
flag2=1;
TR1=1;
Time1=0;
PLED2=0;
FLED2=0;
}
}
if(flag2==1)
{
if(CheckB==1||Time1>HIGH)
{
flag2=0;
TR1=0;
if(Time1>LOW&&Time1<HIGH)
{
PLED2=1;
passbeep();
}
else
{
FLED2=1;
failbeep();
}
}
}
if(flag3==0)
{
if(CheckC==0)
{
flag3=1;
TR2=1;
Time2=0;
PLED3=0;
FLED3=0;
}
}
if(flag3==1)
{
if(CheckC==1||Time2>HIGH)
{
flag3=0;
TR1=0;
if(Time1>LOW&&Time2<HIGH)
{
PLED3=1;
passbeep();
}
else
{
FLED3=1;
failbeep();
}
}
}
display1(bai1,shi1,ge1);
display2(bai2,shi2,ge2);
display3(bai3,shi3,ge3);
}
}
void T0_Time() interrupt 1
{
TH0=0xD8;
TL0=0xF0;
num1++;
if(num1==100)
{
Time0++;
num1=0;
}
bai1=Time0/100;
shi1=Time0%100/10;
ge1=Time0%10;
}
void T1_Time() interrupt 3
{
TH1=0xD8;
TL1=0xF0;
num2++;
if(num2==100)
{
Time1++;
num2=0;
}
bai2=Time1/100;
shi2=Time1%100/10;
ge2=Time1%10;
}
void T2_Time() interrupt 5
{
TH2=0xD8;
TL2=0xF0;
num3++;
if(num3==100)
{
Time2++;
num3=0;
}
bai3=Time2/100;
shi3=Time2%100/10;
ge3=Time2%10;
}
[此贴子已经被作者于2016-5-28 08:01编辑过]