Common questions for microcontroller beginners.[Copy link]
Introduction to Single Chip Microcomputers It can be said that the 20th century spanned three "electrical" eras, namely the electrical era, the electronic era, and the computer era that has now arrived. However, this type of computer usually refers to a personal computer, referred to as a PC. It consists of a host, a keyboard, a display, etc. There is another type of computer that most people are not very familiar with. This type of computer is a single-chip microcomputer (also known as a microcontroller) that gives intelligence to various machines. As the name suggests, the smallest system of this computer uses only one integrated circuit to perform simple calculations and controls. Because of its small size, it is usually hidden in the "belly" of the controlled machine. It plays a role like a human brain in the entire device. If it goes wrong, the entire device will be paralyzed. Now, this type of single-chip microcomputer has been used in a wide range of fields, such as intelligent instruments, real-time industrial control, communication equipment, navigation systems, household appliances, etc. Once various products use single-chip microcomputers, they can play the role of upgrading products, and the adjective "intelligent" is often prefixed to the product name, such as intelligent washing machines. At present, some products made by technicians in some factories or other amateur electronic developers are either too complicated in circuit or too simple in function and can be easily copied. The reason may be that the products do not use single-chip microcomputers or other programmable logic devices. Is it difficult to learn single-chip microcomputers? It should be said that for readers who already have basic knowledge of electronic circuits, especially digital circuits, it will not be too difficult. If you have a certain foundation in PC, it will be easier to learn single-chip microcomputers. In order to enable most readers to use single-chip microcomputers. Here we will try our best to give readers the basic working principles and usage methods of single-chip microcomputers according to the principles of easy to understand, concise, and combining theory with practice, so as to achieve the purpose of leading everyone into the "door" of single-chip microcomputers. However, single-chip microcomputers, like PCs, are a very practical technology. Some people say that "computers are played out", and single-chip microcomputers are the same. Only by "playing" more, that is, more practice and more actual operation, can you really master it. Therefore, this lecture will provide various exercises and experiments, and introduce some single-chip microcomputers and development systems that are suitable for beginners and have a high cost-effective supply. Only by completing these practical links can you lay a solid foundation for further study. "It is not difficult to get started, and it is possible to study further." As long as you have perseverance and determination, follow our "serialization" step by step, you will definitely be able to travel in the world of single-chip microcomputers in the future. The composition of single-chip microcomputers. What are the most important parts of single-chip microcomputers to automatically complete calculations? Let's use an abacus as an example to calculate an arithmetic problem. Example: 36 + 163 × 156 - 166 ÷ 34. Now to perform calculations, we first need an abacus, followed by paper and pen. We write down the problem to be calculated, and then the first step is to calculate 163 × 156, add it and 36 and write the result on paper, then calculate 166 ÷ 34, and then subtract it from the previous result to get the final result. Now, we use a single-chip microcomputer to complete the above process. Obviously, it first needs a component that can replace the abacus for calculation, which is the "calculator"; secondly, it needs a device that can play the role of paper and pen, that is, it can remember the original questions, original data and intermediate results, and also remember the various commands compiled to enable the single-chip microcomputer to automatically perform calculations. This type of device is called "storage". In addition, a controller that can replace the role of people is also needed. It can send various control signals according to the commands given in advance, so that the entire calculation process can proceed step by step. However, these three parts are not enough. The original data and commands must be input, and the calculation results must be output. They all need to be done in sequence, and sometimes they need to wait. As in the above example, when calculating 163×156, the number 36 cannot enter the calculator at the same time. Therefore, it is necessary to set a "gate" on the single-chip microcomputer that acts according to the controller's command, and let new data enter when the calculator needs it. Or, when the calculator gets the final result, it will output this result, and the intermediate results cannot "slip out" of the single-chip microcomputer at will. This kind of "gate" circuit that manages input and output data is called a "port" in the single-chip microcomputer. In the single-chip microcomputer, there are basically three types of information flowing. One type is data, namely various original data (such as 36, 163, etc. in the above example), intermediate results (such as the quotient 4 and remainder 30 obtained by 166÷34), and programs (a collection of commands). In this way, the external device enters the single-chip microcomputer through the "port" and is then stored in the memory. During the calculation process, the data is read from the memory into the arithmetic unit for calculation. The intermediate results of the calculation are stored in the memory, or finally output by the arithmetic unit through the "port". The various commands (programs) that the user wants the single-chip microcomputer to execute are also sent to the controller from the memory in the form of data, and the controller interprets (decodes) them into various control signals to execute various commands such as addition, subtraction, multiplication, and division. Therefore, this type of information is called control command, that is, the controller controls the arithmetic unit to perform calculations and processing step by step, and controls the reading (taking out data) and writing (storing data) of the memory. The third type of information is address information, which tells the arithmetic unit and controller where to get commands and data, where to store the results, and through which port to input and output information. The memory is divided into read-only memory and read-write memory. The former stores debugged fixed programs and constants, and the latter stores some data that may change at any time. As the name implies, once the read-only memory stores data, it can only be read out and cannot be changed (EPROM, E2PROM and other types of ROM can be changed and written through certain methods - Editor's note). Read-write memory can store or read data at any time. In fact, people often combine the arithmetic unit and the controller as the central processing unit - CPU. In addition to calculation, the single-chip microcomputer also needs to complete the control function. Therefore, counting and timing are indispensable. Therefore, a timer and counter are set up in the single-chip microcomputer, and its basic structure is similar to the example in (II) of this series. So far, we have known the basic composition of the single-chip microcomputer, that is, the single-chip microcomputer is composed of a central processing unit (i.e., the arithmetic unit and controller in the CPU), read-only memory (usually represented as ROM), read-write memory (also known as random access memory, usually represented as RAM), input/output port (also divided into parallel port and serial port, represented as I/O port), etc. In fact, there is also a clock circuit inside the single-chip microcomputer, which enables the single-chip microcomputer to perform calculations and control in a rhythmic manner. In addition, there is also a so-called "interrupt system", which has the function of a "communication room". When the parameters of the object controlled by the single-chip microcomputer reach a state that requires intervention, it can be notified to the CPU through this "communication room", so that the CPU can take appropriate coping measures according to the severity of the external situation. Now that we have known the composition of the single-chip microcomputer, the remaining question is how to connect their various parts into an interconnected whole? In fact, there is a "link" inside the single-chip microcomputer that connects them, namely the so-called "internal bus". This bus is like the "main road" of a big city, and the CPU, ROM, RAM, I/O port, interrupt system, etc. are distributed on both sides of this "bus" and connected to it. Therefore, all instructions and data can be transmitted through the internal bus, just like the transmission of various items in a big city is carried out through the main road. The instruction system and assembly language program of the single-chip microcomputer have been described above. Several main components of the single-chip microcomputer constitute the hardware of the single-chip microcomputer. The so-called hardware (Hardware) is a visible and tangible entity. However, such hardware alone only has the possibility of realizing calculation and control functions. For the single-chip microcomputer to truly be able to perform calculations and controls, it must also be coordinated with software (Software). Software mainly refers to various programs. Only when various correct programs are "injected" (stored) into the single-chip microcomputer can it work effectively. The reason why the single-chip microcomputer can automatically perform calculations and controls is that people store the steps of realizing calculations and control step by step in the form of commands, that is, instructions (Instructions) in advance into the memory. Under the control of the CPU, the single-chip microcomputer takes out the instructions one by one, translates and executes them. Take the simple operation of adding two numbers as an example. After the numbers to be calculated have been stored in the memory, the following steps are required: Step 1: Take the first number from its storage unit (Location) and send it to the operator. The second step: take the second number out of the storage unit where it is located and send it to the operator; the third step: add; the fourth step: send the result of the addition to the specified unit in the storage.
All these fetching, sending, adding, storing, etc. are an operation. We write down the various operations required by the computer in the form of commands, which are instructions. But how can we identify and execute these operations? This is determined by the instruction system given to it by the designer when designing the single-chip microcomputer. An instruction corresponds to a basic operation; all the instructions that the single-chip microcomputer can execute are the instruction system (Iustruction Set) of the single-chip microcomputer. Different types of single-chip microcomputers have different instruction systems. When using a single-chip microcomputer, the problem to be solved should be compiled into a series of instructions in advance. These instructions must be instructions that the selected single-chip microcomputer can recognize and execute. The instruction program compiled by the single-chip microcomputer user to solve his own problems is called the source program. Instructions are usually divided into two parts: opcode and operand. The opcode indicates what operation the computer performs, that is, the function of the instruction; the operand indicates the number participating in the operation or the address where the operand is located (that is, the number of the place where the operand is stored). Because the single-chip microcomputer is a programmable device, it only "recognizes" binary code (0, 1). For the single-chip microcomputer to 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 taking a number from the memory to the accumulator in the CPU (in the arithmetic unit, a special register that participates in the operation and stores the operation results) is 74H, the code for adding the immediate number to the accumulator content is 24H, and the code for adding the immediate number code to the accumulator to send the number to the internal RAM memory is F6H~F7H, etc. These instructions are binary machine codes represented in hexadecimal. The word length of the MCS-51 single-chip microcomputer is 8 bits. Sometimes, one byte cannot fully express certain operations. Therefore, there are single-byte instructions and multi-byte instructions in the instruction system. Machine code is composed of a series of 0s and 1s. It has no obvious characteristics, is difficult to remember, understand, and is prone to errors. Therefore, it is very difficult to write programs directly with it. Therefore, people use some mnemonics (Mue monic) - usually the English abbreviations of the instruction function to replace the operation code, such as MOV (abbreviation of Move) is often used for number transmission in MCS-51, and Add (abbreviation of Addition) is used as a mnemonic for addition. In this way, each instruction has obvious action characteristics, is easy to remember and understand, and is not prone to errors. Programs written with mnemonics are called assembly language programs. However, programs written with mnemonics are easy for people to understand, but the microcontroller only recognizes binary machine code. Therefore, in order for the microcontroller to "read" the assembly language program, it must be converted into a program composed of binary machine code. This conversion process is called "assembly". Assembly can be achieved with the help of manual table lookup method, or it can be completed with the help of a PC through the so-called "cross assembler". Once the user program composed of machine code "enters" the microcontroller and then "starts" the microcontroller, it can be made to perform the tasks specified by the input program. CPU and memory of MCS-51 CPU structure The CPU of the 8051 microcontroller consists of an arithmetic unit and a controller. 1. Arithmetic unit The arithmetic unit is based on the ALU, which is a binary arithmetic/logic operation unit, plus 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, which 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 addend) before the operation, and saves its result (such as algebraic sum) after the operation. Register B is mainly used for multiplication and division operations. The flag register PSW is also an eight-bit register, which is used to store some characteristics of the operation result, such as whether there is a carry or borrow. The specific meaning of each bit is as follows. PSW CY AC FO RS1 RS0 OV - P For users, the following four bits are of most concern. 1? Carry flag CY (PSW?7). It indicates whether there is a carry (or borrow) in the operation. If the operation result has a carry (addition) or borrow (subtraction) in the highest bit, this bit is 1, otherwise it is 0. 2? 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 the two eight-digit operations, that is, whether there is a carry (or borrow) when the lower four bits are added (or subtracted). If so, AC is in the 1 state, otherwise it is 0. 3? Overflow flag OV. MCS-51 reflects whether the operation result of the signed number overflows. If there is an overflow, this bit is 1, otherwise it is 0. 4? Parity flag P. Reflects the parity of the content of the accumulator ACC. If the operation result in ACC has an even number of 1s (such as 11001100B, which has 4 1s), P is 0, otherwise, P=1. The other bits of PSW will be introduced later. Since PSW stores the status of the program execution, it is also called the program status word. There is also a logic processor (also known as a Boolean processor) in the operator that performs logical operations on a bit basis. Its function will be explained when introducing bit instructions. 2. Controller The controller is the nerve center of the CPU. It includes timing control logic circuit, instruction register, decoder, address pointer DPTR, program counter PC, stack pointer SP, etc. Here, the program counter PC is a counter composed of 16-bit registers. If the microcontroller wants to execute a program, the program must be pre-loaded into a certain area of the memory ROM in sequence. When the microcontroller is in action, it should take out instructions one by one in sequence to execute them. Therefore, there must be a circuit that can find the unit address where the instruction is located. This circuit is the program counter PC. When the microcontroller starts to execute the program, the address of the first instruction is loaded into the PC. Every time it takes out an instruction (if it is a multi-byte instruction, every time it takes out an instruction byte), the content of the PC is automatically increased by 1 to point to the address of the next instruction, so that the instructions can be executed sequentially. Only when the program encounters a transfer instruction, a subroutine call instruction, or an interrupt (which will be introduced later) will the PC go to the required place. 8051 CPU takes the instruction byte from the corresponding unit of ROM and stores it in the instruction register according to the address specified by C. Then, the instruction code in the instruction register is translated into various forms of control signals by the decoder. These signals are combined with the clock pulses generated by the microcontroller clock oscillator in the timing and control circuit to form a level and clock that changes at a certain time, that is, the so-called control information, which coordinates the data transmission, calculation and other operations between registers in the CPU.
Level: Forum Ranger Articles: 58 Points: 131 Registered: 2003-07-19
5th Floor
The big brother above, you know whether you wrote it yourself or copied it. Generally speaking, thank you for your hard work. People who really want to learn MCU well will not ask questions often, especially some common sense questions. Find a book and read it for half a day, and the basic structure and common functions of MCU will be clear at a glance. I would like to remind all my friends who are new to MCU to read the book carefully. I hope you can add your own experience in the learning process. Don’t ask common sense questions here. I think the experience of discussing the design and development process in the forum is the kind of experience that cannot be found in books. Here is a website for everyone to download electronic books from, all about MCU. I hope you will all become masters. http://218.7.17.204/chaoxing/5.htm