ROM - store the firmware (store the instruction code and some fixed values, the program can not be changed after running)
All the code in the c file and h file, global variables, local variables, constant data defined by the 'const' qualifier, code in the startup.asm file (similar to the bootloader in ARM or the BIOS in X86, some low-end microcontrollers are All without it are stored in the ROM.
RAM—random access to data during program operation (data disappears after power-down). The amount of data that needs to be rewritten in the entire program is stored in RAM. The “amount to be changed†includes global variables, local variables, Stack segment.
FLASH - stores user programs and data that needs to be saved permanently.
For example, the electronic watt-hour meter of the home is now a single-chip microcomputer, and the program of the single-chip microcomputer is stored in the ROM. In the working process, the meter is to calculate the data. It is necessary to collect the voltage and current, and calculate the electricity according to the voltage and current. Voltage and current are a timely data, the user does not care, it is only used to calculate the electricity. After the calculation, the collected data is used up, and then collected again, so these values ​​are not necessary to be permanently stored. Put it in RAM. However, the calculated electrical energy needs to be permanently saved. The MCU will store the electrical energy into the FLASH at regular intervals or at the moment of power failure.
Conventionally, the ROM is used to store the firmware, and the RAM is used to store the data. Because the FLASH ROM is faster than ordinary ROM, it is easy to erase and write. It is generally used to store user programs and data that needs to be saved permanently. For example, the current electronic watt-hour meter, its core is a single-chip, the program of the microcontroller is stored in the ROM. In the working process, the meter is to calculate the data. It is necessary to collect the voltage and current, and calculate the electricity according to the voltage and current. Voltage and current are a timely data, the user does not care, it is only used to calculate the electricity. After the calculation, the collected data is used up, and then collected again, so these values ​​are not necessary to be permanently stored. Put it in RAM. However, the calculated electrical energy needs to be permanently saved. The MCU will store the electrical energy into the FLASH at regular intervals or at the moment of power failure.
--ROM stores the instruction code and some fixed values, the program can not be changed after running; RAM is used for random access of data during program running, and the data disappears after power failure. .
Code means to define the data in the ROM area, with read-only attributes. For example, some header data displayed by LEDs can be defined as code stored in ROM.
ROM: (Read Only Memory) program memory
In the microcontroller is used to store program data and constant data or variable data, all the code in the c file and h file, global variables, local variables, constant data defined by the 'const' qualifier, code in the startup.asm file (similar The bootloader in ARM or the BIOS in X86, some low-end microcontrollers do not have this) are stored in the ROM.
RAM: (Random Access Memory) random access memory
Used to store variables used in the program. In the entire program, the amount that needs to be rewritten is stored in RAM. The "amount to be changed" includes global variables, local variables, and stack segments.
After the program is compiled, assembled, and linked, a hex file is generated. Using a dedicated burning software, the hex file is burned into the ROM through the programmer (how exactly is the hex file transferred to the internal ROM of the MCU?), therefore, all the programs are included in the ROM at this time. Content: Both the line-by-line program code, the local variables used in the function, the global variables declared in the header file, and the read-only constants declared by const are all generated in binary data, included in the hex file, all burned. In the ROM, the ROM at this time contains all the information of the program. It is because of this information that "directs" all the actions of the CPU.
Some people may have doubts, since all the data is in the ROM, where does the data in the RAM come from? When does the CPU load data into RAM? Will it be in the burning time, the data that needs to be put in RAM is burned into RAM?
To answer this question, you must first clear one: ROM is a read-only memory, the CPU can only read data from inside, but can not write data to it. After power-down, the data is still stored in the memory; RAM is random memory, and the CPU can Inside the data, you can write data to it. After the power is turned off, the data is not saved. This is an eternal truth, always remembered.
Clearly the above problem, then it is easy to think that the data in the RAM is not written at the time of burning, because after the burning is completed, the power is unplugged, and when the MCU is powered on again, the CPU can perform the action normally. There is still data in the RAM, which means that the data in the RAM is not written during the programming, and it also shows that the data has been written in the RAM while the CPU is running. The key is here: this data is not written by humans, the CPU writes, when is the CPU written? Listen to me.
Last time, the ROM contains all the program contents. When the MCU is powered on, the CPU starts executing instructions from the first line of code. The work done here is to prepare for the smooth running of the whole program, or to initialize the RAM (Note: ROM is read-only and not written), there are several tasks:
1. Allocate the address space for the global variable---Ã If the global variable has been assigned the initial value, copy the initial value from the ROM to the RAM. If no initial value is given, the initial value under the address corresponding to the global variable It is 0 or is uncertain. Of course, if the address space of the variable has been specified, the corresponding address is directly located, and the task of assigning the address and the location address here is completed by the "connector".
2, set the length and address of the stack segment --- Ã C microcontroller developed in the microcontroller program, generally does not involve the setting of the stack segment length, but this does not mean no setting. The stack segment is mainly used to "save the scene" and "live restore" during the interrupt processing, and its importance is self-evident. And such an important content is also included in the compiler's default content, which is really trouble-free, but not necessarily worry. How did you find it in peacetime? strange.
3. Allocate data segment data, constant segment const, start address of code segment code. The address of the code segment and the constant segment can be ignored. They are 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 taken care of. The data of the data segment is copied from the ROM to the RAM, and in the RAM, there are data segment data, stack segment stack, and a general working register bank. Normally, the address of the working register bank is fixed, which requires that the data segment cannot overwrite the addresses of all working register banks when the data segment is absolutely addressed. Must cause serious concern.
The "first line of code" mentioned here is not necessarily the code of your own code. Most of them are compiled by the compiler, or the demo program file that comes with the compiler. Because, the program you write yourself (C language program) does not contain these contents. A bit of advanced microcontroller, these contents are all in the startup file. It is good to read it carefully.
The usual practice is: the normal flashMCU is stored at the time of power-on or reset, the PC pointer stores "0000", indicating that the CPU starts executing instructions from the 0000 address of the ROM, and puts a jump instruction at the address. The program jumps to the _main function, and then executes one by one according to different instructions. When the interrupt occurs (the number of interrupts is also very limited, 2~5 interrupts), according to the interrupt vector table address allocated by the system, the interrupt vector Inside, place an instruction to jump to the interrupt service routine, and the whole program will run. Deciding the CPU to do this is caused by this ROM structure.
In fact, inside, the C compiler does a lot of work, just, you don't know it. If you read the help file that comes with the compiler, you will know a lot of things. This is the best way to understand the compiler.
I/O port register:
It is also an amount that can be changed. It is arranged at a special RAM address that is accessed by the system, and other variables cannot be defined at these locations.
Interrupt vector table:
The interrupt vector table is fixed in the ROM address inside the MCU, and different addresses correspond to different interrupts. Each time an interrupt is generated, the corresponding interrupt service routine is directly called, and the entry address of the program is placed in the interrupt vector table.
ROM size problem:
For flash type MCUs, the size of the ROM space is usually the entire byte, which is ak*8bits. This is well understood, and at a glance, the space of the ROM is aK. However, for some OTP type MCUs, such as holtek or sonix MCU, it is often seen in the data sheet that "OTP progarming ROM 2k*15bit..." may cause confusion, this "15bit" It is considered that there is more than one byte, and two bytes are insufficient. Is the ROM space 2k, more than 2k, or 4k but a little less?
There are two concepts to be clarified: one is the bit width of the instruction and the other is the length of the instruction. The bit width of an instruction is the width of the data bits occupied by an instruction; some are 8-bit wide and some are 15-bit wide. The instruction length refers to the storage space occupied by each instruction. It has 1 byte, has 2 bytes, and has 3 bytes or even 4 bytes of instructions. This can be an analogy of the image: when we do broadcast gymnastics, there are a lot of actions to do, but each complex action can be broken down into a few simple actions. For example, when doing stretching exercises, we only hear "2, 2, 3, 4, 5, 6, 7, 8" in the broadcast, and each number here represents an instruction. After hearing the "3" command. Our heads, hands, waist, legs and feet make different movements: the two eyes are in front, the left hand is akimbo, the right hand is lifted up, the five fingers are straight and naturally open, the right leg is straight, and the left leg is lunged. ······ and so on a series of decomposition actions, and the instructions to complete these actions have only one "3", but there are many actions to be performed, so the multiple decomposition actions are combined into one instruction, and each The "bit width" of the decomposition action is 15 bits. This is also true in practice. When disassembling or assembling, you can see that compound instructions do have simple instructions combined.
At this point, answer the previous question, the ROM space of this OTP should be 2K, and the instruction bit width is 15 bits. Generally, when the instruction bit width is not a multiple of 8, it means that most of the instruction length of the MCU is one byte (note: the byte width is 15 bits, not 8 bits), and very few are two or more. Bytes, although their total space is small, but they can accommodate a lot of spatial data.
UHC Marine Electrode Water Level Sensor
Uhc Marine Electrode Water Level Sensor,Electrode Type Water Ingress Detection Sensor,Water Ingress Detection Sensor,Water Ingress Detection Sensotion Sensor
Taizhou Jiabo Instrument Technology Co., Ltd. , https://www.jbcbyq.com