The STC89C52 microcontroller lights up two digital tubes and can display a total of 11 numbers from 00 to 10.

Publisher:TechGuru123Latest update time:2022-10-19 Source: csdnKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Manual display

#include //Includes 51 header file

#include //Includes the shift standard library function header file


#define uint unsigned int

#define uchar unsigned char


sbit DU = P2^6;//digital tube segment selection

sbit WE = P2^7;//Nigital tube segment selection


//Common cathode digital tube segment selection table 0-9

uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};


/*====================================

Function : delay(uint z)

Parameter : z delay millisecond setting, value range 0-65535

Return value : None

Description : 12T/Fosc11.0592M millisecond delay

====================================*/

void delay(uint z)

{

uint x,y;

for(x = z; x > 0; x--)

for(y = 114; y > 0; y--);


/*====================================

Description : Two common cathode digital tube dynamic display

====================================*/

void display(uchar num1, uchar num2)

{

/*Display the first position*/

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFE; //1111 1110

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[num1];//

DU = 0; //Latch segment selection data

delay(1);


/*The second digit is displayed*/

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFD; //1111 1101

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[num2];//

DU = 0; //Latch segment selection data

delay(1);

}


void main()//main function itself will loop

{

while(1)

{

display(0, 0); //Nigital tube display function

}

}  


illustrate:

Due to the parameter passing, to display a total of 11 numbers from 00 to 10, you only need to call

Custom function void display(uchar num1, uchar num2)

Pass in:


display(0,1);

display(0,2);

display(0,3);

display(0,4);

display(0,5);

display(0,6);

display(0,7);

display(0,8);

display(0,9);

display(1,0);


So the other codes above are no longer copied.


display automatically

#include //Includes 51 header file

#include //Includes the shift standard library function header file


#define uint unsigned int

#define uchar unsigned char


sbit DU = P2^6;//digital tube segment selection

sbit WE = P2^7;//Nigital tube segment selection

uchar count = 0; // Variables that control the display of the digital tube

uchar i = 0; // Variable that controls the number of loops


//Common cathode digital tube segment selection table 0-9

uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};


/*====================================

Function : delay(uint z)

Parameter : z delay millisecond setting, value range 0-65535

Return value : None

Description : 12T/Fosc11.0592M millisecond delay

====================================*/

void delay(uint z)

{

uint x,y;

for(x = z; x > 0; x--)

for(y = 114; y > 0; y--);


/*====================================

Description : Two common cathode digital tube dynamic display

====================================*/

void display(uchar num1, uchar num2)

{

/*Display the first position*/

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFE; //1111 1110

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[num1];//

DU = 0; //Latch segment selection data

delay(1);


/*The second digit is displayed*/

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFD; //1111 1101

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[num2];//

DU = 0; //Latch segment selection data

delay(1);

}



/*====================================

Variable count: Responsible for controlling which two digital tubes are lit. For example, if count is 1, then the 01 digital tube is lit.

Description : Since two digital tubes are lit at the same time, the continuous circulation of the two digital tubes is required to achieve the simultaneous lighting of the two digital tubes.

So here we encapsulate the code that displays the digital tube on the microcontroller and let us call it.

====================================*/

void singlechip_show_num(uchar count){  

switch(count){ // Precisely manage the digital tube values ​​through switch case statements

    case 0 :

       display(0, 0);

       break; 

    case 1:

       display(0, 1);

       break;

case 2:

       display(0, 2);

       break;

case 3:

       display(0, 3);

       break;

case 4:

       display(0, 4);

       break;

case 5:

       display(0, 5);

       break;

case 6:

       display(0, 6);

       break;   

case 7:

       display(0, 7);

       break; 

case 8:

       display(0, 8);

       break;

case 9:

       display(0, 9);

       break;

case 10:

       display(1, 0);

       break;      

}

}


void main()//main function itself will loop

{

while(1)

{

for (i = 0; i < 200; i++){   // Cycle the lit digital tube 200 times to ensure that the digital tubes visible to the human eye are displayed at the same time.

singlechip_show_num(count);   // Call the digital tube display function

}


count++; //Each loop ends 200 times, the count variable itself +1, and the microcontroller displays the value after +1


if(count == 11){ // When the count variable equals 11, we re-initialize the variable to 0 and restart the counting cycle from 0

count = 0;

}

}

}  


The effect shows:

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here


Keywords:STC89C52 Reference address:The STC89C52 microcontroller lights up two digital tubes and can display a total of 11 numbers from 00 to 10.

Previous article:STC89C52 microcontroller uses timer to flash LED light
Next article:STC89C52 microcontroller independent keyboard

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号