The architecture and principle of 51 microcontroller (STC89C51)

Preface

        Learning microcontrollers, no matter how you learn, will always take some detours. Some people take more detours, and some people take fewer detours. Some people give up halfway, and many people successfully come ashore and master all kinds of skills. Microcontrollers and processors. It is most appropriate to use the 51 microcontroller to learn, because its structure is not complicated and it has everything it needs. Once you learn it, it will be much easier to learn other microcontrollers. Many people will use microcontrollers to light up LEDs and digital tubes. If they can use microcontrollers for serial communication, and if they can use microcontrollers for various sensors and drivers, they feel they are proficient. In fact, this is not enough. It takes a long time to learn to change a microcontroller, and it is even more difficult to change a processor. To truly learn a microcontroller, you need to understand the internal structure and principles of the microcontroller, so that you can succeed all over the world with one trick. Because whether it is a 51-architecture microcontroller, a Cortex-M series microcontroller, or a Cortex-A series processor used on a mobile phone, or a processor used on a PC, the basic principles are not much different. Just like houses, there are skyscrapers, factories, apartments, warehouses, commercial housing, resettlement housing, villas, and toilets. They are all made of steel and cement, with doors, windows, rooms, and large rooms. , small rooms, there are fine decoration and rough decoration. The basic things are the same, but the differences in use are different.

What is a microcontroller

The full English name of single-chip microcomputer is Single-Chip Microcomputer, also called single-chip microcomputer. If you think about the main components of a computer (CPU, memory, hard disk, and motherboard), then the single-chip microcomputer integrates CPU, memory, hard disk, and motherboard on one chip. The CPU is still The CPU, memory is RAM, the hard disk is ROM (the ROM of the microcontroller is also called Flash), and the motherboard is the various bus connections on the microcontroller. Computers can also provide some external interfaces, such as mice, keyboards, monitors, and various USB interfaces. Microcontrollers can also provide various external interfaces, namely those GPIOs. Some GPIOs have special functions, such as UART, SPI, IIC, etc.

So the structure of the microcontroller can be as follows. The CPU interacts with ROM, RAM, and GPIO through the bus.

CPU: It is the core part of the microcontroller and is responsible for executing program instructions, data processing, and controlling various peripherals.                     

Decode instructions and execute instructions: The CPU reads instructions from program memory (Flash), decodes them, and executes them;     

Control data flow: The CPU is responsible for controlling the flow of data on the internal bus and transferring data to internal registers and peripherals

Management of interrupts: The CPU can detect external interrupt signals and process interrupt requests according to priority;                                     

Control timing and clock: The CPU needs to synchronize the operation of each component according to the clock signal to ensure the normal operation of the system 

Processing input and output operations: The CPU can communicate with external devices through input and output ports to achieve control and data exchange of peripherals.

ROM: The ROM of the 51 microcontroller is used to store codes and constant data, including program instructions, constants, initial values ​​of variables, fixed data tables, etc. In the 51 microcontroller, the ROM is not writable and can only be burned. After the microcontroller is powered on, the program code and constant data will be loaded from ROM into RAM for running.

RAM: The RAM of the 51 microcontroller is a temporary memory used to store programs and data. RAM is divided into internal RAM and external RAM. The internal RAM is located inside the chip and is mainly used to store variables and data, heap, stack, etc. required when the program is running. Internal RAM is generally smaller, 128 bytes, 256 bytes, etc. External RAM needs to be connected to the chip through an external interface, which can expand the storage space of the microcontroller. The size of external RAM is generally between a few K bytes to dozens of K bytes.

GPIO: The GPIO (General Purpose Input and Output Port) of the 51 microcontroller is the most basic IO port, which can control external devices and input and output data through programming. Common applications include LED light control, buzzer control, button detection, digital tube display, LCD display, etc.

51 microcontroller CPU

The CPU of the 51 microcontroller is mainly composed of these parts: ALU, special function register, instruction register, instruction decoder, program counter (PC), and clock circuit.

ALU (Arithmetic Logic Unit): ALU is the core component that handles all arithmetic and logical operations in the CPU. It includes adders, logic gates, shifters, etc.

Special function register:

Program Counter (PC): The program counter is a 16-bit register that stores the address of the next instruction. When the 51 microcontroller executes an instruction, the PC register will automatically +1 or +2 according to the length of the instruction to point to the address of the next instruction. When a jump instruction is encountered, the value of the PC register will be directly modified to the one to be jumped. address.

Instruction register (IR): The instruction register is a register specially used to store the currently executed instruction. When the 51 microcontroller executes an instruction, the instruction is read from the program memory into the instruction buffer and then transferred to the instruction register. In the instruction register, the instruction is decomposed into opcodes and operands, and then sent to the instruction decoder for decoding, and finally the corresponding operation is performed.

Instruction decoder (ID): used to decode the instructions in the instruction register into corresponding operations and control the execution of the corresponding execution instructions. In the 51 microcontroller, the instruction decoder usually consists of a microinstruction controller and a microprogram memory. Its main work is to decode information such as operation codes and operands into microinstructions, and send the microinstructions to the execution unit to realize the instruction. implement.

Clock circuit: provides clock signal to CPU

ROM of 51 microcontroller

The ROM of the 51 microcontroller is divided into three areas: code area, data area, and expansion area.

Code area: Also called program area, it is used to store code and is the main part of the program. The size of the code area is determined by the capacity of the ROM, generally 4KB, 8KB, or 16KB;

Data area: used to store constants

Extension area: used to store areas that can be accessed

51 microcontroller RAM

The RAM of the STC89C52 series microcontroller is divided into two parts, one is internal RAM (256 bytes), and the other is internal extended RAM. The size depends on the manual.

Internal 256 bytes RAM, address range is 0x00--0xFF,

The 256-byte RAM is divided into the lower 128-byte internal RAM (0x00--0x7F) and the upper 128-byte internal RAM (0x80--0xFF);

This lower 128 bytes of RAM can be addressed directly or indirectly;

The address range of the upper 128-byte RAM and the special function register are the same, both 0x80--0xFF. They look the same, but they are actually different because their addressing methods are different. The upper 128-byte RAM can only Indirect addressing, special function registers can only be directly addressed. How to understand? Just take the college entrance examination as an analogy. Just like the college entrance examination scores in Henan and Beijing, they are both 500 points. Those in Beijing can go to 985, but those in Henan can only go to 985. You can go to a junior college because their admission system is different, that is, their addressing methods are different.

The lower 128-byte RAM is also called the general RAM area and is the most frequently used RAM area.

The RAM area can be divided into:

Working register group area : address from 00H--1FH, 32 bytes, divided into 4 groups, each group contains 8 8-bit working registers, numbers are R0--R7, 4 register groups are provided because 1 One register group may not be enough. You can decide which working register group to use through the combination of RS1 and RS0 in the program status word register PSW.

Bit addressable area: address 20H--2FH, 16 bytes in total. The so-called bit addressable means that one BIT can be read and written one BIT.

User RAM area and stack area: Address 30--FF, an 8-bit stack pointer (SP), used to point to the stack area. After the microcontroller is reset, the stack pointer SP is 07H, pointing to R7 in working register group 0, so The user initialization program should set the initial value of the SP. Generally, the unit is obtained after 80H, that is, it is set in the lower 128 bytes of RAM area.

51 microcontroller special function register (SFR)

The special function registers of the 51 microcontroller include the following, and the address space is distributed between 0x80--0xFF:

1. P0, P1, P2, P3: port registers, used to control the input and output status of the chip;

2. ACC: Accumulator register, used to store operation results or data transmission;

3. B: B register, used to store the second operand of the operation;

4. PSW: Program status word register, used to store status information of program execution, including flag bits, carry, etc.;

5. SP: Stack pointer register, used to store the address of the stack;

6. DPL, DPH: Data pointer registers, used to store the address of data;

7. IE and IP interrupt enable and interrupt priority registers are used to control the enable and priority of interrupts;

8. TMOD, TCON, TH0, TL0, TH1, TL1: timer/counter mode register, counter register, used for timer and counter control;

9. SCON, SBUF: Serial port control register and data buffer register, used for serial port communication control and data transmission;

10. Other series of microcontrollers will also expand corresponding registers according to functions, such as ADC, PCA

The address allocation of the special function registers of the conventional 51 microcontroller is as follows

The special function registers and address distribution of STC89C52RC can be found from the provided header file


sfr  XICON = 0xc0;
sfr  WDT_CONTR = 0xe1;

sfr ISP_DATA  = 0xe2;
sfr ISP_ADDRH = 0xe3;
sfr ISP_ADDRL = 0xe4;
sfr ISP_CMD   = 0xe5;
sfr ISP_TRIG  = 0xe6;
sfr ISP_CONTR = 0xe7;

sfr P0   = 0x80;
sfr P1   = 0x90;
sfr P2   = 0xA0;
sfr P3   = 0xB0;
sfr P4   = 0xe8; //STC自己扩展了P4端口,标准的51单片机是只有P0,P1, P2, P3端口

sfr PSW  = 0xD0;
sfr ACC  = 0xE0;
sfr B    = 0xF0;
sfr SP   = 0x81;
sfr DPL  = 0x82;
sfr DPH  = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0  = 0x8A;
sfr TL1  = 0x8B;
sfr TH0  = 0x8C;
sfr TH1  = 0x8D;
sfr IE   = 0xA8;
sfr IP   = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;

/*  STC自己扩展的寄存器 */
sfr AUXR   = 0x8E;
sfr AUXR1  = 0xA2;
sfr SADDR  = 0xA9;
sfr IPH    = 0xB7;
sfr SADEN  = 0xB9;
sfr T2CON  = 0xC8;
sfr T2MOD  = 0xC9;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2    = 0xCC;
sfr TH2    = 0xCD;




51 microcontroller architecture

The picture below is the STC89C51 architecture cut out from STC's official manual, and all the included modules are included.

The relationship between TMP1, TMP2 and ACC

The TMP register and the ACC register are both accumulator registers, but their uses are different.

The TMP register is a temporary register used to store temporary results during the calculation process, such as intermediate results of calculations, or temporary storage of some operations. The TMP register is not a general purpose register, so it cannot be accessed directly.

The ACC register is an accumulator register, which is different. It is a general-purpose register that can be directly accessed by the program. The ACC can store the results of addition, subtraction, multiplication, division, shift and other operations, and can also be used to store data and addresses.

What is an address generator?

The address generator refers to the internal memory address generation circuit, which is composed of several registers and logic gates. The address generation circuit generates the address of the internal memory through the following steps:

1. The program counter (PC) points to the address of the next instruction to be executed;

2. The instruction register (IR) reads the instruction pointed to by the PC;

3. According to the operation code of the instruction, the length of the topping instruction is determined;

4. Add the value of PC to the length of the instruction to get the address of the next instruction;

For data memory, the data register (DPTR) can be used to generate the address. This data register is a double data pointer.

How does 51 microcontroller fetch instructions?

51 microcontroller instruction fetching steps:

1. Send the value of the program counter (PC) to the address bus, so that the address stored in the program counter is used as the address of the address bus;

2. Send the address stored in the program counter to the address port of the program memory to select the corresponding storage unit;

3. Read instructions from the program memory and send them to the data bus;

4. Send the instruction from the data bus to the instruction register;

5. Set the program counter +1 to point to the next instruction;

6. Repeat the above process until the program execution ends

How does the program of 51 microcontroller run?

The program of the 51 microcontroller is compiled by the compiler into a file executable by the microcontroller, such as a hex format file or a bin format file;

Use a burner to burn (download) the executable file into the Flash of the microcontroller, that is, the ROM;

After power-on, the 51 microcontroller CPU starts reading instructions from address 0 of Flash;

Perform read and write operations on registers and memory according to instructions;

51 Microcontroller operations before main function

In fact, after the microcontroller is powered on, it does not directly perform various operation instructions in the main function. There are some initialization operations before entering the main function, including:

1. Clear all RAM variables, initialize all registers, and set all IO ports to the default state;

2. Enable interrupts, initialize the interrupt vector table, and set the interrupt priority;

3. Configure the system clock;

4. Initialize various peripherals;

5. Initialize the stack pointer;

These initialization operations are generally completed by the startup code provided by the compiler or development environment.

What is the stack pointer and what is it used for?

There is a stack pointer register (SP) among the special function registers, which is used to point to the address of the top of the stack. When a CALL instruction or interrupt is encountered during program execution, the value of the program counter (PC) will be pushed into the stack, and the value of the SP register will be -1 to point to the next available address of the stack. When returning, the pushed PC value will be taken from the stack, and the value of the SP register will be +1 to point to the top of the stack.

The role of the stack

Stack, English stack, is generally translated as stack, because there is still a certain difference between heap and stack. In computers, heap and stack are both parts of the memory. The stack is generally allocated by the CPU itself, while the heap needs to be applied for by the programmer himself. To release.

In the 51 microcontroller, the stack is used to save context. When an interrupt occurs, the current values ​​in the program counter (PC), status register (PSW), and other general registers are saved in the stack. After the interrupt service routine ends, the contents of the stack are read out.

When using the stack to save context, you need to pay attention to the following points:

1. Ensure that the stack is large enough to save all register values ​​that need to be saved;

2. Before entering the interrupt, other interrupts need to be prohibited from occurring to prevent other interrupts from occurring during the execution of the interrupt service routine, thereby destroying the value saved in the stack;

3. After exiting the interrupt service routine, interrupts need to be re-enabled so that they can respond to other interrupt requests;

4. When using multiple nested interrupts, multiple stacks need to be used to save the context of different interrupts.

What is the interrupt vector of 51 microcontroller?

The interrupt vector of the 51 microcontroller is a set of addresses. This address is the address where the program instructions are stored in the microcontroller.

The program memory is Flash. Its address starts from 0x0000 and gradually increases according to the amount of code. However, there are some special address locations that cannot be occupied by general program instructions. These special locations are interrupt vectors. Once an interrupt occurs, the program The counter (PC) jumps directly to the corresponding interrupt vector address and starts executing interrupt service.

The conventional 51 microcontroller has the following interrupt vectors

  1. External interrupt 0 (INT0): The interrupt vector address is 0x0003H
  2. External interrupt 1 (INT1): The interrupt vector address is 0x0013H
  3. Timer/Counter 0 Interrupt (Timer0): The interrupt vector address is 0x000BH
  4. Timer/Counter 1 Interrupt (Timer1): The interrupt vector address is 0x001BH
  5. Serial port interrupt (UART): The interrupt vector address is 0x0023H

The STC microcontroller has expanded several other interrupts. You can check the manual when using them. Generally, the address of the interrupt vector is located in the address range 0x0000--0x003F of the program memory.
During the program development process, we can write the entry address of the interrupt service subroutine into the interrupt vector address through specific instructions. When the corresponding interrupt event occurs, the CPU will jump to the corresponding entry address according to the entry address stored in the interrupt vector. Interrupt service routine.

おすすめ

転載: blog.csdn.net/m0_49968063/article/details/130783225