Section 26: Dynamic scanning program to drive the digital tube in the while loop of the main function

Publisher:平和思绪Latest update time:2016-03-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Opening remarks:

In the previous section, I demonstrated the programming framework that I commonly use in industrial control through a robot automatic control program, but I have never touched on the human-machine interface. In most actual projects, the human-machine interface is indispensable. This section begins to talk about the most commonly used human-machine interface - the driver of the dynamic digital tube.

This section will teach you two knowledge points:

The first point: the dynamic driving principle of the digital tube.

The second point: How to transfer the content displayed on the digital tube to several variable interfaces through programming, so as to facilitate the writing of higher-level window programs in the future.

For details, please see the source code.

(1) Hardware platform: Based on Zhu Zhaoqi's 51 single-chip microcomputer learning board. Two 74HC595 chips are used to dynamically drive the eight-bit common cathode digital tube.

(2) Functions to be implemented:

After booting up, the display shows 8765.4321. Note that there is a decimal point.

(3) The source code is explained as follows:

#include "REG52.H"

void initial_myself();

void initial_peripheral();

void delay_short(unsigned int uiDelayShort);

void delay_long(unsigned int uiDelaylong);

//74HC595 driving the digital tube

void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01);

void display_drive(); //Drive function for displaying digital tube fonts

//74HC595 driving LED

void hc595_drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01);

sbit beep_dr=P2^7; //Buzzer driver IO port

sbit led_dr=P3^5; //When the pause indicator light is on, it means the process is paused

sbit dig_hc595_sh_dr=P2^0; //74HC595 program of digital tube

sbit dig_hc595_st_dr=P2^1;

sbit dig_hc595_ds_dr=P2^2;

sbit hc595_sh_dr=P2^3; //74HC595 program for LED light

sbit hc595_st_dr=P2^4;

sbit hc595_ds_dr=P2^5;

unsigned char ucDigShow8; //The content to be displayed on the 8th digital tube

unsigned char ucDigShow7; //The content to be displayed on the 7th digital tube

unsigned char ucDigShow6; //The content to be displayed on the 6th digital tube

unsigned char ucDigShow5; //The content to be displayed on the 5th digital tube

unsigned char ucDigShow4; //The content to be displayed on the 4th digital tube

unsigned char ucDigShow3; //The content to be displayed on the third digital tube

unsigned char ucDigShow2; //The content to be displayed on the second digital tube

unsigned char ucDigShow1; //The content to be displayed on the first digital tube

unsigned char ucDigDot8; //Whether the decimal point of digital tube 8 is displayed

unsigned char ucDigDot7; //Whether the decimal point of digital tube 7 is displayed

unsigned char ucDigDot6; //Whether the decimal point of digital tube 6 is displayed

unsigned char ucDigDot5; //Whether the decimal point of digital tube 5 is displayed

unsigned char ucDigDot4; //Whether the decimal point of digital tube 4 is displayed

unsigned char ucDigDot3; //Whether the decimal point of digital tube 3 is displayed

unsigned char ucDigDot2; //Whether the decimal point of digital tube 2 is displayed

unsigned char ucDigDot1; //Whether the decimal point of digital tube 1 is displayed

unsigned char ucDigShowTemp=0; //temporary intermediate variable

unsigned char ucDisplayDriveStep=1; //Dynamic scanning digital tube step variable

unsigned char ucDisplayUpdate=1; //Update display flag

//Common cathode digital tube character table obtained based on the schematic diagram

code unsigned char dig_table[]=

{

0x3f, //0 Sequence number 0

0x06, //1 Sequence number 1

0x5b, //2 Sequence number 2

0x4f, //3 Sequence number 3

0x66, //4 Sequence number 4

0x6d, //5 Sequence number 5

0x7d, //6 Sequence number 6

0x07, //7 Serial number 7

0x7f, //8 Sequence number 8

0x6f, //9 Sequence number 9

0x00, //Do not display serial number 10

};

void main()

{

initial_myself();

delay_long(100);

initial_peripheral();

while(1)

{

display_drive(); //Drive function for displaying digital tube fonts

}

}

/* Note 1:

* The principle of dynamic driving of digital tubes is that among the eight digital tubes, only one digital tube is displayed at any moment, and the other seven digital tubes are

* By setting its common bit com to a high level to turn off the display, as long as the speed of switching the screen is fast enough, human vision cannot distinguish it, and it feels like eight digital tubes

* are lit at the same time. In the following dig_hc595_drive(xx,yy) function, the first parameter xx is the pin that drives the digital tube segment seg, and the second parameter yy is the driver

* The pin of the common position com of the digital tube.

*/

void display_drive()

{

// The following program can also compress the capacity if some arrays and shifted elements are added. However, what Hongge pursues is not capacity, but clear explanation ideas.

switch(ucDisplayDriveStep)

{

case 1: //Display the first position

ucDigShowTemp=dig_table[ucDigShow1];

if(ucDigDot1==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfe);

break;

case 2: //Display the second digit

ucDigShowTemp=dig_table[ucDigShow2];

if(ucDigDot2==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfd);

break;

case 3: //Display the third digit

ucDigShowTemp=dig_table[ucDigShow3];

if(ucDigDot3==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xfb);

break;

case 4: //Display the 4th digit

ucDigShowTemp=dig_table[ucDigShow4];

if(ucDigDot4==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xf7);

break;

case 5: //Display the 5th digit

ucDigShowTemp=dig_table[ucDigShow5];

if(ucDigDot5==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xef);

break;

case 6: //Display the 6th digit

ucDigShowTemp=dig_table[ucDigShow6];

if(ucDigDot6==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xdf);

break;

case 7: //Display the 7th position

ucDigShowTemp=dig_table[ucDigShow7];

if(ucDigDot7==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0xbf);

break;

case 8: //Display the 8th digit

ucDigShowTemp=dig_table[ucDigShow8];

if(ucDigDot8==1)

{

ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point

}

dig_hc595_drive(ucDigShowTemp,0x7f);

break;

}

ucDisplayDriveStep++;

if(ucDisplayDriveStep>8) //After scanning 8 digital tubes, restart scanning from the first one

{

ucDisplayDriveStep=1;

}

/* Note 2:

* If the digital tube is driven directly by the IO pin of the microcontroller, since the driving speed is too fast, a little delay should be added here or

* Use counting delay to delay the time. The purpose is to stay for a while before switching to other digital tubes when switching to each digital tube display in the eight-digit digital tube.

* digit digital tube interface, which can increase the display effect. However, since Zhu Zhaoqi 51 learning board drives the digital tube indirectly through 74HC595,

* When the microcontroller drives 74HC595, the dig_hc595_drive function itself needs to execute many instructions, which is equivalent to delay.

* Therefore, there is no need to add a delay function or count delay.

*/

}

//74HC595 driver function of digital tube

void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01)

{

unsigned char i;

unsigned char ucTempData;

dig_hc595_sh_dr=0;

dig_hc595_st_dr=0;

ucTempData=ucDigStatusTemp16_09; //Send the high 8 bits first

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

{

if(ucTempData>=0x80)dig_hc595_ds_dr=1;

else dig_hc595_ds_dr=0;

/* Comment 3:

* Note that the delay_short here must be as small as possible, otherwise the speed of dynamic scanning of the digital tube will not be sufficient.

*/

dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

dig_hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

ucTempData=ucDigStatusTemp08_01; //Send the lower 8 bits first

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

{

if(ucTempData>=0x80)dig_hc595_ds_dr=1;

else dig_hc595_ds_dr=0;

dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

dig_hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

dig_hc595_st_dr=0; //The ST pin updates the data of the two registers and outputs them to the output pin of 74HC595 and latches them

delay_short(1);

dig_hc595_st_dr=1;

delay_short(1);

dig_hc595_sh_dr=0; //Pull low to enhance anti-interference

dig_hc595_st_dr=0;

dig_hc595_ds_dr=0;

}

//74HC595 driver function for LED lamp

void hc595_drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01)

{

unsigned char i;

unsigned char ucTempData;

hc595_sh_dr=0;

hc595_st_dr=0;

ucTempData=ucLedStatusTemp16_09; //Send the high 8 bits first

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

{

if(ucTempData>=0x80)hc595_ds_dr=1;

else hc595_ds_dr=0;

hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

ucTempData=ucLedStatusTemp08_01; //Send the lower 8 bits first

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

{

if(ucTempData>=0x80)hc595_ds_dr=1;

else hc595_ds_dr=0;

hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register

delay_short(1);

hc595_sh_dr=1;

delay_short(1);

ucTempData=ucTempData<<1;

}

hc595_st_dr=0; //The ST pin updates the data of the two registers and outputs them to the output pin of 74HC595 and latches them

delay_short(1);

hc595_st_dr=1;

delay_short(1);

hc595_sh_dr=0; //Pull low to enhance anti-interference

hc595_st_dr=0;

hc595_ds_dr=0;

}

void delay_short(unsigned int uiDelayShort)

{

unsigned int i;

for(i=0;i

{

; //A semicolon is equivalent to executing an empty statement

}

}

void delay_long(unsigned int uiDelayLong)

{

unsigned int i;

unsigned int j;

for(i=0;i

{

for(j=0;j<500;j++) //Number of empty instructions in the embedded loop

{

; //A semicolon is equivalent to executing an empty statement

}

}

}

void initial_myself() //Initialize the MCU in the first zone

{

led_dr=0; //Turn off the independent LED light

beep_dr=1; //Use PNP transistor to control the buzzer, it will not sound when the output is high level.

hc595_drive(0x00,0x00); //Turn off all LED lights driven by the other two 74HC595

}

void initial_peripheral() // Initialize the periphery of the second zone

{

/* Note 4:

* Transfer the contents displayed by the digital tube to the following variable interfaces to facilitate the writing of higher-level window programs in the future.

* Just change the content of the corresponding variables below to display the numbers you want. Beginners should take a closer look at the display_drive and other functions.

* If you understand the whole story, you will know the framework principle of this driver.

*/

ucDigShow8=8; //The content to be displayed on the 8th digital tube

ucDigShow7=7; //The content to be displayed on the 7th digital tube

ucDigShow6=6; //The content to be displayed on the 6th digital tube

ucDigShow5=5; //The content to be displayed on the 5th digital tube

ucDigShow4=4; //The content to be displayed on the 4th digital tube

ucDigShow3=3; //The content to be displayed on the 3rd digital tube

ucDigShow2=2; //The content to be displayed on the second digital tube

ucDigShow1=1; //The content to be displayed on the first digital tube

ucDigDot8=0;

ucDigDot7=0;

ucDigDot6=0;

ucDigDot5=1; //Display the 5th decimal point

ucDigDot4=0;

ucDigDot3=0;

ucDigDot2=0;

ucDigDot1=0;

}

Closing remarks:

After downloading this program to Zhu Zhaoqi 51 learning board, I found that the display effect is quite good. However, this program also has a weakness. In some projects, the more tasks in the main function loop, the longer the digital tube will stay at a certain moment. Once it exceeds a certain value, it will seriously affect the display effect. Is there any way to improve it? Of course there is. For details, please listen to the next analysis - the program of dynamically scanning digital tubes in the timer interrupt.

Reference address:Section 26: Dynamic scanning program to drive the digital tube in the while loop of the main function

Previous article:Section 25: Using LED lights and buttons to simulate motion control of industrial automation equipment
Next article:Section 27: Program for dynamically scanning digital tubes in a timer interrupt

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号