The purpose of a microcontroller program is to make the microcontroller “obey” and perform a series of actions as designed by humans to achieve a specific function. For example, using a microcontroller to control a running light: after powering on, the first light turns on, then after a delay, the second light turns on, followed by a delay for the third light, and so on. After a delay, it returns to the first light, continuing this cycle indefinitely. There are several questions here: how many lights should turn on the first time, how long should the delay be, and how many different lights should be set to turn on in one cycle? These determine the patterns and speed of the running lights, and the microcontroller clearly lacks the ability to make judgments on its own; it must rely on humans to “assign” tasks, which it executes in the order designed by humans. To make the microcontroller “obey,” commands must be issued in a language that the microcontroller can understand, known as “instructions.” The number of instructions that any model of microcontroller can accept is limited; for example, the 8051 microcontroller has 111 instructions, while the PIC microcontroller has only 35. Each instruction performs a basic action, and to achieve a specific function, these instructions must be arranged and combined into a sequence, which is the program. There are two forms of instruction representation: machine language and assembly language. For instance, to set the P1.0 pin to a low level using machine language, it is represented as C2H 90H. By placing these two numbers into the microcontroller’s program memory, the microcontroller can execute them to set the P1.0 pin to a low level; to set it to a high level, the machine language representation is D2H 90H. As for why these two numbers are used, there is no need to delve into that; it is determined by the design engineers of the chip, and we cannot change it, only learn it. C2H 90H is an instruction, and such a numerical instruction format is difficult to memorize, making programming very challenging. Therefore, people use “mnemonics” to replace these numbers. For example, the mnemonic for C2H 90H is CLR P1.0, while for D2H 90H, it is SETB P1.0. Since mnemonics use abbreviations of English letters and have certain meanings, they are easier to remember and use than numerical instruction formats. Writing programs using mnemonic instructions is called “assembly language source code.” However, assembly language source code cannot be directly sent to the microcontroller; programs written in mnemonic form must ultimately be converted into numerical instruction formats like C2H 90H and D2H 90H to be sent to the microcontroller’s program memory. The process of converting mnemonic instructions into numerical instructions is called “assembly.” There are two methods of assembly: one is “manual assembly,” where people find the numerical form of the written assembly instructions through a lookup table and write them down; the other is “machine assembly,” where after writing the assembly language source code, a computer software processes these source programs to obtain a sequence of numerical instructions, which is usually referred to as “machine code,” the code that can be directly executed by the machine. The numerical instruction sequence obtained after machine assembly is saved as a file on the disk, and this file typically has a .HEX extension, commonly known as a HEX file. Once the HEX file is obtained, it can be written into the program memory. To write the program into the chip, a specialized device called a “programmer” is usually required, along with compatible programming software running on a computer. By opening the HEX file in this software, it can be written into the chip. With the advancement of technology, programming methods have become increasingly diverse, but the overall concept remains the same. Specific details will be addressed in another question, “What is a Microcontroller Programmer?”
Scan the QR code below to follow us!
Let’s walk together on the electronic road!