[MCS-51 Single-chip Microcomputer Assembly Language] Final Review Summary ②——Assembly Programming (Question 2)

insert image description here

sequential structure

  • 程序默认以顺序结构执行

branch structure

  • Use CJNEto branch;
    insert image description here

insert image description here

Rules of use of CJNE

CJNE A, #data rel 
  • If (A) = (data), then 顺序执行;
  • If (A) > (data), then 0 -> (CY)并跳转(important);
  • If (A) < (data), then 1 -> (CY)并跳转(important);

loop structure

contains part

  • 循环初始化
  • 循环体
  • 循环控制
  • 循环结束

Classification

  • 计数式循环控制(Using the number of loops as the control condition for whether the loop is executed)

    • It is suitable for the structure of executing first and then judging;
    • Applicable to occasions where the number of cycles is known;
    • Multi-use DJNZinstructions;
  • 条件循环控制(Use whether a certain condition is satisfied or not as the control condition for whether the loop is executed)

    • Applicable to statements that are judged first and then executed;
    • Applicable to occasions where the number of cycles is unknown;
    • More use CJNE, JZ(judgment A) and JC, JNC(judgment CY) instructions;

Frequently Asked Questions

  • The types of frequently asked questions in this chapter are 汇编语言程序设计;

Example 1

topic description

  • Assume that Xthe value of the variable is stored in 内部RAM中的50单元, and the value of the function Yis stored in 内部RAM中的60Hthe unit. Please write a program to realize the following piecewise function.

insert image description here

answer

	ORG		0000H			;设置起始地址
	AJMP	MAIN			;跳转到MAIN开始执行
	ORG		0030H			;跳过一些终端地址
MAIN:						
	MOV	A,	50H				;取出X的值
	CJNE	A,	#12,	Jdg2;判断(A)是否等于12,不等于就跳转
BR1:
	MOV		60H,	#6		;令Y = 6
	SJMP	OUTG			;跳转到出口
Jdg2:
	JNC		BR3				;若(CY)=0,则说明X>12,则跳转到分支3
BR2:
	ADD		A,	#6			;X = X + 6
	MOV		60H,	A		;将结果给Y
	SJMP	OUTG			;分支结束,跳转到出口
BR3:
	ADD		A,	#2			;X = X + 2
	MOV		60H,	A		;将结果给Y
OUTG:
	SJMP	$				;在此处死循环
  • Be sure to read the notes carefully! ! !

Among them, representsBR1 the situation; represents the situation; represents the situation;X = 12BR2X > 12BR3X < 12

Example 2

topic description

  • 50H~5FH①Initialize the contents of the address interval of the on-chip RAM (0 ~ 15)to (50H) = 0, (51H) = 1..., ②Then copy the (50H ~ 5FH)contents of the interval to the unit of the on-chip RAM (30H ~ 3FH).

answer

	ORG		0000H
	AJMP	MAIN
	ORG		0030H			
MAIN:
	MOV		R7,		#16	;设置循环次数
	MOV		R0,		#30h;存储地址的起始位置
	MOV		A,		#0	;存储数的最小值
LOOP1:
	MOV		@R0,	A	;A中存的值放入R0处
	INC		A			;A = A + 1
	INC		R0			;R0 = R0 + 1
	DJNZ	R7,		LOOP1;(R7) = (R7)-1,若R7!=0,则跳转到LOOP1
	
	MOV		R0,		#50H;复制的起始地址
	MOV		R1,		#30H;粘贴的起始地址
	MOV		R7,		#16	;设置循环次数
LOOP2:
	MOV		A,		@R0	;R0中存的值放入A
	MOV		@R1,	A	;A中存的值放入R1处
	INC		R0			;R0 = R0 + 1
	INC		R1			;R1 = R1 + 1
	DJNZ	R7,		LOOP2;(R7) = (R7)-1,若R7!=0,则跳转到LOOP2
OUTG:
	SJMP $

Example 3

topic description

  • The circuit is shown in the figure. It is known that the crystal oscillator frequency of the microcontroller is 12MHZ. Please program to realize the flickering phenomenon of the light-emitting diode LED 亮0.1秒,灭0.1秒, which is required 模块化技术设计.

insert image description here

answer

important formula

  • T(机器周期) = 12 / ∫(晶振频率)

topic analysis

  • According to the characteristics of light-emitting diodes, if P1.0 is 高电平a diode , if P1.0 is 低电平a diode ;

  • According to the formula, if the crystal oscillator frequency is 12MHZ, then the machine cycle is 1微秒;

  • The title requires the use of modular programming, which means that it is required to use 子程序;

the code

	ORG		0000H
	SJMP	MAIN
	ORG		0030H
MAIN:
	SETB	P1.0		;LED灭
LOOP:
	CLR		P1.0		;LED亮
	ACALL	DL100ms		;延时0.1s
	SETB	P1.0		;LED灭
	ACALL	DL100ms		;延时0.1s
	SJMP	LOOP		;循环控制LED的亮灭
DL100ms:
	MOV	R6,	#100		;外循环产生100ms的延时
DL1ms:					
	MOV	R7,	#200		;产生1μs的延时需要1个机器周期
DL5μs:
	NOP					;NOP指令为1个机器周期
	NOP		
	NOP
	DJNZ	R7,	DL5ms	;DJNZ为2个机器周期
	DJNZ	R6,	DL1ms
	RET					;子程序返回

code analysis

  • The program uses an inline loop design. The inner loop (DL1ms) is 200 times, and each loop consumes 5个机器周期, that is 5微秒, so the total consumption 200x5 = 1000微妙 = 1ms;
  • The outer loop is 100 times to achieve the 100x1ms = 100ms = 0.1s delay effect;

More examples to be updated...

insert image description here

Guess you like

Origin blog.csdn.net/gllll_yu/article/details/131008366