Our Latest News

How the program runs in the 80C51 microcontroller

We want to understand how the microcontroller runs the program, we first need to understand the composition of the microcontroller, we take 80C51 microcontroller as an example here to understand how the program runs in the microcontroller.

The composition of the microcontroller

The internal hardware structure of the 8051 microcontroller consists of

Central processing unit CPU: It is the core component inside the microcontroller, which determines the main functional characteristics of the microcontroller and consists of two major parts, the operator and the controller.

This image has an empty alt attribute; its file name is image-137.png

Memory: 8051 microcontroller in the system structure of the Harvard type, the program and data are stored in two memories, one called program memory, the other for data memory in the physical structure of the program memory and data memory, there are four physically independent of each other storage space, namely, on-chip ROM and off-chip ROM, on-chip RAM and off-chip RAM.

Timer / counter (T / C): 8051 microcontroller has two 16-bit timer / counter, each T / C can be set to both counting mode, can also be set to timing mode, and its timing count results to the computer control.

Parallel I/O ports: The 8051 has four 8-bit parallel I/O interfaces (P0 to P3) to achieve parallel input and output of data.

Serial port: The 8051 microcontroller has a full-duplex serial port for serial communication between the microcontroller and the microcontroller or other devices.

Interrupt control system: 8051 has a total of five interrupt sources, non-advanced and low-level it can receive external interrupt applications, timer / counter applications and serial port applications, commonly used in real-time control, automatic fault handling, computer and peripherals between the transfer of data and human-computer dialogue.

Microcontroller startup process

The start-up process of the microcontroller is to run the internal program of the chip (this program is not accessible to the user and can not be rewritten), i.e. the start-up code, after the power is applied. After the start-up code program has established the operating environment, it will read the serial port status, that is, each port used by the user to download the program, to determine whether the user is using the port ready to download the program.

If yes, it will download the user program to the specified address as requested by the user. If not, it jumps to the entry of the user program that has already been downloaded, thus giving control of the chip to the user program. If it is a new chip that has not been downloaded yet, then it stays in the loop of reading the serial port status.

The boot code is usually burned into flash, which is a program that is executed as soon as the system is powered on, and it runs before any user C code. After power-up, the arm processor is in the arm state, running in management mode, while all system interrupts are disabled and the PC goes to address 0 to fetch instructions for execution.

An executable image file must have an entry point, and the entry address of an image file that can be placed at the start of rom must also be set to 0. In assembly language, you can define your own entry point for a program, and when there are multiple entry points in a project, you need to indicate the entry point of the program with -entry in the connector.

If the user creates a program that contains a main function, there will also be an entry point corresponding to the C library initialization code. In general, the startup code accomplishes two main tasks: one is to initialize the execution environment, such as interrupt vector table, stack, I/O, etc.; the other is to initialize the c library and the user application.

In the first stage, the process of starting the code can be described as follows

Creation of the interrupt vector table.

Initializing the memory.

Initializing the stack registers.

Initializing the i/o and other necessary devices.

Changing the state of the processor as needed.

PC computers these devices with system at power-up, and microcontroller processing is similar, except that they are read BIOS, have it completed a lot of initialization operations, and finally, call the system initialization function, the control to the operating system, so we see the Windows, Linux system started.

If you think of the operating system as a very large bare-metal program running on the processor (that is, a program that runs directly on the hardware, because the operating system is running directly on the CPU), then the operating system boots much like the MCU program boots. The former has a very large initialization program to complete a very complex initialization, and the latter has a short piece of assembly code to complete some simple initialization.

What if the programs on the system start up? They are determined by the system. On Linux, after typing . /If not, the shell assumes that it is an executable file (usually in elf format on Linux) and then calls some related functions to copy the contents of the p-file on the hard disk into memory (DDR RAM) and establish a running environment for it (of course there is memory mapping, virtual memory, linking and loading, and some other things here). The function is ready for execution.

From the above, it can be seen that the program on the microcontroller and the usual program running on the system, the difference in the start-up is very large, if the program call main before the action, are abstracted as initialization, the program can be simplified to start: to establish the runtime environment + call main function, so that the program execution is not very different.

Because the program running on the microcontroller (bare metal program), is the same as the operating system running on the hardware, they belong to a level. The reason why some differences between programs on microcontrollers and programs on PCs were not distinguished in the past is that this point was not understood.

Program execution

When the program is executed, from where to read instructions and where to read data, there was also a long time of confusion because the difference between the program on the system and the bare-metal program was not clear.

The running process of a program in a microcontroller is divided into several steps: fetching instructions, analyzing instructions and executing instructions.

The task of fetching instruction is: read the current instruction from the program memory according to the value in the program counter PC to the instruction register.

The task of the analyze instruction stage is: to take out the instruction operand from the instruction register and decode it, and analyze its instruction nature. If the instruction requires an operand, the operand address is searched for.

The process of executing a computer program is actually repeating the above operation process instruction by instruction until it encounters a stop instruction to cycle through the waiting instructions.

Although in the “Principles of Microcomputers” class know that when the program runs, read from memory instructions and data for execution and write back. But only a few K of RAM on the microcontroller, and flash generally have tens of K or even 1M, this time instructions and data are in the memory?

The memory referred to here refers only to RAM, because on the PC we often say that the memory is DDR RAM memory, so preconceived that the microcontroller is also the same, have not understood that in fact RAM and Flash are both memory.

This is impossible, because the teacher in class only said memory, but memory on the PC is generally DDR RAM, will not be the hard disk, the hard disk is where the data is saved; thus the analogy, when you make yourself dizzy, the RAM of the microcontroller corresponds to DDR RAM, then Flash does not correspond to the hard disk? Understood on CSAPP, the reason why the PC is all on DDR RAM is the speed factor.

The speed of the hard disk is too slow, even the upcoming SSD is still several orders of magnitude worse than DDRRAM, so copy to DDRRAM. At this point, the code and data of a program are stored consecutively, where the code segment is a read-only area and the data segment is a read-write area (this is determined by the memory management mechanism of the operating system).

At runtime, they are then copied to the faster SRAM to get faster execution speed. And for, the operating frequency of the microcontroller is only a few M, tens of M, the difference between reading from Flash and from RAM may not be obvious, will not become a bottleneck in the execution of the program (and for the PC, the speed of Flash is too slow, DDRRAM is also very slow, even SRAM is also a lot slower, so then increase the operating frequency can not improve the execution speed of the program, so Now the fastest CPU operating frequency is around 2003, a bottleneck emerged.

As an example

At power-on, the program calculator PC changes to 0000H and the microcontroller automatically enters the process of executing the program under the action of the timing circuit. The execution process is actually a cyclic process of taking out instructions (taking out the instructions stored in memory in advance) and executing them (analyzing and executing them).

For example, the execution instruction: MOV A,#0E0H, whose machine code is 74H E0H, the function of the instruction is to send the operand E0H into the accumulator, 0000H cell has been stored in 74H, 0001H cell has been stored in E0H. When the microcontroller starts to run, the first thing is to enter the take out finger stage, the order of which is.

The content of the program counter (this time is 0000H) is sent to the address register;

The content of the program counter is automatically added by 1 (becomes 0001H);

The content of the address register (0000H) is sent to the memory through the internal address bus, and the unit with address 0000H is selected with the address decoding in the memory;

The CPU makes the read control line active;

The contents of the selected memory cell (74H at this time) are sent to the internal data bus under the read command control, and the contents are sent to the instruction register through the data bus because it is the fetch finger stage.

Multi-threaded execution program

In order to increase the CPU usage, think about it another way, since we can’t reduce the execution time of one program, we can execute more programs in the same time, one core executes one program, two cores can execute two programs, so multi-core CPUs become the mainstream nowadays).

So the bare-metal program instructions are stored in Flash (Flash memory), while the data is placed in RAM (flash has a limit on the number of writes, and its speed is still much worse than RAM). More broadly speaking, on a microcontroller RAM stores data segments, bss segments, stack segments; ROM (EPROM, EEPROM, Flash and other non-volatile storage devices) stores code, read-only data segments.

Essentially, this and the PC program are stored in the RAM is the same, the PC is the operating system provisions can be read and write, while the microcontroller is dependent on different storage devices to distinguish between read and write (of course, now the Flash is read and write, if the Flash is not written to limit the number of times, and the speed can be similar to RAM, the microcontroller is not as long as the Flash can be It (directly equivalent to DDRRAM on the PC)? This cost will also be lower than a RAM, a Flash, more cost effective, more cost-effective for manufacturers).

Data storage and reading

For the storage and reading of instructions and data during the execution of the program of the microcontroller, the following is understood.

After programming the microcontroller, the code segment, data segment, bss segment, rodata segment, etc. of the program are stored in the Flash. When the microcontroller is powered on, the initialization assembly code copies the data segment, bss segment, into RAM and builds a good stack to start calling the main function of the program.

After that, there is a program memory, and data memory, runtime from the Flash (that is, instruction memory, code memory) to read instructions, from the RAM to read and write data.

Whether it is a microcontroller or a PC, the memory pyramid that exists is the same, the speed factor, the cost constraints lead to a level of faster memory faster speed and higher cost. It should be said that the understanding of them is the understanding of the memory pyramid.


    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663