Without further ado, let’s start with the code:
ORG 0000H
AJMP MAIN
MAIN:SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAYA
AJMP MAIN
DELAY: MOV R7,#255
D1: MOV R6,#255
D2: DJNZ R6,D2
DJNZ R7,D1
RIGHT
DELAYA: MOV R7,#100
K1: MOV R6,#100
K2: DJNZ R6,K2
DJNZ R7,K1
RIGHT
END
Let me analyze this entire program from beginning to end: First, this program is used to light up the P1.0 lamp in a loop.
ORG 0000H; We have already mentioned its function in this sentence. If you don’t understand, look ahead.
AJMP MAIN; This sentence is divided into two parts, AJMP and MAIN. AJMP is an absolute jump instruction, which is called absolute jump in English. Similar transfer instructions include ljmp, sjmp, and jmp. Here is a brief explanation, ljmp is a long jump, that is, the distance that can be jumped is relatively long, sjmp is a short jump, and JMP is a jump. Since I don’t plan to learn that deeply now, I don’t plan to go into it in depth, as long as I can achieve the goal. That’s it. MAIN is a label, which means jumping to the program running where the label is located. The label is equivalent to an address.
MAIN:SETB P1.0; This is the content in the MAIN label. Use a semicolon to separate the label and the program. The program setb is to set the bit. Turn the p1.0 bit to high level, which is set bit
LCALL DELAY; I need to explain it carefully here. lcall is a long instruction to call a function, which is composed of long call. call is a statement to call a function. The function name is DELAY. This is similar to a transfer instruction, but the function is a block. Its RET is returned later, and the transfer is to go to the content of that label for execution, no matter what is returned, it is not returned. The function of this function is a delay.
CLR P1.0; After the delay is over, clear p1.0 to 0, clear.
LCALL DELAYA; then continue to execute the next delay
AJMP MAIN; Here is another jump instruction. Jumping back to the main label is equivalent to repeatedly executing the code here.
DELAY: MOV R7, #255; This is a delay function. r7 is an 8-bit working register, so it can hold up to 255. The function of the instruction is to put 255 into the register r7.
D1: MOV R6, #255; d1 is the label, the function of the command is the same as above
D2: DJNZ R6,D2; D2 is also a label. DJNZ is equivalent to if else in C language. Let me explain its function in detail. The dec 1 jump if not zero instruction is to put r6 in Subtract one from the content of , and then determine whether it is equal to 0. If it is not equal to 0, jump to the content of the following label and continue execution. If it is equal to 0, continue to execute the next instruction, and then because the following label is still D2, so This instruction is executed until r6 equals 0
DJNZ R7,D1; This command is the same as the above, except that it jumps back to D1, so it goes back to d1 and repeats it.
RET; This is the return of the function, indicating the end of the function.
I'll write a piece of code in C language to represent the delay function in this assembly, otherwise I'm afraid you won't understand it.
public void delay()
{
for(int i=255;i>0;i--)
{
for(int j=255;j>0;j--)
{}
}
}
That is, there are 255 subtraction operations inside first, and then a 255 subtraction operation is surrounded outside, so the total is 255*255 subtraction operations. We know that the microcontroller runs relatively fast, so maybe the delay of your microcontroller running like this is not enough. You can add another surround, but this layer of surround about 10 times is enough.
DELAYA: MOV R7,#100; This is a shorter delay function
K1: MOV R6,#100
K2: DJNZ R6,K2
DJNZ R7,K1
RIGHT
END; remember to add the end of the program afterwards
Previous article:Use assembly to write the beginning of 51 microcontroller to light up the LED
Next article:Assembling 51 microcontroller button detection
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- Thermal resistance measurement circuit
- Sharing a video of a senior hardware engineer simulating DDR3 SDRAM with Cadence ------- Signal integrity DDR3 timing analysis
- Prepare to make a low-cost ESP32 small board
- Chip CH551 please write a simple program
- [LiChuang Open Source] First try of the national MCU N32G430 current meter and fast charging sampling
- 2019 Electricity Competition Power Supply Competition Topic Analysis and Data Sharing
- FPGA and Digital Image Processing
- Score points! Tell us how you have prepared for the eSports tournament so far and what you think.
- Prize live broadcast opens up IoT society Nichicon's new power storage device
- SensorTile.box_--Unicleo-GUI debugging tutorial