Click the blue text to follow us
1. Overview of Microcontroller Programming Languages
1. Three commonly used programming languages for microcontrollers
The widely used high-level language is C. Whenever a new type of microcontroller is released, there is a corresponding C compiler to support it. The drawback of programming in high-level languages is that it is not real-time, the structure is not compact, and the compiled code occupies a relatively large amount of storage space, which is a disadvantage in microcontroller application systems with limited memory.
-
Instruction Statements
-
Pseudoinstruction Statements
Instruction format: Character Name EQU Number or Assembly Symbol
Example: PA8155 EQU 8001H ; assigns the value 8001H to the label PA8155
This makes the character name in the instruction equivalent to the given number or assembly symbol.
If a certain address is to be used multiple times in the program, the EQU instruction assigns it a character name, and once a change is needed, simply change the number after the EQU command.
Note: The character names equivalent to EQU must be assigned values before use, and in the same source program, the same label can only be assigned once.
2. Basic Structure of Assembly Language
1. Sequential Program Design
Example 1: Adding two unsigned double-byte numbers.
Assume the addend is stored in internal RAM at 40H (high byte), 41H (low byte), and the adder is stored at 50H (high byte), 51H (low byte), with the sum stored in 40H and 41H units.
The program is as follows:
ORG 0000H
JMP START
ORG 0100H
START: CLR C ; Clear Cy
MOV R0, #41H ; Load the address of the addend into data pointer R0
MOV R1, #51H ; Load the address of the adder into data pointer R1
MOV A, @R0 ; Load the content of the low byte of the addend into A
ADD A, @R1 ; Add the two low bytes
MOV @R0, A ; Store the sum of the low byte into the low byte of the addend
DEC R0 ; Point to the high byte of the addend
DEC R1 ; Point to the high byte of the adder
MOV A, @R0 ; Load the high byte of the addend into A
ADDC A, @R1 ; Add the two high bytes with Cy
MOV @R0, A ; Store the sum of the high byte into the high byte of the addend
END
2. Branch Program Design
-
Implementing branches: Conditional transfer instructions
Accumulator zero check instructions JZ/JNZ
Comparison transfer instruction CJNE
Bit control transfer instructions JC/JNC/JB/JNB/JBC
-
Control each branch: Unconditional transfer instructions
LJMP/AJMP/SJMP/JMP
Loop programs generally consist of four main parts:
(1) Initialization: Prepares for the loop program, such as setting loop counts, and pre-setting values for variables and address pointers.
(2) Processing: The part of the program that is executed repeatedly, it is the entity of the loop program and the main part of the loop program.
(3) Loop control: This part modifies loop variables and control variables, and checks if the loop should end, until the end condition is met, at which point it exits the loop.
(4) End part: This part mainly analyzes, processes, and stores the results of the loop program.
Example: Convert a byte’s hexadecimal number in a certain internal RAM unit into two ASCII codes, storing the result in two consecutive internal RAM units.
Assuming a byte’s hexadecimal number is in internal RAM unit 40H, and the result is stored in units 50H and 51H, parameter passing can be done using the stack, and the program is as follows:
MAIN: MOV R1, #50H ; R1 is the pointer for storing results
MOV A, 40H ; Ais the hexadecimal number to be converted
SWAP A ; Convert the high half byte first
PUSH ACC ;Push onto the stack
LCALL HEASC ; Call the subroutine to convert the content of the low half byte to ASCII code;
Subroutine HEASC for conversion
POP ACC
MOV @R1, A ; Store the conversion result of the high half byte
INC R1
PUSH 40H
LCALL HEASC
POP ACC
MOV @R1, A ; Store the conversion result of the low half byte
END
HEASC: MOV R0, SP
DEC R0
DEC R0 ; R0 points to the address of the hexadecimal parameter
XCH A, @R0 ; Retrieve the parameter to be converted
ANL A, #0FH ; Retain the low half byte
ADD A, #2 ; Modify the A value
MOVC A, @A+PC ; Lookup table
XCH A,@R0; Return the result to the stack
RET
TAB: DB 30H, 31H, 32H, …

Long press the image to follow
Discover more exciting content
WeChat ID: Mechanical-knowledge