51单片机数码管 知识点操作总结

数码管有共阴极和共阳极
显示成数字样子的叫 段码

unsigned char code seg_cc[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极段码
unsigned char code seg_ca[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//共阳极段码

有单个数码管
也有2级联,4级联,8级联的数码管,就要用到位码

unsigned char code bit_seg_cc[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//共阴极,供电端给低电平
unsigned char code bit_seg_ca[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//共阳极,供电端给高电平


//亮一个共阳极的,0~9
void main()
{
    unsigned char i = 0;
    P0 = 0x00;
    while(1)
    {
        for(i = 0; i < 10 ; i++)
        {
            P0 = seg_ca[i];
            DelayMs(100);
        }
    }*/
}

/**************************************************
共阴极的,0~99自增,我用了74HC573锁存芯片写了一个display函数
74HC573:c为1时不锁存,c为0时锁存当前P口的值。
*************************************************/
void display(unsigned char FirstBit,unsigned char Lenth)
{
    unsigned char a;
    for(a = FirstBit ; a < FirstBit+Lenth ; a++)
    {
            DatePort = 0;
            Seg_Latch = 1; //开门
            Seg_Latch = 0; //关门 

            DatePort = bit_seg_cc[a];
            Bit_Latch = 1; //开门
            Bit_Latch = 0; //关门

            DatePort = seg_cc[display_Num[a]];
            Seg_Latch = 1; //开门
            Seg_Latch = 0; //关门

            DelayUs2x(100);
    }
}
int main()
{
    unsigned char x = 0; //个位
    unsigned char y = 0; //十位
    unsigned int Num = 0;
    P0 = 0x00;
    while(1)
    {
        x++;
        if(x == 10)//累加,往下运行,就是刷新,不会卡住
        {   
            y++;
            x = 0;
        }
        if(y == 50)
        {
            y = 0;
            Num++;
        }
//  DelayMs(100);//延时,不往下运行,不是我想要的
        if(Num == 99)
            Num = 0;
        display_Num[0] = Num/10;
        display_Num[1] = Num%10;
        display(0,2);
    }
}

/***************************************************************
只要改#define MAXNUM 就能实现任意数的累加
***************************************************************/    
#define MAXNUM 999
int main()
{
    unsigned char i = 0;
    unsigned char j = 0;
    unsigned char x = 0;
    unsigned int temp = 0;
    unsigned int Num = 0;
    unsigned int a = 0;
    P0 = 0x00;
    temp = MAXCOUNT;
    while(temp /= 10)
        i++;
    i++;
    j = i;
    while(1)
    {
        x++;
        if(x == 200)//累加,往下运行,就是刷新,不会卡住
        {   
            Num++;
            x = 0;
            if(Num == MAXCOUNT+1)
            {
                Num = 0;
            }
        }
//  DelayMs(100);//延时,不往下运行,不是我想要的
        a = Num;
        i = j;
        while(i != -1)
        { 
            display_Num[--i] = a%10;//bit0为个位
            a/=10;
        }
        display(0,j);
    }
}
/******************************************************
滚动数码管,显示电话号码
********************************************************/
unsigned char code my_tele_seg_code[11] = {};//存自己电话
void display_mytele(unsigned char FirstBit,unsigned char Lenth,unsigned char seg_code_first)
{
    unsigned char a = 0;
    unsigned char z = seg_code_first;
    for(a = FirstBit ; a < FirstBit+Lenth ; a++)
    {
        DatePort = 0;
        Seg_Latch = 1; //开门
        Seg_Latch = 0; //关门 

        DatePort = bit_seg_cc[a];
        Bit_Latch = 1; //开门
        Bit_Latch = 0; //关门

        DatePort = my_tele_seg_code[z++];
        Seg_Latch = 1; //开门
        Seg_Latch = 0; //关门
        DelayUs2x(100);
    }
}
void main()
{
    unsigned char i = 0;
    unsigned char x = 0;
    while(1)
    {
        x++;
        if(x == 100)//累加,往下运行,就是刷新,不会卡住
        {   
            i++;
            x = 0;
        }
        if(i <= 7)
        {
            DelayUs2x(200);
            display_mytele(7-i,i+1,0);
        }
        else if(i>7 && i<=10)
        {
            display_mytele(0,8,i-7);
        }
        else if(i>10 && i <= 18)
        {
            DelayUs2x(200);
            display_mytele(0,18-i,i-7);
        }
        else 
        {
            i = 0;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zzl_godstyle/article/details/81168560