First, the role of ROM, RAM and Flash in single chip
ROM - Store the curing program (stored instruction code and some fixed value, the program is not changed after running)
C Files and HCs in the H file, global variables, local variables, constant data defined by the 'const' definition, code in the Startup.asm file (similar to the bootloader in the ARM, or BIOS in X86, some low-end microcomputer is No this) is stored in the ROM.
RAM - Random Access (Power Data Disappearance) of the data in the program is running in the entire program, which is stored in the RAM, "Changed amount" includes global variables, local variables, Stack section.
Flash - stores user programs and data that requires permanent saved.
For example: The electronic electricity meter of household now, its kernel is a single-chip microcomputer, the program of the microcontroller is stored in the ROM. The electric meter is in the work process, and the voltage and current to be acquired, and the current is calculated based on the voltage and current. A time-time data when the voltage and current, the user is not concerned, it is only used to calculate the electricity, after calculating the data collected, then the next time, then the next time, so these values are not necessary to store, Place it in the RAM. However, the time calculated, it is necessary to be preserved, the single-chip chance timing or instantaneously puts the electricity number in the flash in the moment.
Conventional ROM is used to store curing programs, and RAM is used to store data. Since Flash ROM is fast than ordinary ROM read and write speed, it is convenient to store user programs and data that requires permanent saving. For example, the electronic electricity meter of home now, its kernel is a single-chip microcomputer, the program of the microcontroller is stored in the ROM. The electric meter is in the work process, and the voltage and current to be acquired, and the current is calculated based on the voltage and current. A time-time data when the voltage and current, the user is not concerned, it is only used to calculate the electricity, after calculating the data collected, then the next time, then the next time, so these values are not necessary to store, Place it in the RAM. However, the time calculated, it is necessary to be preserved, the single-chip chance timing or instantaneously puts the electricity number in the flash in the moment.
Be
--ROM storage instruction code and some fixed values, the program is not changed after running; RAM is used for random access of data in the program run, and the data disappears after power-down. .
Code means that data is defined in the ROM area, with only read properties, such as some LED displayed header data, can be defined as a Code stored in the ROM.
ROM: (Read Only Memory) Program Memory
In the microcontroller used to store program data and constant data or variable data, all code, global variables, local variables, column data defined in the C file and H file, the code in the Startup.asm file (similar to the STARTUP.ASM file) Bootloader or X86 in ARM, some low-end microcontrollers are not available in the ROM.
RAM: (Random Access Memory) Random Access Memory
The variables used in the program are used. Anyone used in the entire program is stored in RAM, and the "changed amount" includes global variables, local variables, stack segments.
After compiling, assemble, link, generate the HEX file. With dedicated burning software, burn the HEX file to the ROM through the burner (how to transfer the HEX file to the ROM within the MCU?), This time the ROM, contains all the program Content: Whether it is a row of program code, the local variables used in the function, the global variables declared in the header file, the read-only constant of Const declarations are generated in binary data, including in the HEX file, all burned Inside the ROM, the ROM at this time contains all the information about the program, which is because of this information, "Guide" all the action of the CPU.
Some people may have questions, since all the data is in the ROM, where is the data in the RAM come from? When is the CPU load data into the RAM? Will it be in the time of burning, it has been put in the RAM data burned into the RAM?
Be
To answer this question, you must first clarify: ROM is read-only memory, the CPU can only be read from the instead of writing data in it, and the power-down data is still saved in the memory; RAM is a random memory, CPU can be Read the data in it, it can be written to the data inside, and the data is not saved after power-down, this is the eternal truth, which is always in the heart.
It is easy to think that the data in the RAM is not written when it is burned, because after the burning is completed, unplug the power, after the MCU is powered up, the CPU can perform actions normally. There is data in RAM, indicating that the data in the RAM is not written when it is burned, and also means that data has been written in the RAM when the CPU is running. The key is here: This data is not human written, the CPU is written, when is the CPU written? Listen to me.
Up to the back, the ROM contains all the program content, when the MCU is powered on, the CPU starts executing instructions from the first line code. The work made here is to prepare for the smooth run of the entire program, or initialization to RAM (Note: ROM is read-only), there are several working tasks:
1. Assign address space for global variables - à If the global variable has been assigned, copy the initial value from the ROM to the RAM. If it is not assigned, the initial value corresponding to the global variable 0 or uncertain. Of course, if the address space of the variable has been specified, the corresponding address is directly positioned, then the task of assigning the address and positioning address here is done by the "connector".
2, set the length and address of the stack segment --- à In the single chip program developed by C language, there is no setup of the length of the stack segment, but this does not mean not set it. The stack segment is mainly used to "save the scene" and "live restoration" in the time of interruption, and its importance is self-evident. And this important content is also included in the contents of the compiler, and there is no peace of mind. How did you find it? strangeness.
3, allocate data segment DATA, constant segment const, start address of the code segment code. The address of the code segment and the constant segment may, whether it is fixed in the ROM, no matter how they are arranged, they will not affect the program. But the address of the data segment must be careful. When the data segment data is copied from the ROM to the RAM, and in the RAM, both data segment DATAs, there is also a stack segment Stack, as well as a universal working register group. Typically, the address of the working register group is fixed, which requires the data segment to overwrite the address of all working registers when absolute address segments are required. It must be seriously concerned.
The "first line of code" said here is not necessarily the program code you write, most of which are the compiler, or the Demo program file comes with the compiler. Because you write the program (C language program), it does not contain these content. Superior MCUs, these contents are in Startup file. Read carefully, good.
The usual approach is: When the normal FlashMCU is in power-on or reset, the PC pointer is stored in the PC pointer, indicating that the CPU starts executing instructions from the 0000 address of the ROM, and put a jump instruction at the address. The program jumps to the _main function, then according to different instructions, the execution is executed, when the interrupt occurs (the number of interrupts is limited, 2 ~ 5 interrupts), according to the interrupt vector table address allocated by the system, in the interrupt vector Inside, place an instruction that jumps to the interrupt service program, so, the entire program ran. Deciding this CPU, which is caused by this ROM structure.
In fact, this, C language compiler made a lot of work, just, you don't know. If you carefully read the Help file coming with the compiler, you will know a lot, this is the best way to understand the compiler.
Be
I / O port register:
It is also an amount that can be changed, which is arranged in a special RAM address, access to the system, and other variables cannot be defined in these locations.
Interrupt vector table:
The interrupt vector table is fixed in the ROM address that is fixed within the MCU, and the different addresses correspond to different interrupts. When each interrupt is generated, the corresponding interrupt service subroutine directly calls the program's entry address in the interrupt vector table.
ROM size problem:
For Flash types of MCUs, the size of the ROM space is usually all the entirety, that is, ak * 8bits. This is very understanding, I know that the space of the ROM is AK. However, for some OTP type microcontrollers, such as Holtek or SONIX's microcontrollers, often see "OTP Progarming ROM 2K * 15bit ...", may have doubts, this "15bit" It is considered to be 1 byte, 2 bytes are insufficient, then this ROM space is 2K, more than 2K, or 4k, but a little bit?
Here you need to clarify two concepts: one is the bit wide and the other is the length of the instruction. The bitwise of the instruction refers to the width of the data bits of one instruction; some are 8-bit bit wide, some are 15-bit bit wide. The command length refers to the storage space accounted for each instruction, with one byte, with 2 bytes, and 3 bytes or even 4 bytes of instructions. This approach to this image: When we do broadcast gym, there are many movements to do, but each complex action can be broken down into several simple actions. For example, when doing stretching, we only hear "2, 2, 3, 4, 5, 6, 7, 8" inside the broadcast, and each of them represents an instruction, and after hearing the "3" instruction. Our head, hands, waist, legs, feet make different movements: two eyes, left hand, the right hand is lifted up, the five fingers are straight, the right leg is straight, the left leg is a bow ····································································· The "bit wide" of the decomposition action is 15bits. This is also true, and when it is reversible or assembled, it can be seen that the composite instruction is indeed a simple instruction.
At this point, answer the previous question, this OTP's ROM space should be 2K, and the instruction bit is 15 bits. In general, when the command bit is not 8 times, the length of the MCU is indicated by one byte (Note: the byte width is 15 bits, not 8 bits), and a minimum of 2 or more. Bytes, although their total space is small, but it can accommodate space data. , Reading the full text, the technology area
About μClinux transplantation on ARM7 S3C4510B
Multi-looped into charge and discharge controller design
Interrupt service routines for ARM embedded systems
LED dimming engine design based on 8-bit MCU
How to display Chinese characters, ASCII characters and color graphics on the LCD monitor
Our other product: