51 microcontroller: digital tube display

1. Static digital tube display

        To light up the digital tube, you need to use the P0 port and the P2 partial port. The P0 port is responsible for displaying the number to be output, and the P2 partial port is responsible for which digital tube is lit. For details, see the circuit diagram. This time it also includes 74HC245 and 74HC138 chips.

        Now let the third digital tube in the digital tube area of ​​the microcontroller display the number 5. The program is as follows

 #include"reg52.h"

//Define P2 port
sbit LSA=P2^2; 
sbit LSB=P2^3; 
sbit LSC=P2^4;

//Use an array to represent the relevant level input corresponding to each digital output on the digital tube
int str[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f};


void Display(int Location,int Number)
{     //Use the case statement to list the positions displayed by the 8 digital tubes one by one     switch(Location)        {         case 1: LSC=0;LSB=0;LSA=0; break;         case 2 : LSC=0;LSB=0;LSA=1; break;         case 3: LSC=0;LSB=1;LSA=0; break;         case 4: LSC=0;LSB=1;LSA=1; break;         case 5: LSC=1;LSB=0;LSA=0; break;         case 6: LSC=1;LSB=0;LSA=1; break;         case 7: LSC=1;LSB=1;LSA=0; break;         case 8: LSC=1;LSB=1;LSA=1; break;     }











    P0=str[Number];
}

int main()
{     Display(3,5); //Parameter 1 represents the position of the digital tube, parameter 2 represents the number 0-9 to be output     while(1)     {     } }




The experimental phenomena are as follows

2.Dynamic digital tube display

        This part mainly pays attention to the problem of delay and elimination of afterimages. If no delay is added, the two numbers switch very quickly, which may lead to bit tampering. The procedure is as follows

 #include"reg52.h"

//Define P2 port
sbit LSA=P2^2; 
sbit LSB=P2^3; 
sbit LSC=P2^4;

//Use an array to represent the relevant level input corresponding to each digital output on the digital tube
int str[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f};

void Delay(int time)
{     while(time--) //The while loop takes 10us     {     } }



void Display(int Location,int Number)
{     //Use the case statement to list the positions displayed by the 8 digital tubes one by one     switch(Location)        {         case 1: LSC=0;LSB=0;LSA=0; break;         case 2 : LSC=0;LSB=0;LSA=1; break;         case 3: LSC=0;LSB=1;LSA=0; break;         case 4: LSC=0;LSB=1;LSA=1; break;         case 5: LSC=1;LSB=0;LSA=0; break;         case 6: LSC=1;LSB=0;LSA=1; break;         case 7: LSC=1;LSB=1;LSA=0; break;         case 8: LSC=1;LSB=1;LSA=1; break;     }











    P0=str[Number];

    //Delay for a period of time, waiting for the display to stabilize. If no delay is added, the previous number and the next number may be usurped.
    Delay(100);
    //Eliminate the afterimage of the digital tube
    P0=0x00;
}

int main()
{      //Parameter 1 represents the position of the digital tube, parameter 2 represents the number 0-9 to be output     while(1)     {         Display(1,1);         Display(2,2);         Display(3,3) ;





    }
}

The experimental phenomena are as follows

 

Guess you like

Origin blog.csdn.net/weixin_52300845/article/details/124322380