+86 0755-83044319

Technical Blogs

/
/

Microcontroller Basics

release time:2024-02-24Author source:SlkorBrowse:1296

The Composition of Single-Chip Microcomputer


What are the most important parts that a single-chip microcomputer should have in order to automatically perform calculations?


Let's take an abacus as an example to calculate an arithmetic problem. For instance, 36 + 163 × 156 - 166 ÷ 34. To carry out this calculation manually, we would need an abacus, paper, and a pen. We would record the problem to be solved and then step by step, first calculate 163 × 156 and add it to 36, then calculate 166 ÷ 34 and subtract it from the previous result to get the final answer.


Now, if we want to use a single-chip microcomputer to perform the same process, it would need key components to replace the abacus for computation, which is the "arithmetic unit." Additionally, it would need devices to serve as paper and pen to store the original problem, the initial data, intermediate results, and the commands for automatic computation. These components are known as "storage units." Furthermore, there would be a need for a controller to act as a substitute for human decision-making, issuing various control signals based on predetermined commands to ensure the smooth progression of the entire calculation process. However, having these three components alone is not sufficient; the input of original data and commands, as well as the output of the calculation results, must follow a specific sequence and sometimes require waiting.


For example, when calculating 163 × 156, the number 36 cannot enter the arithmetic unit simultaneously. Therefore, it is necessary to set up "gates" in the single-chip microcomputer according to the controller's commands, allowing new data to enter the arithmetic unit when needed. Similarly, when the arithmetic unit obtains the final result, this result should be outputted, but the intermediate results should not be able to freely "leak" out of the single-chip microcomputer. This kind of control over input and output data is managed through "gate" circuits within the single-chip microcomputer, known as "ports." In a single-chip microcomputer, there are essentially three types of information in circulation: data, including various original data, intermediate results, and programs (a collection of commands), which need to be input into the single-chip microcomputer through the "ports," stored in the storage unit, read by the arithmetic unit for computation, and have the arithmetic unit's intermediate results stored back into the storage unit, or be outputted by the arithmetic unit through the "input/output ports."


Additionally, various commands (programs) that users want the single-chip microcomputer to execute are sent into the controller as data from the storage unit. The controller interprets (decodes) these commands into various control signals for executing functions such as addition, subtraction, multiplication, division, etc. This type of information is known as control commands, as the controller controls the arithmetic unit to perform the computation and processing step by step, as well as controlling the reading (fetching data) and writing (storing data) of the storage unit. The third type of information is address information, which tells the arithmetic unit and the controller where to fetch instructions and data, where to store the results, through which port to input and output information, etc.


The storage unit is further divided into read-only memory (ROM) and random-access memory (RAM). The former stores pre-programmed fixed programs and constants, while the latter stores data that may change at any time. As the name suggests, once data is stored in the ROM, it can only be read and cannot be changed (types of ROM such as EPROM, E2PROM, etc., can be altered or have data written using specific methods - Editor's note). In contrast, RAM allows data to be stored and read at any time.


In practice, the arithmetic unit and the controller are often combined and referred to as the central processing unit (CPU). In addition to performing computations, the single-chip microcomputer also requires control functions. Therefore, it cannot do without counting and timing. Hence, a timer/counter is incorporated into the single-chip microcomputer to ensure rhythmic operations during computation and control. Furthermore, there is a so-called "interrupt system," which serves as a "message delivery room." When the parameters of the controlled object of the single-chip microcomputer reach a state that requires intervention, the interrupt system can notify the CPU through this "message delivery room", prompting the CPU to take appropriate measures based on the urgency of the external situation.


Now that we have covered the basic composition of the single-chip microcomputer, the remaining question is how to interconnect its various parts into a cohesive whole. Internally, the single-chip microcomputer has a "bus" that connects these parts, known as the "internal bus." This bus acts as the "main road" of a city, with the CPU, ROM, RAM, I/O ports, and interrupt system distributed along the two sides of this "bus" and connected to it. Consequently, all instructions and data can be transmitted via the internal bus, similar to the transportation of various goods within a city through the main road.


Single-Chip Microcomputer Instruction System and Assembly Language Programs


Earlier, we discussed several major components of the single-chip microcomputer, which constitute the hardware of the single-chip microcomputer. Hardware refers to tangible entities that can be seen and touched. However, having such hardware alone only provides the potential to perform calculations and control functions. For the single-chip microcomputer to effectively perform calculations and control, it must also have the cooperation of software, mainly referring to various programs. Only by loading various correct programs into the single-chip microcomputer can it effectively work. The reason why the single-chip microcomputer can automatically perform calculations and control is precisely because people have pre-stored step-by-step processes of calculation and control in the form of commands, that is, a series of instructions, in the storage unit. Under the control of the CPU, the single-chip microcomputer retrieves and executes these instructions one by one.


For example, to perform a simple operation of adding two numbers, after the numbers to be operated on have been stored in the storage unit, the following steps need to be carried out:

Step 1: Retrieve one number from its storage unit and send it to the arithmetic unit.

Step 2: Retrieve the second number from its storage unit and send it to the arithmetic unit.

Step 3: Perform addition.

Step 4: Store the result of the addition in a specific storage unit.


All of these actions—retrieving data, sending data, performing addition, and storing data—are considered operations. When we express the various operations that we want the computer to execute in the form of commands, these are called instructions. However, how can the computer recognize and execute these operations? This depends on the instruction system assigned to the computer during its design. Each instruction corresponds to a basic operation, and the complete set of instructions that the single-chip microcomputer can execute is called its instruction system, and different types of single-chip microcomputers have different instruction systems.


When using a single-chip microcomputer, the problems to be solved should be compiled into a series of instructions in advance. These instructions must be ones that the selected single-chip microcomputer can recognize and execute. The instruction program compiled by the user to solve their own problems using a single-chip microcomputer is called a source program. Instructions are usually divided into two major parts: the operation code (opcode) and the operand. The operation code indicates what operation the computer should perform, that is, the function of the instruction; the operand represents the number or the address of the number involved in the operation. Since the single-chip microcomputer is a programmable device, it only "understands" binary code (0, 1). To make the single-chip microcomputer operate, all instructions in the single-chip microcomputer system must be represented in the form of binary code. For example, in Intel's MCS-51 series single-chip microcomputer, the instruction code for transferring a number from the storage unit to the accumulator (a dedicated register in the arithmetic unit for participating in operations and storing operation results) is 74H, the code for adding a number to the content of the accumulator is 24H, and the code for transferring the contents of the accumulator to the internal RAM storage unit is F6H to F7H. These instructions are represented in hexadecimal form, which corresponds to the binary machine code.


MCS-51 single-chip microcomputer has an 8-bit CPU, and sometimes, a single byte is insufficient to fully express certain operations. Therefore, the instruction system includes single-byte instructions as well as multi-byte instructions. The machine code consists of a sequence of 0s and 1s, which lacks distinct features, making it difficult to remember, understand, and prone to errors. Therefore, it is extremely challenging to write programs directly using machine code. As a solution, mnemonics—usually abbreviated English words representing the functions of the instructions—are used to replace the opcodes. For example, in the MCS-51 series, the commonly used mnemonic for the operation of moving data is "MOV", and for addition, it is "ADD". This way, each instruction has distinct action characteristics, making it easy to remember, understand, and less prone to errors. Programs written using mnemonics are called assembly language programs. However, although programs written with mnemonics are easy for humans to understand, the single-chip microcomputer only recognizes machine code in binary form. Therefore, to enable the single-chip microcomputer to "understand" assembly language programs, they must be converted into programs consisting of binary machine code. This conversion process is called "assembly", which can be achieved through manual table lookup or by using a PC through a so-called "cross-assembler program". Once the user program composed of machine code enters the single-chip microcomputer and the microcomputer is started, it will execute the tasks specified in the input program.


MCU-51 CPU and Memory

The CPU of the single-chip microcomputer 8051 consists of an arithmetic unit and a control unit.


I. Arithmetic Unit

The arithmetic unit, with the arithmetic/logic unit (ALU) at its core, is responsible for performing binary arithmetic and logical operations in the CPU. It also includes the temporary register TMP, the accumulator ACC, the register B, the program status flag register PSW, and the Boolean processor. The accumulator ACC is an eight-bit register and is the most frequently used register in the CPU. When performing arithmetic and logical operations, the accumulator ACC often temporarily stores an operand (such as the number to be added) before the operation and then saves the result afterward. The register B is mainly used for multiplication and division operations. The flag register PSW is also an eight-bit register used to store certain characteristics of the operation results, such as carry, borrow, etc. The specific meanings of its bits are as follows:

PSW CY AC FO RS1 RS0 OV -

For the user, the following four bits are the most important:


Carry flag CY (PSW 7). It indicates whether there is a carry (or borrow) during the operation. If there is a carry (for addition) or a borrow (for subtraction) in the highest bit of the operation result, this bit is 1; otherwise, it is 0.

Auxiliary carry flag AC. Also known as the half-carry flag, it reflects whether there is a half-carry in the lower four bits of two eight-bit numbers, indicating whether there is a carry (or borrow) in the addition (or subtraction) of the lower four bits. If there is a carry, AC is in state 1; otherwise, it is in state 0.

Overflow flag OV. MCS-51 reflects whether there is overflow in the operation results of signed numbers. When there is overflow, this bit is 1; otherwise, it is 0.

Parity flag P. It reflects the parity of the content in the accumulator ACC. If the result of the operation in the ACC has an even number of 1s (for example, 11001100B, which has 4 ones), P is 0; otherwise, P = 1. The meanings of the other bits in PSW will be introduced later. Because PSW stores the execution status of the program, it is also known as the program status word. The arithmetic unit also includes a logic processor (also known as a Boolean processor) that performs bit-level logical operations, which will be explained further when discussing bit instructions.


II. Control Unit

The control unit serves as the neural center of the CPU and includes the timing control logic circuit, the instruction register, the decoder, the data pointer DPTR, the program counter PC, and the stack pointer SP. The program counter PC is a 16-bit register composed of counters. To execute a program on the single-chip microcomputer, the program must be sequentially loaded into a designated area of the ROM. When the single-chip microcomputer is in operation, it should sequentially retrieve instructions from the storage unit for execution. Therefore, there needs to be a circuit that can identify the address of the instruction, which is the program counter PC. When the single-chip microcomputer starts running a program, the PC is loaded with the address of the first instruction. Every time an instruction (or a byte of a multi-byte instruction) is retrieved, the content of the PC automatically increments by 1 to point to the address of the next instruction, making the program execute sequentially. Only when the program encounters branching instructions, subroutine call instructions, or interrupts (which will be introduced later), does the PC move to the required location. The single-chip microcomputer specifies the address through the PC, retrieves the corresponding instruction byte from the ROM, and stores it in the instruction register. Then, the instruction code in the instruction register is decoded into various forms of control signals by the decoder. These signals, together with the clock pulses generated by the single-chip microcomputer's clock oscillator in the timing and control circuit, form varying voltage levels and clock pulses according to a certain timing beat, known as the control information. This information coordinates the data transfer between registers, operations, and other actions inside the CPU.


III. Memory

The memory is another essential component of the single-chip microcomputer, consisting of the random-access memory (RAM). Its capacity can be expanded to 64k and is used to store real-time input data. The internal RAM of the 8051 contains 256 storage units. From 00H to 7FH, the internal RAM stores general-purpose data, while from 80H to FFH, it serves as a special register area. When considering actual usage, understanding the structure of the internal RAM and the allocation of addresses is crucial since these will be frequently used while learning the instruction system and program design. The address space of the internal RAM of the 8051 spans from 00H to FFH, encompassing 256 bytes. This space is divided into two parts: the area from 00H to 7FH (0 to 127) is allocated as internal data RAM, while the area from 80H to FFH is reserved for special function registers. Within these 256 bytes, there is also a designated "bit address" area, which not only allows addressing by byte but also facilitates addressing by "bit." For data that requires bitwise operations, this area can be utilized. From 00H to 1FH, four sets of working registers have been allocated, with each group occupying 8 RAM bytes, denoted as R0 to R7. The selection of a register group is determined by the previously mentioned bits RS1 and RS0 in the flag register. By setting different binary numbers in these two bits, different register groups can be chosen, as shown in Table 1.


IV. Special Function Registers

The special function registers (SFR) have an address range of 80H to FFH. In the MCS-51, aside from the program counter PC and the four working register areas, the remaining 21 special function registers are located in this SFR block. Five of them are dual-byte registers, collectively occupying 26 bytes. The symbols and addresses of each special function register are listed in Table 2. Some of these special function registers are associated with the chip pins, while others are used for internal control functions. For example, P0 to P3 are parallel ports that serve as I/O ports, providing 32 I/O lines, each of which is bidirectional and mostly has a secondary function. The other registers are used for controlling the chip, and their functions will be further explained in subsequent sections.


Single-Chip Microcomputer Instruction System and Addressing Modes

For a single-chip microcomputer to operate properly, programs must be compiled, stored in the memory, and then executed by the CPU. A program is composed of instructions, and the basic components of an instruction are the operation code and the operand. Since there are many types of single-chip microcomputers, each with its own instruction system, the representation of the operation code and operand, as well as the instruction codes, varies. Thus, a thorough understanding of the complete instruction system of the selected single-chip microcomputer is necessary. While each series of single-chip microcomputers may have its own distinct instruction system, they also share commonalities. Mastering the instruction system of one single-chip microcomputer can facilitate understanding and working with other series. The MCS-51 series of single-chip microcomputers is widely used and has many derivative models, making it representative. Therefore, here the instruction system of the MCS-51 series is explained using the example of the composition and application of "instructions".


When using a single-chip microcomputer, the problems to be solved should be compiled into a series of instructions. These instructions must be ones that the selected single-chip microcomputer can recognize and execute. The instruction program compiled by the user to solve their own problems using a single-chip microcomputer is called a source program. Instructions are usually divided into two major parts: the operation code (opcode) and the operand. The operation code indicates what operation the computer should perform, that is, the function of the instruction; the operand represents the number or the address of the number involved in the operation. Since the single-chip microcomputer is a programmable device, it only "understands" binary code (0, 1). To make the single-chip microcomputer operate, all instructions in the single-chip microcomputer system must be represented in the form of binary code.


There are seven addressing modes in the MCS-51 series, and some are explained below:

Immediate Addressing:

The operand is written directly into the instruction and is placed alongside the operation code in the program storage unit. The "#" symbol is placed before the immediate value to indicate immediate addressing, for example, #20H.


Register Addressing:

The operand is placed in a register, and the name of the register directly represents the address of the operand in the instruction. For instance, MOV A, R0 belongs to register addressing, where the content of register R0 is transferred to the accumulator A.


Direct Addressing:

The operand is placed in a single unit of the single-chip microcomputer's internal RAM, and the address of this unit is directly written in the instruction. An example of this is ADD A, 70H, where 70H is the direct address.

Register Indirect Addressing:

The operand is stored in a specific unit in RAM, and the address of that unit is stored in register R0 or R1. If the address of RAM is greater than 256, then the address is stored in the 16-bit register DPTR (data pointer). In this case, the @ symbol is added before the register name to indicate this indirect addressing mode. For example, MOV A, @R0. Other addressing modes include indexed addressing, relative addressing, bit addressing, etc., which will be explained in detail later.


Some may ask, isn't it simple and clear to directly provide the actual operand in the instruction? Why do we need other addressing modes? This is because it is difficult to immediately provide the operand when programming. For example, when controlling temperature using a microcontroller, it is necessary to subtract the ambient temperature, which constantly changes, from the given control temperature (such as 20°C). Obviously, it cannot be provided in the program instruction. Instead, it needs to be inputted through an input/output port and stored in a register, which requires the use of register addressing. Another example is when performing arithmetic operations to calculate the average scores of each student in a class. It would be cumbersome to write a separate program for each student's scores and directly provide them in the program to calculate the average. Instead, a general program can be written to calculate the average score by inputting each student's score into different units in the memory. In this case, direct addressing can be used, and one program can be used for each student, making it more convenient. Therefore, the more addressing modes there are, the more convenient and flexible the programming becomes, and the wider the range of applications.


Addressing is like finding a person. If the person being sought has various contact methods such as a mobile phone, pager, landline, etc., it is easier to find them. The same goes for microcontrollers. The more addressing modes there are, the easier it is to find the operands, and the stronger the functionality of the microcontroller. When introducing the addressing modes of the 8051 series microcontroller earlier, various registers within the microcontroller, such as the accumulator A, general-purpose registers R0 to R7, data pointer DPTR, and memory, were often encountered. In the subsequent introduction of instructions, data will be transferred or processed between these registers and memory. Therefore, it is necessary to be familiar with the internal structure of the microcontroller in order to write programs.


The basic characteristics of the internal overall structure of the 8051 microcontroller are as follows:


  • 8-bit CPU, on-chip oscillator

  • 4k bytes of ROM, 128 bytes of RAM

  • 21 special function registers

  • 32 I/O lines

  • Addressable 64k bytes of external data and program storage space

  • 2 16-bit timers/counters

  • Interrupt structure: two levels of priority, five interrupt sources

  • One full-duplex serial port

  • Bit addressing (ability to access the content of a specific bit), suitable for bitwise logical operations. In addition to the 128-byte RAM, 4k-byte ROM, and interrupt, serial port, and timer modules, there are also 4 sets of I/O ports P0 to P3, and the remaining components constitute the entire CPU. Replacing the 4k ROM with EPROM results in the structure of the 8751, removing the ROM/EPROM portion gives the block diagram of the 8031, and replacing the ROM with Flash memory or EEPROM, or removing certain I/O, gives rise to derivative variants of the 51 series, such as the 89C51, AT89C2051, etc.

Service hotline

+86 0755-83044319

Hall Effect Sensor

Get product information

WeChat

WeChat