51 MCU Tutorial from Scratch - 11 Data Transfer Instructions

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

Use the program to practice data transfer instructions

Data transfer instructions between the accumulator A of the microcontroller and the external RAM

MOVX A,@Ri

MOVX@Ri,A

MOVX A,@DPTR

MOVX @DPTR,A

Note:
1) In the 51 series MCU, the only accumulator that interacts with the external RAM is the A accumulator. All data that needs to be transferred to the external RAM must be sent through A, and all data that needs to be read from the external RAM must also be read through A. Here we can see the difference between the internal and external RAM. The internal RAM can directly transfer data, but the external RAM cannot. For example, to send a unit in the external RAM (set to the data of the 0100H unit) to another unit (set to the 0200H unit), the content of the 0100H unit must first be read into A, and then transferred to the 0200H unit.

To read or write external RAM, of course, you must know the address of the RAM. In the last two MCU instructions, the address is directly placed in DPTR. In the first two instructions, since Ri (i.e. R0 or R1) is only an 8-bit register, only the lower 8-bit address is provided. Because sometimes the number of extended external RAM is relatively small, less than or equal to 256, it is enough to provide only 8-bit address.

When using it, the address to be read or written should first be sent to DPTR or Ri, and then the read or write command should be used.

Example: Send the content in the 100H unit in the microcontroller's external RAM to the 200H unit in the external RAM.

MOV DPTR, #0100H

MOVX A, @DPTR

MOV DPTR,#0200H

MOVX @DPTR,A

Program memory transfers instruction to accumulator A

MOVC A, @A+DPTR This instruction is to send the number in ROM to A. This instruction is also called the microcontroller table lookup instruction, which is often used to look up a table that has been prepared in ROM. Description:

This instruction introduces a new addressing method: variable addressing. This instruction is to find data in an address unit of ROM. Obviously, the address of this unit must be known. The address of this unit is determined as follows: there is a number in DPTR and a number in A when executing this instruction. When executing the instruction, add the numbers in A and DPTR to get the address of the unit to be found.

The search result is placed in A, so the value in A may not be the same before and after the execution of this instruction.

Example: There is a number in R0, and you need to use a table lookup to determine its square value (the value range of this number is 0-5)

MOV DPTR, #TABLE

MOV A, R0

MOVC A, @A+DPTR

TABLE: DB 0,1,4,9,16,25

Assume that the value in R0 is 2, and is sent to A, and the value in DPTR is TABLE, then the address of the ROM unit finally determined is TABLE+2, that is, the number is taken from this unit, and the number is 4, which is obviously 2 squared. The same can be said for other data.

The real meaning of the label: From this point, we can also see another problem. We use labels to replace specific unit addresses. In fact, the real meaning of the label is the address value. Here it represents the starting position of the data 0, 1, 4, 9, 16, 25 stored in the ROM. In the LCALL DELAY microcontroller instruction we have learned before, DELAY represents the starting address of the program labeled DELAY stored in the ROM. In fact, the CPU finds this program through this address.

You can look at the meaning of the labels through the following routine:

MOV DPTR, #100H

MOV A, R0

MOVC A, @A+DPTR

ORG 0100H.

DB 0,1,4,9,16,25

If the value in R0 is 2, the final address is 100H+2, which is 102H, and the value found in unit 102H is 4. Do you understand this?

Why not write the program like this, using labels? Doesn't it just add to the confusion?

If we write a program in this way, we must determine the specific location of this table in the ROM when writing the program. If we want to insert another program before this program after writing the program, the location of this table will change again, and we have to change the sentence ORG 100H. We often need to modify the program, which is very troublesome, so we use labels instead. As long as the program is compiled, the position will change automatically. We leave this troublesome matter to the computer - referring to the computer we use.

Stack Operations

PUSH direct

POP direct

The first instruction is called push, which is to put the content in direct into the stack, and the second instruction is called pop, which is to put the content in the stack back into direct. The execution process of the push instruction is to first add 1 to the value in SP, then use the value in SP as the address, and put the value in direct into the RAM unit with the value in SP as the address. Example:

MOV SP, #5FH

MOV A, #100

MOV B, #20

PUSH ACC

PUSH B

The execution of the first PUSH ACC instruction is as follows: add 1 to the value in SP, which becomes 60H, and then send the value in A to the 60H unit. Therefore, after executing this instruction, the value of the 60H unit in memory is 100. Similarly, when executing PUSH B, SP+1 is added, which becomes 61H, and then the value in B is sent to the 61H unit. That is, after executing this instruction, the value in the 61H unit becomes 20.

The POP instruction is executed in the microcontroller as follows: first, the value in SP is used as the address, and the number in this address is sent to the direct following the POP instruction, and then SP is reduced by 1.

Continuing from the previous example:

POP B

POP ACC

The execution process is: use the value in SP (now 61H) as the address, take the value in the 61H unit (now 20), and send it to B, so after executing this instruction, the value in B is 20, then reduce SP by 1, so after this instruction is executed, the value of SP becomes 60H, then execute POP ACC, use the value in SP (60H) as the address, take the number from the address (now 100), and send it to ACC, so after executing this instruction, the value in ACC is 100.

What is the meaning of this? The value in ACC is originally 100, and the value in B is originally 20. Yes, in this example, it does not make sense, but in actual work, other instructions are usually executed after PUSH B, and these instructions will change the values ​​in A and B. So at the end of the program, if we want to restore the values ​​in A and B to their original values, then these instructions make sense.

Another question, if I don't use the stack, for example, use MOV 60H, A at the PUSH ACC instruction, use MOV 61H, B at the PUSH B instruction, and then use MOV A, 60H, MOV B, 61H to replace the two POP instructions, isn't it the same? Yes, it is the same in terms of the result, but it is different in terms of the process. The PUSH and POP instructions are single-byte, single-cycle instructions, while the MOV instruction is a double-byte, double-cycle instruction. What's more, the role of the stack is more than that, so there is a stack on general computers, and the same is true for microcontrollers. When we write subroutines and need to save data, we often do not use the latter method, but use the stack method to achieve it.

Example: Write out the running results of the following MCU program

MOV 30H, #12

MOV 31H, #23

PUSH 30H

PUSH 31H

POP 30H

POP 31H

As a result, the value in 30H becomes 23, and the value in 31H becomes 12. The data is exchanged. From this example, we can see that when using a stack, the order of writing into the stack and the order of writing out of the stack must be opposite to ensure that the data is sent back to the original position, otherwise an error will occur.

Homework: Execute the above routine under MCS51 and pay attention to the changes in the memory window and stack window.

Keywords:MCU Reference address:51 MCU Tutorial from Scratch - 11 Data Transfer Instructions

Previous article:51 MCU Tutorial from Scratch - 10 Data Transfer Instructions
Next article:51 MCU Tutorial from Scratch - 12 MCU Arithmetic Instructions

Recommended ReadingLatest update time:2024-11-17 12:24

Analysis of single bus data transmission in single chip microcomputer data communication
Pure microcontrollers cannot do big things, and must be equipped with various peripherals. Therefore, it is essential to understand the data communication between the microcontroller and the sensor. Common microcontroller data communication methods include SPI, IIC, RS232, single bus, etc. Each communication method has
[Microcontroller]
Analysis of single bus data transmission in single chip microcomputer data communication
STC microcontroller watchdog feeding program
*Epoch-making——51 single-chip microcomputer feeding the dog*/ /**/ #include "STC.h" #define LED_PORT P2 //Define LED control port as P2 /*****************************/ void Delay(void) { unsigned char i,j; for(i = 0;i 130;i ++) { for(j = 0;j 255;j ++); }  } /******************************
[Microcontroller]
Explanation of common terms related to single chip microcomputer
    Bus:    refers to an information transmission line that can serve multiple components. In a microcomputer system, each component communicates with each other through the bus.   Address bus (AB): The address bus is unidirectional and is used to transmit address information. The width of the address bus is 16 bits,
[Microcontroller]
LCD driver AY0438 and its interface design with PIC microcontroller
1. Overview AY0438 is a complete MCOS display driver produced by Microchip. It can directly drive LCD display modules under the control of a single-chip microcomputer or a microprocessor. It has a simple structure and is easy to use. Especially in driving 32-segment LCD displays, it can show its exquis
[Microcontroller]
LCD driver AY0438 and its interface design with PIC microcontroller
Interpretation of 51 single chip microcomputer LED system circuit
The LED rotating display is a rotating LED display screen developed based on the principle of visual persistence. It installs 16 LED light-emitting devices on a carrier with a certain rotation speed. Each LED light-emitting tube is arranged in a straight line with equal spacing. As the rotation speed increases, the pr
[Embedded]
Interpretation of 51 single chip microcomputer LED system circuit
Design of MTU and RTU radio telemetry system based on AVR single chip microcomputer and dedicated MODEM chip
    I. Introduction     MTU (Master Terminal Unit central dispatcher) and RTU radio telemetry system based on AVR microcontroller and dedicated MODEM chip.     Remote RTUs (the system can carry 256 RTUs) distributed at pipeline monitoring points throughout the city collect data, process the data and send the data to t
[Microcontroller]
51 MCU mixed programming
Keil C language and assembly language mixed programming   1. Embedding assembly in C language   1. To embed the assembly code snippet in the C file, add the assembly code in the following way:   #pragma ASM   ; Assembler Code Here   #pragma ENDASM   2. In the Project window, right-click the C fil
[Microcontroller]
STC15 series MCU SPI usage tutorial (Part 3)
Software emulation of SPI Take STC15W408AS microcontroller as an example 1. Hardware Wiring STC15 series MCU SPI usage tutorial (I) 2. Programming 1. Related macro definitions and pin definitions //Redefine the data type #ifndef fly #define uchar unsigned char #endif #ifndef uint #define uint  unsigned int #en
[Microcontroller]
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号