Assembling a 51 microcontroller to light up the LED in a loop

Publisher:郑哥Latest update time:2023-01-11 Source: zhihuKeywords:Assembly Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:Assembly Reference address:Assembling a 51 microcontroller to light up the LED in a loop

Previous article:Use assembly to write the beginning of 51 microcontroller to light up the LED
Next article:Assembling 51 microcontroller button detection

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号