3rd lecture Digital Display

Source: Internet
Author: User
I. Digital Display Principle

We use the seven-and eight-segment LED digital tubes most often. The eight-segment has a decimal point while the other is basically the same. The so-called eight-segment is that there are eight small LED light-emitting diodes in the digital tube, and different shapes are displayed by controlling the light-off of different LEDs. Digital tubes can be divided into two types: Common cathode and common anode. In fact, the total cathode is to connect the eight LEDs together to make them grounded, in this way, no matter which led has a high end, it will be able to light up. The anode is to connect the anode of the eight LEDs together. The schematic diagram is as follows.

The two com ends of the pin chart are connected together, which is the public end. The co-yin digital tube needs to ground it, and the co-yang digital tube connects it to the positive 5 V power supply. An eight-segment digital tube is called a single digital tube. Multiple digital tubes can be combined to form multiple digital tubes. Their segment line selection (that is, a, B, c, d, e, f, g, DP), and their respective public ends are called bit line selection. During display, all segments are sent to the character encoding, and the digital tube will be lit when the selected position is selected.

The 8-segment digital tube, corresponding to a byte of 8 bits, A corresponds to the lowest bits, and DP corresponds to the highest bits. Therefore, if you want the digital display to 0, the character encoding of the co-yin digital tube is 00111111, that is, 0x3f; the character encoding of the co-yang digital tube is 11000000, that is, 0xc0. We can see that the two codes are the opposite. For example,.

II, Light up a digital tube

The following describes how to light up a digital tube with seven common overcast digital tubes.

L The P0 port of the 51 series single-chip microcomputer does not have the pull-up resistance (other ports). Therefore, if the P0 port is directly connected to the segment Line Selection of the digital tube, it cannot be lit. We need to add a 220 ohm pull-up resistance for it. Note that the pull-up resistance value cannot be too large. The schematic diagram of the experiment is as follows.

Among them, 7seg-com-cat-grn is a seven-section co-yin digital tube, which is displayed in green. Res indicates the resistance. Select the following resistors for resistance search, for example,.

Right-click the resistor in the selected graph and then left-click it. The resistance value of the resistor can be changed in the pop-up form. For example,.

The seven resistors look messy. In fact they can be replaced by an exclusive RESPACK-7. For example,.

The schematic diagram is finished here. Let's start to write the source program. Let the digital display character "0 ".

# Include

Void main ()

{

P0 = 0x3f; // The P0 port sends the encoding of the character '0'

}

The display effect is as follows.

This program is very easy, so we will not analyze it any more.

III, Different characters are displayed in a digital tube

The following program allows a digital tube to display different characters in turn.

# Include

Void delay ();

Void main ()

{

P0 = 0x3f; // display the character '0'

Delay (); // delay for a while

P0 = 0x06; // display the character '1'

Delay ();

P0 = 0x5b; // display the character '2'

Delay ();

}

Void delay ()

{

Int I, J;

For (I = 1000; I> 0; I --)

For (j = 100; j> 0; j --);

}

This program implements cyclic display of the characters '0', '1', and '2. But if we want to display a lot of other numbers cyclically, It is very troublesome to write their encoding every time. Here we can write all the encoding into an array, in the future, you only need to call the array. The program is as follows.

# Include

Unsigned char code table [] = {0x3f, 0x06, 0x5b}; // defines the encoding array. Pay attention to the last semicolon.

Void delay ();

Void main ()

{

P0 = table [0]; // call the first element of the array

Delay ();

P0 = table [1];

Delay ();

P0 = table [2];

Delay ();

}

Void delay ()

{

Int I, J;

For (I = 1000; I> 0; I --)

For (j = 100; j> 0; j --);

}

Here it should be noted that unsigned char indicates that the elements in the array are unsigned bytes data, and code indicates that this is an encoding array. After compilation, it does not occupy the memory space, but occupies the program storage space, we know that the memory size of a single-chip microcomputer is very small, so this is very important. Table is the name of an array. You can change it as needed. Since the elements in the array start from 0, table [0] is the first element 0x3f.

IV, Multiple digital tubes are displayed at the same time

The schematic diagram is as follows:

Among them, 7seg-mpx8-cc-blue is an 8-bit eight-segment co-overcast digital tube, which is displayed in blue. The segment is connected to the P0 port, and the position is connected to the P2 port.

Display All Digital tubes with the same character. The source program is as follows:

# Include

Void main ()

{

P2 = 0; // All P2 ports are low. Select all digital devices

P0 = 0x3f; // display the character '0'

}

This program only has one more "p2 = 0;" than the first program to implement bitwise selection. The final effect is as follows:

Allows random characters to be displayed. The source program is as follows:

# Include

Void main ()

{

P2 = 0xaa; // select the numbers 1, 3, and 5 from the left.

P0 = 0x3f;

}

The effect is as follows:

V, Dynamic Display

The above display is static, and the following describes dynamic display. What is static display and Dynamic Display? It will be very clear after reading the following content.

Since only one character can be displayed when multiple digital tubes are displayed, how can different digital tubes display different characters? We completed this program first, let the first digital display 1, the second digital display 2, and the third digital display 3. To make the program shorter, we only control the first three digits. The same is true for the other five digits.

The source program is as follows:

# Include

Unsigned char code table [] = {0x3f, 0x06, 0x5b, 0x4f };

Void delay ();

Void main ()

{

P2 = 0xfe; // select the first digital tube

P0 = table [1]; // display the character '1'

Delay (); // delay for a while

P2 = 0xfd; // select the second digital tube

P0 = table [2]; // display the character '2'

Delay ();

P2 = 0xfb; // select the third digital tube

P0 = table [3]; // display the character '3'

Delay ();

}

Void delay ()

{

Int I, J;

For (I = 1000; I> 0; I --)

For (j = 100; j> 0; j --);

}

This program is to select a digital tube to display a character, the same as the unit of digital display principle is the same. Here, you will find a delay after each character is displayed. What is the effect of this delay? We can first try to change this latency to see what the effect will be. Let's change the initial value of I in the first for loop in the delay () function from 1000 to 100, and then run the program. What is the effect? And then change it to 10? At this time, whether we want different digital tubes to display different characters at the same time has come out. The effect is as follows:

This is the dynamic display effect described above. What is the difference between static display and dynamic display? Obviously, in layman's terms, we send character encoding and bit selection signals in turn to digital users, and use the human eye's visual settings for the moment to make people feel like several digital tubes are lit up at the same time, in this way, different characters can be displayed on different digital tubes at the same time. For example, if you take the cigarette lit at night and go through the air at high speed, you will see a bright line, but in fact it is just a bright line. If you do not know about it, you can go to other documents to view the relevant Visual Knowledge temporarily. Static display is the number of digits selected at the same time. This is the fundamental difference between them.

VI, Shadow

Here we must first explain a problem. All the code we typed above is written directly to the main () function. Have you ever thought about how to run all the statements in the Main () function from the beginning to the end? You will think it will start from scratch, right! As can be seen from the previous program, commands run in an infinite loop. However, this default loop is not reliable. Generally, we use an endless loop statement in the program to implement an infinite loop. The main function of the source program can be changed:

Void main ()

{

While (1) // Infinite Loop

{

P2 = 0xfe;

P0 = table [1];

Delay ();

P2 = 0xfd;

P0 = table [2];

Delay ();

P2 = 0xfb;

P0 = table [3];

Delay ();

}

}

We can see that all the statements to be cyclic are stored in a while (1) {} loop for running. In future programs, the main part of the program will be placed in this statement.

After the program is written in this way, you can reduce the latency of the latency function, for example:

Void delay ()

{

Int I, J;

For (I = 5; I> 0; I --)

For (j = 1; j> 0; j --);

}

At this time, run the program, was it found very messy! The effect may be as follows:

This is what we call "shadow ".

In fact, on a real board, even if the delay is very long, you can see the phenomenon of "shadow. The reason for this is that the CPU runs very fast. when the data is sent to the bit selection and segment selection, the data is then sent to the bit selection, however, the segment selection data of this bit has not yet been sent, so this bit still maintains the data of the last segment selection, and then the segment selection data of this bit is sent, because of the visual residue, the Display Results of the selected data segments overlap, resulting in confusion. Simply put, a digital tube shows the overlapping effect of the characters to be displayed in the previous position and the characters to be displayed. To avoid "drag-and-drop", you must disable each digital tube after it is displayed. We can add "p2 = 0xff;" so that no digital tube is selected, the next digit will not be affected when it is displayed again. This is called "shadow ". Let's change the program to the following:

Void main ()

{

While (1)

{

P2 = 0xfe;

P0 = table [1];

Delay ();

P2 = 0xff; // shadow

P2 = 0xfd;

P0 = table [2];

Delay ();

P2 = 0xff;

P2 = 0xfb;

P0 = table [3];

Delay ();

P2 = 0xff;

}

}

However, after running, you will find that the effect has not changed. Why? To study the cause, we conduct online debugging and then run the program in one step to see how the program runs. We have already talked about how to perform online debugging.

For example, press the debug button in Keil to see that the Proteus simulation graph has started running. Then, select the source program one display interface in Keil and press the next-step debugging button to enter the subfunction. For example, the subfunction is entered in the following debugging process. After you press this button, a yellow arrow appears in front of the first statement, indicating that this statement has not been run and will be run next time.

Click the single-step button again. After the first statement is run, you will find that the first digital tube is lit up. Because no value is assigned, all seven sections are lit up. For example,.

Click the single-step button to see that although the segment selection has been assigned a value, the digital tube is not displayed. For example,.

Then, click a single step to enter the internal part of the delay () function. The digital tube also displays '1', for example.

Click the single step continuously until the delay () function exists. Then we click another single step button, which does not enter the subfunction. For example,.

After you click a single step, run the P2 = 0xff statement, and the digital tube is no longer displayed. For example, run the following command.

After clicking the single step and running the P2 = 0xfd statement, we found that the second digital tube actually displays '1'. In fact, this is also true because the data selected for the segment has not changed. This is the reason for "shadow. For example,.

Click the next step to prepare to run the latency function. For example,.

Click a single step. After running the latency function, the correct characters are displayed, for example.

The reason is found, so we can debug it online. We can see that many program running details can be found during single-step online debugging. Therefore, we can find the answer to some difficult questions through this solution.

We have seen that the program error is caused by the shadow statement "p2 = 0xff;" and does not play a proper role. How can we make a difference? Just now, during online debugging, we found that we only needed to select data for the digital tube, and it would be lit up, so we could send it to the segment to select data first, then, it is sent to the bitwise data, so that it will display the correct characters, and then delay it for a while, and then add the shadow statement, it will be extinguished. The second digit is sent to the selected data segment, but the digital tube still destroys the data. It will not produce a shadow. At this time, it will be sent to the selected data, and it will display the correct characters. The program can be changed as follows:

Void main ()

{

While (1)

{

P0 = table [1];

P2 = 0xfe;

Delay ();

P2 = 0xff;

P0 = table [2];

P2 = 0xfd;

Delay ();

P2 = 0xff;

P0 = table [3];

P2 = 0xfb;

Delay ();

P2 = 0xff;

}

}

In this way, after compilation, the running will be perfectly displayed. If you are not quite clear, you can perform online debugging again to check the program running process.

Note: As shown above, even if the order of the two statements is incorrect, the running effect of the entire program will be greatly affected. Debugging a program is a very complicated task. In order to avoid errors, we need to standardize our statements when writing the source program. We have thoroughly studied it from the beginning of the simplest program, in this way, you will not waste a lot of time on these details when writing large programs. Here we only provide a demo. There are many other details that need to be accumulated during the process of writing a large number of programs. Knowledge can be taught, but experience cannot be taught.

3rd lecture Digital Display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.