How can we make the light flash continuously? Actually, we need to make the light turn on for a while and then turn off for a while, which means that P10 needs to output high and low levels continuously. How can we achieve this requirement? Please consider whether the following instructions are feasible:
SETB P10
CLR P10
………
This is not possible. There are two problems. First, the computer executes instructions very quickly. After executing SETB P10, the light turns off, but after a very short time (microseconds), the computer executes the CLR P10 instruction again, and the light turns on again. Therefore, it is impossible to tell that the light was off. Second, after executing CLR P10, the SETB P10 instruction will not be executed again, so there will be no chance to turn it off again.
To solve these two problems, we can make the following assumptions. First, after executing SETB P10, we can delay for a period of time (a few seconds or a few tenths of a second) and then execute the second instruction, so that we can distinguish that the light has been off. Second, after executing the second instruction, let the computer execute the first instruction again, and keep going around in circles. We call this a "loop", and this will complete the task.
The following is the program (the numbers in brackets are written for the convenience of explanation and do not need to be entered):
Main program:
LOOP: SETB P10; (1)
LCALL DELAY; (2)
CLR P10; (3)
LCALL DELAY; (4)
AJMP LOOP; (5)
; The following subroutine
DELAY: MOV R7, #250; (6)
D1: MOV R6, #250; (7)
D2: DJNZ R6, D2; (8)
DJNZ R7, D1; (9)
RET; (10)
END; (11)
Analyze the first five instructions according to the above assumptions.
The first one is to turn off the light, the second one should be a delay, the third one is to turn on the light, the fourth one is exactly the same as the second one, also a delay, and the fifth one should be to jump to the first instruction. The implementation principles of the second and fourth ones will be discussed later. Let's look at the fifth one first. LJMP is an instruction, which means transfer. Where to transfer? It is followed by LOOP. Let's see where there is another LOOP. Yes, there is a LOOP before the first instruction, so it is very intuitive that we can know that it will jump to the first instruction. The LOOP before the first instruction is called a label. Its purpose is to give this line a name for easy use. Do we have to call it LOOP? Of course not. What name to give is entirely determined by the programmer. It can be called A, X, etc. Of course, at this time, the name after the fifth instruction LJMP must also be changed.
The purpose of the second and fourth instructions is to delay. How is it implemented? The form of the instruction is LCALL. This instruction is called a subroutine call instruction. Look at what follows the instruction, DELAY. Find DELAY before the sixth instruction. Obviously, this is also a label. The function of this instruction is as follows: when the LCALL instruction is executed, the program will go to the program marked by the label after LCALL for execution. If the RET instruction is encountered during the execution of the instruction, the program will return to the instruction below the LCALL instruction to continue execution. From the instructions starting from the sixth line, you can see that there is indeed a RET instruction. After executing the second instruction, it will switch to execute the 6th instruction. After executing instructions 6, 7, 8, and 9, it will encounter the 10th instruction: RET. After executing this instruction, the program will come back to execute the third instruction, which will clear P10 to turn on the light. Then there is the fourth instruction. Executing the fourth instruction means switching to execute instructions 6, 7, 8, 9, and 10, and then coming back to execute the 5th instruction. The 5th instruction means letting the program return to the 1st instruction and start executing. This cycle repeats itself, and the light keeps turning on and off.
All the programs from the line marked with DELAY to the line marked with RET are delay programs, which are about a few tenths of a second. As for the specific time, we will learn how to calculate it later. The last line of the program is END, which is not an instruction. It just tells us that the program ends here. It is called a "pseudo-instruction".
Previous article:UPS digital phase-locking technology based on single chip microcomputer
Next article:Temperature plotter based on tinyAVR and Nokia3310 LCD
Recommended ReadingLatest update time:2024-11-16 14:25
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
- EEWORLD University ---- Computer Architecture (Huazhong University of Science and Technology)
- Innovative application of capacitive touch buttons in the elevator industry
- The role of inductance
- EEWORLD University ---- Motor Control
- Overload protector related
- Sub-library: Summary of official Chinese technical documents on temperature and humidity sensors
- Rainbow Arch designed with Circuit Playground Express
- An interview with Roger Hall, General Manager of Qorvo High Performance Solutions Division, to help you learn more about 5G
- 【ST NUCLEO-H743ZI Review】——by bigbat
- How long have you not spent the Lantern Festival at home?