51 MCU Tutorial from Scratch - Part 6 Delay Program Analysis

Publisher:zhuanshiLatest update time:2012-02-15 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Master the delay program in assembly

In the last class, we already know that the symbols R7 and R6 in the program represent RAM units, which are used to store some data. Now let’s take a look at the meaning of other symbols.

DELAY: MOV R7,#250   ;(6)

D1: MOV R6,#250   ;(7)

D2: DJNZ R6,D2    ;(8)

DJNZ R7, D1 t0

RIGHT ;(10)

Click to browse the next page

〈MCU Delay Program〉

MOV: This is an instruction, which means to transfer data. Speaking of transfer, we all know that to transfer something, we need to transfer it from one person's hand to another person's hand, which means there must be a receiver, a transmitter and something. From the instruction MOV R7, #250, R7 is a receiver, 250 is the number to be transferred, and the transmitter is omitted in this instruction (note: not every transfer instruction will be omitted, in fact, most data transfer instructions will have a transmitter). Its meaning is also very clear: send the data 250 to R7, so after executing this instruction, the value in the R7 unit should be 250. There is a # sign in front of 250, what does this mean? This # is used to indicate that 250 is the thing itself that is being transferred, not the transmitter. So what does MOV R6, #250 mean? There should be no need to analyze it.

DJNZ: This is another instruction. Let's look at the two things following this instruction. One is R6 and the other is D2. We already know what R6 is. Let's check what D2 is. D2 is in front of this line. We have already learned that it is called a label. What is the purpose of a label? It is to give this line a name. The execution process of the DJNZ instruction is as follows. It subtracts 1 from the value of the first parameter behind it, and then checks to see if the value is equal to 0. If it is equal to 0, it will continue to execute. If it is not equal to 0, it will transfer to where? You may have guessed that it transfers to the place specified by the second parameter (please explain how this statement is executed in your own words). The final execution result of this instruction is to circle around 250 times.

After executing DJNZ R6, D2 (that is, after the value of R6 is equal to 0), the next line will be executed, that is, DJNZ R7, D1. Please analyze the result of this sentence by yourself. (Mov R6, #250 will be executed, and the value in R7 will be reduced by 1). Finally, DJNZ R6, D2 will be executed 250*250=62500 times. Why execute the same instruction so many times? It is to delay.

A question: What will happen if 0 is put in R6?

2. Timing Analysis:

We introduced the delay program earlier, but it is not perfect, because we only know that the sentence DJNZ R6, D2 will be executed 62500 times, but how long will it take to execute so many times? Does it meet our requirements? We don't know yet, so we will solve this problem below.

Let me first ask a question: What is the most important thing in our school? (Bell) The principal can go on business trips, and teachers can take a break, but if there is no bell in the school for a day, there will be chaos. The whole school works in unison and in a coordinated manner under the unified command of the bell. This bell rings according to a certain schedule, which we can call "time sequence - the order of time". A unit composed of people must have a certain time sequence, and computers must have a stricter time sequence. In fact, a computer is more like a big clock. There are strict rules for when the minute hand moves, when the second hand moves, and when the hour hand moves, and there can be no chaos at all. The things that computers have to accomplish are more complicated, so their time sequence is also more complicated.

We know that when a computer is working, it fetches instructions from the ROM one by one and then executes them step by step. We define the time it takes for a computer to access the memory as a machine cycle. This is a time reference, just like we humans use "seconds" as our time reference. Why not just use "seconds"? It's so good and very familiar. As we continue to learn, we will find that using "seconds" is not a habit.

A machine cycle consists of 12 clock cycles. Let's calculate how long a machine cycle is. Suppose a single-chip microcomputer works with a 12M crystal oscillator, and its clock cycle is 1/12 (microseconds). Its machine cycle is 12*(1/12) or 1 microsecond. (Please calculate the machine cycle of a single-chip microcomputer working with a 6M crystal oscillator).

Among all the instructions of the MCS-51 microcontroller, some are completed relatively quickly and only require one machine cycle, some are completed relatively slowly and require two machine cycles, and two instructions require four machine cycles. This is not difficult to solve, is it? The execution time of the instruction I ask you to sweep the floor is always longer than the instruction of wiping the blackboard. In order to keep the length of the instruction execution time constant, a new concept is introduced: instruction cycle. The so-called instruction cycle refers to the time to execute an instruction. INTEL gives the instruction cycle number for each instruction. Most of these data do not need to be remembered by us, but some instructions need to be remembered, such as the DJNZ instruction, which is a two-cycle instruction.

Let's calculate the delay. First, we must know the frequency of the crystal oscillator. If the crystal oscillator is 12M, then one machine cycle is 1 microsecond. The DJNZ instruction is a two-cycle instruction, so it takes 2 microseconds to execute once. A total of 62,500 executions is exactly 125,000 microseconds, or 125 milliseconds.

Exercise: Design a delay program with a delay of 100 milliseconds.

Key points analysis: 1. Whether the number in a unit can exceed 255. 2. How to allocate two numbers.

3. Reset Circuit

1. Reset method

⒈ Reset condition:
The RST pin maintains a high level for more than 2 machine cycles.

⒉ Reset circuit

Click to browse the next page

〈MCU reset circuit〉

⒊ CPU status after reset

PC: 0000H TMOD: 00H

Acc: 00H TCON: 00H

B:     00H          TH0:  00H

PSW:00H TL0:00H

SP:  07H             TH1:  00H

DPTR:0000H TL1: 00H

P0~P3:FFH          SCON: 00H

IP: ×××00000B SBUF: Uncertain

IE:0××00000B       PCON: 0×××0000B

Any single-chip microcomputer must have a reset process before working. What does reset mean? It is like the preparatory bell we ring before class. When the preparatory bell rings, everyone automatically enters the classroom from the playground and other places. During this period of time, there is no teacher intervention. For the single-chip microcomputer, the program has not yet started to execute, and it is doing preparation work. Obviously, the preparation work does not take too long, and the reset only takes 5ms. How to reset? Just add a high level to the RST pin of the single-chip microcomputer. As mentioned above, the time is not less than 5ms. In order to achieve this requirement, many methods can be used. Here is one for reference, see Figure 1. In fact, we have seen it in the figure of the last experiment.

The working principle of this reset circuit is: when the power is turned on, the two ends of the capacitor are equivalent to a short circuit, so the RST pin is at a high level, and then the power supply charges the capacitor through the resistor, the voltage at the RST end slowly drops to a certain level, that is, a low level, and the microcontroller starts to work normally.

Keywords:MCU Reference address:51 MCU Tutorial from Scratch - Part 6 Delay Program Analysis

Previous article:51 MCU Tutorial from Scratch - Part 5 MCU Mini Program
Next article:51 MCU Tutorial from Scratch - Part 7 Parallel Port Structure

Recommended ReadingLatest update time:2024-11-16 15:56

How big is the pull-up resistor of 51 MCU?
1. In order to achieve quasi-3-state, the P0 port of the 51 single-chip microcomputer adopts OC output, that is, collector floating output, also called totem pole output. This circuit structure has only pull-down capability, and there is no current in the high-level output, which shows a high-impedance state at a high
[Microcontroller]
Simulation Design of PIC Microcontroller Source Program
When we edit the C language of PIC microcontroller, we often use the simulation technology of the source program, because the simulation of the program can replace the hardware production and debugging of part of the microcontroller. Even when we study the C language program of PIC microcontroller in depth, the simula
[Microcontroller]
Simulation Design of PIC Microcontroller Source Program
What is the difference between Atmega16 and 51 microcontrollers? Analysis from both software and hardware aspects
1. Introduction to Atmega16 microcontroller Today our topic is microcontrollers. When you talk about microcontrollers, you may first think of the 51 microcontroller, and then use external digital circuits and analog circuits to control peripheral hardware, such as PWM, ADC, I2C, etc. So what are we talking about today
[Microcontroller]
What is the difference between Atmega16 and 51 microcontrollers? Analysis from both software and hardware aspects
Design of LCD1602 electronic clock based on single chip microcomputer
Learning tasks: (1) Basic understanding of LCD1602 (2) Using LCD1602 to realize minute and second timing based on 51 single chip microcomputer; (3) Using buttons to control the adjustment of minutes and seconds (two buttons control the increase of minutes and seconds respectively); (4) Able to realize the function of
[Microcontroller]
WiFi-based detection car with real-time video transmission
introduction With the development of automation technology, intelligent control is widely used in military, scientific research and civilian fields. The wireless detection car integrates electronics, mechanics, communications, sensors, computer hardware and software, artificial intelligence and automatic control. In a
[Microcontroller]
WiFi-based detection car with real-time video transmission
Design of temperature acquisition system based on single chip microcomputer
Abstract: This system is a temperature acquisition control system with 89C51 single-chip microcomputer as the core control technology. It introduces the design scheme of temperature acquisition system composed of DS18B20 temperature sensor. The lower computer of this temperature acquisition system adopts 89C51 single-
[Microcontroller]
Design of temperature acquisition system based on single chip microcomputer
Design of electrical parameter measurement system based on single chip microcomputer
In automatic measurement and control systems, it is often necessary to measure and display relevant electrical parameters. At present, most measurement systems still use transformer-type voltage and current transformers. Due to the non-ideality of the transformer, there are large errors in the ratio and phase measurem
[Microcontroller]
Design of electrical parameter measurement system based on single chip microcomputer
Solutions to optimize real-time performance and efficiency through intelligent motor control
For decades, most motor control applications have relied on general-purpose brushed DC and stepper motors that provide low cost and ease of implementation. However, with the continuous innovation and integration of microprocessor (MCU) architectures, developers today can use more advanced and intelligent motor types a
[Microcontroller]
Solutions to optimize real-time performance and efficiency through intelligent motor control
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号