51单片机数码管递增显示

2020-12-10 10:03发布

硬件: STC90C51(普中科技51开发板) 连线:74HC138译码管A B C与P22 P23 P24连接,控制数码管的位选;P0与数码管连接,控制段选

  


 代码如下: 

#include <reg51.h>

#include <intrins.h>



 sbit HC138A = P2^2;

 sbit HC138B = P2^3;

 sbit HC138C = P2^4;

 sbit LE = P1^0;


//数码管共阴级编码

unsigned char code table1[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

//数码管共阳级编码

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

//74HC138译码管 P2 端的设置

unsigned char code table3[] = {0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c};

//8个数码管各显示的数字值

unsigned char table4[8] = {0};


void delay(unsigned int num)

{

    while(num--);

}


void shuMaGuanShow(unsigned long num)

{

    table4[7] = num/10000000;

    table4[6] = num%10000000/100000;

    table4[5] = num%1000000/100000;

    table4[4] = num%100000/10000;

    

    table4[3] = num%10000/1000;

    table4[2] = num%1000/100;

    table4[1] = num%100/10;

    table4[0] = num%10;

}


void showNum(unsigned long num)

{

     int j;

     int i;

     shuMaGuanShow(num);

     for(i=8;i>0;i--)

     {

         for(j=0;j<8;j++)

         {    

                    LE = 0;

                    P0 = table2[table4[j]];

                    LE = 1;

                    P2 = table3[7-j];

                    delay(40);

         }

         LE = 0;

     }

}


// 数码管显示

int main()

{

     long num = 0;

     while(1)

     {

         showNum(num);

         num++;

     }

}