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.
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
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Project source code] Design and implementation of traffic lights based on FPGA
- Detailed explanation of comprehensive knowledge of PCB design and wiring using Bluetooth speakers as an example
- [National Technology N32G457 Review] MP3 voice module based on serial communication and serial screen control
- ON Semiconductor RSL10 Bluetooth Development Board Experience
- 【GD32L233C-START Review】7. PWM LED Driver
- [Original] I recently heard that ST's chips have started to increase in price. So can domestic chips rise through this opportunity?
- Free review: Domestic RISC-V Linux board Fang Xingguang VisionFive
- How to configure DM368 to make the output image horizontally mirrored when using sensor image acquisition
- Chat: How does your company control technical documents?
- [ModelSim FAQ] Can't launch the ModelSim-Altera software