
A microcontroller is essentially a small computer, and just like a large computer, it requires a data storage system. Microcontrollers have this capability, often integrated with the CPU, making them compact and flexible.Until the early 1990s, the most accessible microcontroller in China was the 8031: a chip without built-in memory, requiring external RAM and ROM to function, turning the microcontroller into a three-chip system…Now, things are different. There are various models like the 51, AVR, STC, and others like the 430, PIC, etc., each boasting their advantages, but no one dares to say, “I don’t need memory.”Data Storage Methods in Microcontrollers01Program Memory (ROM)The program memory stores the soul of the microcontroller:the operational program.Small ones may have only 1KB, which can hold a maximum of 1024 bytes of data, but since many instructions are 2-byte or 3-byte, it cannot accommodate 1024 instructions.There are larger ones with up to 128KB.These 8-bit data are either etched into the chip at the factory or programmed in one-time.For hobbyists or developers, the most they can do is use a special tool called a programmer to load the successfully debugged machine code, or like with AVR microcontrollers, spend a few bucks to make a download cable to transfer data from the computer (perhaps this is what makes AVR so appealing).Once the program is in the microcontroller’s program memory, it cannot be freely rewritten without the aforementioned devices. During operation, the microcontroller only reads instructions or fixed data from it, hence the nickname “Read-Only Memory” (ROM), which includes EPROM (erasable by ultraviolet light), EEPROM (electrically erasable), and the emerging FLASH ROM.
One-time programmable ROM is used only in mass-produced products where the circuit and program are fixed; in practical operation, they all function similarly.To locate data in ROM, each 8-bit storage unit has a fixed “address,” usually represented in hexadecimal.For example, for a so-called 4K ROM, the addresses range from 0000H to 0FFFH (i.e., from 0000, 0001…4095). The microcontroller determines from which address to fetch data entirely based on the program itself, without our intervention.Remember, when powered on, the microcontroller goes through a brief reset process and immediately turns to the lowest address in ROM, 0000H, where it often finds a “jump” instruction that directs it to another address: the actual starting address of the program, such as 0080H for the 51 series.ROM is the program memory, which, besides instructions, also includes certain fixed data necessary for the program’s operation, such as data tables.If we require the microcontroller to output values from 00H to FFH (255) in a sine wave pattern at a rate of 10,000 times per second,it would be overwhelming for it to calculate each value according to a formula.However, we can pre-calculate these values and store them in ROM, making retrieval much easier.Another important application: many devices with microcontrollers have digital displays, and those numbers are actually controlled by the microcontroller’s output lines to the field electrodes of the display.These character shapes are also stored in the ROM as a font table, corresponding each character shape to the digits 0-9 (internally represented as binary numbers 0000-0101).Common common-anode 7-segment displays require a positive voltage on the anode, with all cathodes grounded to display the number “8”. The display code for the number 8 is the binary number “10000000” (the 1 corresponds to the decimal point, preventing it from being displayed).02Data Memory (RAM)This is a memory area that allows data to be accessed at any time, meaning it can be read (retrieved) and written (stored), abbreviated as RAM.The RAM used in modern microcontrollers is static RAM (SRAM), which differs from the memory modules used in computers.As long as you write data into SRAM and do not power it off or clear it, the data remains there.Computers use dynamic RAM, which requires constant refresh pulses to retain data.Since the amount of information processed by microcontrollers is much smaller than that of computers, the RAM they carry is also relatively small: ranging from none, to 128, 256, … 1K, 2K, up to 4K, which is significantly less than ROM.In practice, RAM serves primarily as a temporary storage area for data, unless large amounts of data are needed for image processing.Generally, for microcontrollers performing relatively simple tasks, this amount is sufficient; if it is insufficient, external SRAM such as 6116, 6264, etc., can be added for expansion.
To access 8-bit binary numbers in RAM, addresses are also used to indicate their specific locations, just like in ROM.If a certain microcontroller has 1K (1024) RAM, its addresses range from 0000 to 1024, or in hexadecimal from 0000H to 03FFH. Thus, the addresses are the same as those in ROM.Will there be confusion?No, because reading from ROM is done via the microcontroller’s program pointer, transfer instructions, or lookup table instructions, which do not enter the RAM area;reading and writing RAM is done through different data transfer instructions, which also do not enter the ROM area.This is also a difference from computers, where both programs and data reside in memory modules, and if addresses overlap, it can lead to unpredictable consequences. This storage structure in microcontrollers is also known as Harvard architecture.The Role of RAM in MicrocontrollersThe role of RAM in microcontrollers is primarily to store temporary data.For example, when measuring temperature with a microcontroller, taking one measurement per second and displaying the average value over one minute (updating every minute):We first convert the temperature, an analog signal, into a proportional binary number through a sensor, amplification circuit, and A/D conversion, then every second, we sequentially store the digital values into the microcontroller’s RAM through the input port, perform pairwise summation and averaging calculations, and finally display the resulting value, clearing all 60 storage units to zero to remove old data, and the process repeats.ConclusionAdditionally, there are several registers within the microcontroller, which are few in number but have significant functions. They not only temporarily store data but also facilitate exchange, processing, and transmission, as well as record the current state of the microcontroller. Input and output ports also exist as special function registers, each with specific roles that cannot be easily clarified without consulting relevant literature.


Screenshots of Selected Electronic Books

【Complete Set of Hardware Learning Materials】
