51 Single Chip Microcomputer (Ten)—— 8-bit Digital Tube-Digital Tube Scanning

Publisher:PeacefulWarriorLatest update time:2021-07-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Principle Introduction

The control circuit of the digital tube is introduced in detail in "51 Single-Chip Microcomputer (VIII)", and the hardware circuit is shown in the figure below.

In this experiment, we scan the digital tube. When conducting the experiment, you need to connect the P00~P07 pins of the microcontroller to J44. The corresponding relationship of the interface is (P00-D0), ..., (P07-D7). Connect the P10 and P11 pins of the microcontroller to J45, P10 corresponds to DM on J45, and P11 corresponds to WM.


2. Example Introduction

The code for this experiment is as follows


#include //Include header file, generally no need to change

 

#define DATA P0 //define data port. When you encounter DATA in the program, replace it with P0 port.

sbit DM = P1^0; //define latch enable port segment latch

sbit WM = P1^1; //define latch enable port bit latch

 

unsigned char BJTY_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //display segment code value 0123456789

unsigned char BJTY_WeiMa[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // respectively correspond to the corresponding digital tube light up, that is, the bit code

unsigned char m=0;

 

/************************************************************************/

/* Delay function */

/* The calculation of delay time corresponds to the 12MHz crystal oscillator of the microcontroller */

/* x is the delay in ms, for example: when x is 10, the delay time is about 10ms */

/************************************************************************/

void delay1ms(unsigned int x)

{

unsigned int i,j;

for(j=0;j for(i=0;i<123;i++);

}

/************************************************************************/

/* Main function */

/************************************************************************/

void main(void) 

{

for(;;) 

{

    DATA=0; // Clear the display to prevent ghosting

    DM=1; //Segment latch

    DM=0;

    DATA=BJTY_WeiMa[m]; //Get bit code 

    WM=1; //bit latch

    WM=0;

    DATA=BJTY_DuanMa[m]; //Get segment code

    DM=1; //Segment latch

    DM=0;   

    delay1ms(2); //delay 2ms

   

    m++;

    if(m==8) //Check if the 8-bit scan is finished

    m=0; //If the scan is completed, scan 8 bits again from the first one

}


Burn the program into the microcontroller and run it. The effect is as follows.

 1. Dynamic scanning is required when multiple digital tubes are needed to display different data. The scanning method is to light up the first digit of the digital tube first, then the second digit, and then light up the other digits. After lighting up the eighth digit, turn back to light up the first digit, and repeat this process. The dynamic scanning speed is very fast. Due to the visual persistence of the human eye, the effect seen is that all eight digital tubes are lit at the same time. The so-called "visual persistence" means that when the human eye observes the scene, the light signal is transmitted to the brain nerves for a short period of time. After the light effect ends, the visual image does not disappear immediately. This residual vision is called "afterimage", and this visual phenomenon is called "visual persistence".


It can be seen from the program that after assigning a bit of data to the digital tube, there is a delay of 2ms.


delay1ms(2); //delay 2ms


It takes a total of 16ms to display 8 bits of data, and the visual persistence time of the human eye is about 100ms. Therefore, although only one digital tube is lit at a single moment, the effect seen by people is that all 8-bit digital tubes are lit and display different numbers.


2. If statement


        In this program, the if statement is used.


          if(m==8) //Check if the 8-bit scan is finished


                 m=0; //If the scan is completed, scan 8 bits again from the first one


        The function of this if statement is to determine whether m is equal to 8. If so, the eighth digital tube is lit up, then m=0 is set, and the first digital tube is lit up in the next cycle.


        The if statement can form a branch structure. It judges based on the given conditions to decide to execute a branch program segment. There are three basic forms of the if statement in C language.


The first form is the basic form:


if(expression)


Statements


Its semantics is: if the value of the expression is true, the following statement is executed, otherwise the statement is not executed. The process can be represented as the following figure.

The second form is: if-else


if(expression)


Statement 1;


else


Statement 2;


Its semantics is: if the value of the expression is true, statement 1 is executed, otherwise statement 2 is executed. The process can be represented as the following figure.

The third form is the if-else if-else form


The first two forms of if statements are generally used in situations with two branches. When there are multiple branch choices, you can use if-else if-else statements, which are generally in the form of:


 if(expression1)


Statement 1;


else if(expression 2)


Statement 2;


else if(expression 3)


Statement 3;



else if (expression m-1)


Statement m-1;


else


statement m;


Its semantics is: judge the value of the expression in turn, and when a value is true, execute the corresponding statement. Then jump out of the entire if statement and continue to execute the program. If all expressions are false, execute statement m. Then continue to execute the subsequent program. The execution process of if-else if-else statement is shown in the figure below.

For the operation code of the latch, please refer to the explanation in "51 Single-Chip Microcomputer (Part 9)".


Reference address:51 Single Chip Microcomputer (Ten)—— 8-bit Digital Tube-Digital Tube Scanning

Previous article:51 Single Chip Microcomputer (XI)—— 8-bit Digital Tube-4-bit Accumulation
Next article:51 Single Chip Microcomputer (Part 9) - 8-bit Digital Tube - Display one bit

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号