Improvement of MCU Program

Publisher:salahc1983Latest update time:2012-11-07 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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".

Reference address:Improvement of MCU Program

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

Chanel lost the lawsuit against Huawei for logo infringement in the European Court of Justice
On April 21, the European Court of Justice in Luxembourg ruled that the logo applied by Huawei for its computer hardware products would not cause confusion with Chanel's logo. This means that Chanel's lawsuit to protect its logo has temporarily failed. Chanel believes that the logo Huawei applied for for its computer
[Mobile phone portable]
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号