新手入门
- 积分
- 3
- 金钱
- 3
- 注册时间
- 2020-6-10
- 在线时间
- 0 小时
|
1金钱
#include <reg52.h>
#define Dataport P0 //宏定义
sbit Duan = P2^2; //段锁存
sbit Wei = P2^3; //位锁存
unsigned char code DuanMa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char huancun[8];
void display(unsigned char fistbit,unsigned char num);
void delay(unsigned int t)
{
while(--t);
}
void main()
{
unsigned char j;
signed long num=99999999;
while(1)
{
j++;
if(j==10) //扫描所有的数码管算一次j,10次j数值加1
{
j=0;
num--;
if(num == -1) //87654321
num = 99999999;
}
if(num<10000000) //值小于10000000(千万级),则千万位不显示
huancun[0] = 0; //消隐
else
huancun[0] = DuanMa[num/10000000];
if(num<1000000)
huancun[1] = 0;
else
huancun[1] = DuanMa[num/1000000%10];
if(num<100000)
huancun[2] = 0;
else
huancun[2] = DuanMa[num/100000%10];
if(num<10000)
huancun[3] = 0;
else
huancun[3] = DuanMa[num/10000%10];
if(num<1000)
huancun[4] = 0;
else
huancun[4] = DuanMa[num/1000%10];
if(num<100)
huancun[5] = 0;
else
huancun[5] = DuanMa[num/100%10];
if(num<10)
huancun[6] = 0;
else
huancun[6] = DuanMa[num/10%10];
huancun[7] = DuanMa[num%10];
display(0,8);
}
}
void display(unsigned char firstbit,unsigned char num) //fisbirt表示从第firstbit个数码管开始显示,num表示显示num个数码管.二者为形参
{
unsigned char i;
for(i=0;i<num;i++) //一次扫描num个数码管
{
Dataport = 0; //每次进入时消除前一次段锁存的数据,消除重影
Duan = 1;
Duan = 0;
Dataport = WeiMa[i+firstbit];
Wei = 1;
Wei = 0;
Dataport = huancun[i];
Duan = 1; //锁存透明,输入与输出直通
Duan = 0; //锁存关闭,保留数据
delay(200);
上面就是程序,显示的时候第八个数码管亮度正常,前七个数码管都比较暗;我怀疑时扫描时间太快了,所以我加长了时间,但是虽然亮度强一点了,同时也出现抖动,不知道怎么弄。想问一下大家亮度暗时由什么造成的呢?还有就是按键扫描和数值累加有没有什么冲突的地方?
|
|