发个帖求助下,刚入门没多久,动态扫描还是不是很熟,用一个138译码器和一个锁存器控制两位数码管自动计数,中间加了按键启动,按key4后自动加一,但是动态扫描的时候数码管亮度不正常,消隐有问题,大家看看。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
uint num,num0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);//延时子程序
void into(); //初始化子程序
void keyboard();
void display(uint numdis) //显示子程序
{
uchar bai,shi,ge,j;
bai=numdis/100;
shi=numdis%100/10;
ge =numdis%10;
P1=table[bai];
LSA=0;
LSB=1;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1=0x00;
P1=table[shi];
LSA=1;
LSB=0;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1 =0x00;
P1 =table[ge];
LSA=0;
LSB=0;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1=0x00;
}
void main()//主函数
{
into();
while(1)
{
keyboard();
display(num);
}
}
void into() //初始化
{
num=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
void keyboard()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
num++;
if(num==120)
num=0;
while(!key1);
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
if(num==0)
num=120;
num--;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0);
{
num=0;
while(!key3);
}
}
if(key4==0) //这里要调用定时器
{
delay(10);
if(key4==0)
{
while(!key4);
TR0=~TR0;
}
}
}
void delay(uint z) //延时子程序
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1 //定时器
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
num0++;
if(num0==40)
{
num0=0;
num++;
if(num==120)
num=0;
}
}
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
uint num,num0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);//延时子程序
void into(); //初始化子程序
void keyboard();
void display(uint numdis) //显示子程序
{
uchar bai,shi,ge,j;
bai=numdis/100;
shi=numdis%100/10;
ge =numdis%10;
P1=table[bai];
LSA=0;
LSB=1;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1=0x00;
P1=table[shi];
LSA=1;
LSB=0;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1 =0x00;
P1 =table[ge];
LSA=0;
LSB=0;
LSC=0;
j=20;//扫描间隔时间设定
while(j--);
P1=0x00;
}
void main()//主函数
{
into();
while(1)
{
keyboard();
display(num);
}
}
void into() //初始化
{
num=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
void keyboard()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
num++;
if(num==120)
num=0;
while(!key1);
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
if(num==0)
num=120;
num--;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0);
{
num=0;
while(!key3);
}
}
if(key4==0) //这里要调用定时器
{
delay(10);
if(key4==0)
{
while(!key4);
TR0=~TR0;
}
}
}
void delay(uint z) //延时子程序
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1 //定时器
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
num0++;
if(num0==40)
{
num0=0;
num++;
if(num==120)
num=0;
}
}