单片机数码管显示-消影问题

看郭天祥的单片机书中数码管部分,使用 P0=0xff 的方法可以消除段选数据对位选的干扰,另外使用我下面的方法可以达到同样的效果。但即便这样数码管仍有影子,这种状况是,当前数码管有下一个数码管的影子,或者当前数码管有前一个数码管的影子。

分析一下产生原因,当我们点亮一个数码管后,总要先将段选或者位选改为下一个数码管的值,而不能同时改变。当先改变段选时,那么当前数码管就会有短暂的时间显示下一个数码管的数字。当先改变位选时,下一个数码管就会有短暂的时间显示当前数码管的数字。

那么解决方法是,先将段选置0送入锁存器,改变位选的值为下一个数码管,最后再改变段选值。同理另一种方法是,先将位选赋值 0xff 即关闭所有数码管,改变段选值,最后改变位选值。

另外一种勉强可以的方法是,增加延时时间,这样正常数字显示时间就相对影子数字的短暂时间变长,看起来影子就变得暗了,但延时太长导致数码管闪烁,所以这种方法不建议,郭天祥书中的示例程序使用这种方法。

只贴出前2种方法的代码。

#include <reg52.h>
#include <intrins.h>

sbit we=P2^7;
sbit du=P2^6;

unsigned char code table[] = {
0x3f , 0x06 , 0x5b , 0x4f,
0x66 , 0x6d , 0x7d , 0x07,
0x7f , 0x6f , 0x77 , 0x7c,
0x39 , 0x5e , 0x79 , 0x71,
0x00};

void show();
void delayms(unsigned time);

unsigned char num[6]={0,0,14,13,0,12};
unsigned char count=0;
unsigned char nums=0;
unsigned char numm=0;

void main()
{
    EA=1;
    ET0=1;
    TMOD=0x01;
    TH0=(65536-45872)/256;
    TL0=(65536-45872)%256;
    TR0=1;
    show();
}

void t0_time() interrupt 1
{
    TH0=(65536-45872)/256;
    TL0=(65536-45872)%256;
    count++;

    if(count%20==0)
    {
        count=0;
        nums++;
        if(nums==60)
        {
            nums=0;
            numm++;
            num[2]=numm%10;
            num[3]=numm/10;
        }
        num[0]=nums%10;
        num[1]=nums/10;
    }
}

void show()
{
    unsigned char i;
    
    while(1)
        for(i=0;i<6;i++)
        {
            P0=0;
            du=1;
            du=0;

            P0=~(0x20>>i);
            we=1;
            we=0;

            P0=table[num[i]];
            du=1;
            du=0;

            delayms(1);
        }
}

void delayms(unsigned time)
{
    unsigned i,j;

    for(i=time;i>0;i--)
        for(j=110;j>0;j--)
        ;
}
interrupt.c

 第二种

void show()
{
    unsigned char i;
    
    while(1)
        for(i=0;i<6;i++)
        {
            P0=0xff;
            we=1;
            we=0;      

            P0=table[num[i]];
            du=1;
            du=0;

            P0=~(0x20>>i);
            we=1;
            we=0;

            delayms(1);
        }
}
interrupt.c

参考 http://bbs.21ic.com/icview-413371-1-1.html

原文地址:https://www.cnblogs.com/zackcoder/p/3521255.html