MCU is the abbreviation for Microcontroller Unit, commonly known as a microcontroller. It appropriately reduces the frequency and specifications of CPUs, integrating memory, counters, USB, A/D converters, UART, PLC, DMA, and even LCD driver circuits onto a single chip, forming a chip-level computer. This allows for different combinations of control for various applications, such as mobile phones, PC peripherals, remote controls, automotive electronics, industrial stepper motors, robotic arms, etc., where MCU can be found.
Development History of Microcontrollers
The history of microcontrollers is relatively short, but its development has been rapid. Its emergence and development have generally synchronized with that of microprocessors (CPUs). Since Intel first launched the 4-bit microprocessor in 1971, its development can roughly be divided into five stages. Below is an introduction based on the development of Intel’s microcontrollers.
The initial stage of microcontroller development. In November 1971, Intel designed the 4-bit microprocessor Intel 4004, which integrated 2000 transistors per chip, along with RAM, ROM, and shift registers, forming the first MCS-4 microprocessor. It later introduced the 8-bit microprocessor Intel 8008, and other companies subsequently released their own 8-bit microprocessors.
The low-performance microcontroller stage. Represented by the MCS-48 series launched by Intel in 1976, it adopted a single-chip structure integrating an 8-bit CPU, 8-bit parallel I/O interface, 8-bit timer/counter, RAM, and ROM. Although its addressing range was limited (not exceeding 4 KB), it had no serial I/O, small RAM and ROM capacity, and a relatively simple interrupt system, its functionality met the needs of general industrial control and intelligent instruments.
The high-performance microcontroller stage. High-performance 8-bit microcontrollers launched during this stage generally included serial ports, multi-level interrupt processing systems, and multiple 16-bit timers/counters. The capacity of on-chip RAM and ROM increased, and the addressing range could reach 64 KB, with some chips even featuring A/D conversion interfaces.
The 16-bit microcontroller stage. In 1983, Intel launched the high-performance 16-bit microcontroller MCS-96 series, which utilized the latest manufacturing technology, achieving a chip integration level of 120,000 transistors per chip.
Microcontrollers have developed to higher levels in integration, functionality, speed, reliability, and application areas.
Classification and Applications of Microcontrollers
MCUs can be classified based on their memory types into non-ROM and with ROM types. For non-ROM chips, external EPROM must be used (typical is 8031); chips with ROM can be further divided into on-chip EPROM type (typical chip is 87C51), MASK on-chip ROM type (typical chip is 8051), and on-chip Flash type (typical chip is 89C51).
Based on usage, they can be divided into general-purpose and special-purpose; according to the width of the data bus and the length of data bytes processed at a time, they can be classified as 8, 16, and 32-bit MCUs.
Currently, the most widely used MCU application market in China is the consumer electronics field, followed by industrial and automotive electronics markets. Consumer electronics include home appliances, televisions, game consoles, and audio-video systems. The industrial sector includes smart homes, automation, medical applications, and new energy generation and distribution. The automotive sector includes automotive powertrains and safety control systems.
Basic Functions of Microcontrollers
For the vast majority of MCUs, the following functions are the most common and fundamental. Although the description may vary for different MCUs, they are essentially the same:
1. Timer: Although there are many types of timers, they can be categorized into two main types: fixed interval timers, where the timing is set by the system and cannot be controlled by user programs. The system only provides a few fixed time intervals for user programs to select from, such as 32Hz, 16Hz, 8Hz, etc. This type of timer is common in 4-bit MCUs and can be used for clock and timing-related functions.
Another type is the Programmable Timer, as the name suggests, where the timing can be controlled by the user’s program. Control methods include selecting the clock source, prescale selection, and setting the preset value. Some MCUs may have all three, while others may have one or two. This type of timer is very flexible in application, and its usage can vary widely. One of the most common applications is to achieve PWM output.
Since the clock source can be freely selected, this type of timer is generally combined with an Event Counter.
2. I/O Ports: Any MCU has a certain number of I/O ports; without I/O ports, the MCU loses its channel for communication with the outside world. Based on the configurability of I/O ports, they can be classified into the following types:
Pure input or pure output ports: These I/O ports are determined by the MCU hardware design and can only be either input or output, and cannot be set in real-time through software.
Direct read/write I/O ports: For example, the I/O ports of MCS-51 belong to this type. When executing the read I/O port instruction, it is an input port; when executing the write I/O port instruction, it automatically becomes an output port.
Programmable I/O ports: The input or output of these I/O ports is set by the program according to actual needs, making them more flexible in application. They can implement bus-level applications such as I2C buses, various LCD, and LED driver control buses.
One important point to remember when using I/O ports is that for input ports, there must be a clear level signal to ensure that it cannot be floating (this can be achieved by adding pull-up or pull-down resistors); for output ports, the output state level must consider the external connection situation to ensure that there is no pull current or sinking current in standby or static states.
3. External Interrupts: External interrupts are also a basic function possessed by the vast majority of MCUs, generally used for real-time signal triggering, data sampling, and state detection. Interrupt methods can be edge-triggered (rising or falling edge) or level-triggered. External interrupts are generally implemented through input ports; if it is an I/O port, the interrupt function will only be enabled when set as input; if it is an output port, the external interrupt function will be automatically disabled (some exceptions exist in ATMEL’s ATiny series, where output ports can also trigger interrupt functions). Applications of external interrupts include:
Detection of external trigger signals: One is based on real-time requirements, such as controlling thyristors, detecting burst signals, etc., while another situation is for power-saving needs.
Measurement of signal frequency: To ensure that signals are not missed, external interrupts are the ideal choice.
Data decoding: In remote control applications, software methods are often required to decode various encoded data, such as Manchester and PWM encoding.
Key detection and system wake-up: For MCUs that enter sleep mode, external interrupts are generally needed to wake them up, with the most basic form being a key press that generates a level change.
4. Communication Interfaces: The communication interfaces provided by MCUs generally include SPI, UART, and I2C interfaces, described as follows:
SPI Interface: This interface is a basic communication method provided by most MCUs, where data transmission is controlled by a synchronous clock. Signals include: SDI (Serial Data Input), SDO (Serial Data Output), SCLK (Serial Clock), and Ready signals; in some cases, there may be no Ready signal. This interface can operate in Master or Slave mode, commonly understood as who provides the clock signal, with the one providing the clock being the Master, and the other being the Slave.
UART (Universal Asynchronous Receive Transmit): This is one of the most basic asynchronous transmission interfaces, with only Rx and Tx signal lines. The basic data format is: Start Bit + Data Bit (7-bits/8-bits) + Parity Bit (Even, Odd, or None) + Stop Bit (1~2 Bits). The time taken for one data bit is referred to as the Baud Rate.
For most MCUs, the length of data bits, data parity method (odd, even, or none), the length of stop bits, and Baud Rate can be flexibly set through programming. This interface is most commonly used for data communication with the serial port of a PC.
I2C Interface: I2C is a data transmission protocol developed by Philips, which also uses two signals: SDAT (Serial Data Input/Output) and SCLK (Serial Clock). Its greatest advantage is the ability to connect multiple devices on this bus, identified and accessed by address; one of the biggest benefits of the I2C bus is that it can be easily implemented in software through I/O ports, with the transmission data rate entirely controlled by SCLK, which can be fast or slow, unlike the UART interface, which has strict rate requirements.
5. Watchdog Timer: The watchdog timer is also a basic configuration in most MCUs (some 4-bit MCUs may lack this function). Most MCUs allow the program to reset the watchdog but not disable it (some are set during programming, like Microchip PIC series MCUs), while others decide whether to enable it in a specific way, such as Samsung’s KS57 series, where accessing the watchdog register automatically enables it and cannot be disabled. Generally, the reset time of the watchdog can be set by the program. The most basic application of the watchdog is to provide the MCU with a self-recovery capability in case of unexpected failures leading to a crash.
Global Leading Microcontroller Manufacturers
Below is a list of mainstream manufacturers of microcontrollers (there may be some omissions), ranked in no particular order:
North America and Europe
1. Freescale + NXP: Netherlands, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, LED and general lighting, healthcare, multimedia integration, home appliances and power tools, building automation technology motor control, power and power converters, energy and smart grids, automation, computer and communication infrastructure.
2. Microchip + Atmel: United States, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, industrial, motor control, automotive, building automation, home appliances, home entertainment, industrial automation, lighting, IoT, smart energy, mobile electronic devices, computer peripherals.
3. Cypress + Spansion: United States, mainly providing 8-bit, 16-bit, and 32-bit MCUs. Application areas: automotive electronics, home appliances, medical, consumer electronics, communication and telecommunications, industrial, wireless.
4. ADI (Analog Devices): United States, mainly providing 8-bit, 16-bit, and 32-bit MCUs. Application areas: aerospace and defense, automotive applications, building technology, communication, consumer electronics, energy, healthcare, instrumentation and measurement, motors, industrial automation, security.
5. Infineon: Germany, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, consumer electronics, engineering, commercial and agricultural vehicles, data processing, electric transportation, industrial applications, medical devices, mobile devices, motor control and drive, power, solutions for motorcycles, electric bicycles, and small electric vehicles, smart grid, lighting, solar system solutions, wind energy system solutions.
6. ST Microelectronics: Italy/France, mainly providing 32-bit MCUs. Application areas: LED and general lighting, transportation, healthcare, multimedia integration, home appliances and power tools, building automation technology motor control, power and power converters, energy and smart grids, automation, computer and communication infrastructure.
7. Qualcomm: United States, mainly providing 16-bit and 32-bit MCUs. Application areas: smartphones, tablets, wireless modems.
8. Texas Instruments: United States, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, consumer electronics, medical devices, mobile devices, communication.
9. Maxim: United States, mainly providing 32-bit MCUs. Application areas: automotive electronics, consumer electronics, industrial applications, security.
1. Renesas: Japan, mainly providing 16-bit and 32-bit MCUs. Application areas: computers and peripherals, consumer electronics, health and medical electronics, automotive electronics, industrial, communication.
2. Toshiba: Japan, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, industrial, motor control, wireless communication, mobile phones, computers and peripheral devices, imaging and audio-video, consumer (home appliances), LED lighting, security, power management, entertainment devices.
3. Fujitsu: Japan, mainly providing 32-bit MCUs. Application areas: automotive, medical, machinery, home appliances.
4. Samsung Electronics: South Korea, mainly providing 16-bit and 32-bit MCUs. Application areas: automotive electronics, industrial, motor control, automotive, building automation, home appliances, home entertainment, industrial automation, lighting, IoT, smart energy, mobile electronic devices, computer peripherals.
1. Sigma Microelectronics: Mainly provides 32-bit MCUs, application areas: telecommunications, manufacturing, energy, transportation, electricity, etc.
2. Zhuhai Obit: Mainly provides 32-bit MCUs, application areas: aerospace: satellite, rocket, aircraft; high-end industrial control: embedded computers; ship control, industrial control, power equipment, environmental monitoring.
3. GigaDevice: Mainly provides 32-bit MCUs, application areas: industrial automation, human-machine interface, motor control, security monitoring, smart home, IoT.
4. Shengxi Microelectronics: Mainly provides 8-bit and 32-bit MCUs, application areas: small appliances, consumer electronics, remote controls, mice, lithium batteries, digital products, automotive electronics, medical instruments and measurement, toys, industrial control, smart home, and security.
5. Chipsea Technology: Mainly provides 16-bit and 32-bit MCUs, application areas: instruments and meters, IoT, consumer electronics, home appliances, automotive electronics.
6. United Microelectronics: Mainly provides 8-bit and 16-bit MCUs, application areas: consumer electronics, white goods, industrial control, communication equipment, automotive electronics, computers.
7. Zhuhai Jianrong: Mainly provides 8-bit MCUs, application areas: home appliances, mobile power supplies.
8. Juchip Technology: Mainly provides 8-bit to 32-bit MCUs, application areas: tablets, smart homes, multimedia, Bluetooth, Wi-Fi audio.
9. Aisico Microelectronics: Mainly provides 8-bit and 16-bit MCUs, application areas: consumer chips, communication chips, information chips, home appliances.
10. Huaxin Microelectronics: Mainly provides 8-bit and 4-bit MCUs, application areas: satellite receivers, mobile phone chargers, perpetual calendars, multi-functional remote controls.
11. Shanghai Beiling (Hua Da Semiconductor Holdings): Mainly provides 8-bit, 16-bit, and 32-bit MCUs, application areas: computer peripherals, HDTV, power management, small appliances, digital appliances.
12. Haier Integrated Circuits: Mainly provides 14-bit, 15-bit, and 16-bit MCUs, application areas: consumer electronics, automotive electronics, industrial, smart instruments.
13. Beijing Junzheng: Mainly provides 32-bit MCUs, application areas: wearable devices, IoT, smart home appliances, automotive, consumer electronics, tablets.
14. Zhongwei Semiconductor: Mainly provides 8-bit MCUs, application areas: smart home appliances, automotive electronics, security monitoring, LED lighting and landscape, smart toys, smart home, consumer electronics.
15. Shenzhou Longxin Integrated Circuit: Mainly provides 32-bit MCUs, application areas: power monitoring, smart grid, industrial digital control, IoT, smart home, data monitoring.
16. Unisoc Microelectronics: Mainly provides 8-bit and 16-bit MCUs, application areas: smart appliances.
17. Times Minxin: Mainly provides 32-bit MCUs, application areas: automotive navigation, traffic monitoring, fishing boat supervision, power and telecommunications networks.
18. China Resources Silicon Microelectronics (a subsidiary of China Resources Micro): Mainly provides 8-bit and 16-bit MCUs, application areas: consumer electronics, industrial control, home appliances.
19. Guoxin Technology: Mainly provides 32-bit MCUs, application areas: information security field, office automation field, communication network field, information security field.
20. Zhongtian Micro: Mainly provides 32-bit MCUs, application areas: smartphones, digital TVs, set-top boxes, automotive electronics, GPS, e-readers, printers.
21. China Resources Microelectronics: Mainly provides 8-bit and 16-bit MCUs, application areas: home appliances, consumer electronics, general control circuits for industrial automation control.
22. Zhongying Electronics: Mainly provides 4-bit, 8-bit, 16-bit, and 32-bit MCUs, application areas: home appliances, motors.
23. Lingdong Microelectronics: Mainly provides 32-bit MCUs, application areas: motor control, Bluetooth control, high-definition display, wireless charging, drones, mini printers, smart labels, electronic cigarettes, LED dot matrix screens, etc.
24. New Tang Technology: Mainly provides 32-bit MCUs, application areas: lighting, IoT, etc.
25. Neusoft Carrier: Mainly provides 8-bit and 32-bit MCUs, application areas: home appliances, smart homes, instruments and meters, LCD panel controllers, industrial control, etc.
26. Betel: Mainly provides 32-bit MCUs, application areas: smart homes, industrial control, and consumer product fields.
27. Shengquan Technology: Mainly provides 8-bit MCUs, application areas: automotive, education, industrial control, medical, and small to medium-sized display panels.
28. Hangshun Chip: Mainly provides 32-bit MCUs, application areas: automotive, IoT, consumer, industrial, etc.
29. Fudan Microelectronics: Mainly provides 16-bit and 32-bit MCUs, application areas: smart meters, smart locks, etc.
1. Hongjing Technology: Mainly provides 8-bit MCUs. Application areas: communication, industrial control, information appliances, voice.
2. Shengqun Semiconductor: Mainly provides 8-bit and 32-bit MCUs. Application areas: consumer electronics, LED lighting, etc.
3. Lingsun Technology: Mainly provides 8-bit and 16-bit MCUs. Application areas: home audio and video.
4. Zhongying Electronics: Mainly provides 4-bit and 8-bit MCUs. Application areas: chargers, mobile power supplies, home appliances, industrial control.
5. Songhan Technology: Mainly provides 8-bit and 32-bit MCUs. Application areas: remote controls, smart chargers, large and small systems, electronic scales, ear thermometers, blood pressure monitors, tire pressure monitors, and various measurement and health equipment.
6. Huabang Electronics: Mainly provides 8-bit and 16-bit MCUs. Application areas: automotive electronics, industrial electronics, networks, computers, consumer electronics, IoT.
7. Shisu Technology: Mainly provides 4-bit, 8-bit, and 51-bit MCUs. Application areas: remote controls, small appliances.
8. Youhua Microelectronics: Mainly provides 4-bit and 8-bit MCUs. Application areas: recording integrated circuit products, consumer electronics, home products.
9. Yingguang Technology Microcontroller: Mainly provides 4-bit and 8-bit MCUs. Application areas: machinery, automation, home appliances, robotics.
10. Yilong Electronics: Mainly provides 8-bit and 16-bit MCUs. Application areas: consumer electronics, computers, smartphones.
Tips for Learning Microcontrollers
For any MCU, the basic principles and functions are generally similar, the differences lie only in the configuration and number of peripheral function modules, instruction systems, etc.
For the instruction system, while the forms may seem diverse, they essentially represent different symbols, with similar meanings, functionalities, and addressing methods.
To understand an MCU, you first need to know its ROM space, RAM space, number of I/O ports, number of timers and timing methods, peripheral function modules (Peripheral Circuit), interrupt sources, operating voltage, and power consumption, etc.
After understanding these MCU features, the next step is to compare the functions of the selected MCU with the requirements of the actual project development to clarify which resources are currently needed and which are unnecessary for the project.
For the functions needed in the project that are not provided by the selected MCU, it is essential to understand the relevant MCU documentation thoroughly to find indirect methods to achieve them. For example, if the developed project requires communication with the PC’s COM port, but the selected MCU does not provide a UART port, one can consider using external interrupts to achieve this.
For resources needed in project development, it is crucial to carefully understand and read the manual, while unnecessary function modules can be ignored or browsed. In learning about MCUs, application is key and the primary goal.
Once the relevant functions of the MCU are clarified, you can start programming.
For beginners or designers using this MCU for the first time, many unclear descriptions of MCU functionalities may arise. Such issues can be resolved through two methods: one is to write special verification programs to understand the functions described in the documentation; the other is to temporarily ignore them and write the microcontroller program according to your current understanding, leaving modifications and improvements for debugging. The former method is suitable for projects with ample time and beginners, while the latter is more appropriate for those with some microcontroller development experience or in tight project timelines.
Do not spend too much time trying to understand the instruction system. The instruction system is merely a logical description symbol; during programming, check the relevant instructions based on your logic and the program’s logic requirements. As programming progresses, you will become more familiar with the instruction system, even memorizing it unconsciously.
Writing Programs for Microcontrollers
The programming of MCUs differs significantly from PC programming. Although C-based MCU development tools are becoming increasingly popular, for an efficient program code, assembly language remains the simplest and most effective programming language for designers who prefer it.
In MCU program writing, the basic framework is generally consistent, typically divided into three main parts: initialization (this is the most significant difference between MCU programming and PC programming), the main program loop, and interrupt handling programs, each explained as follows:
1. Initialization: Initialization is the most fundamental and crucial step in all MCU program designs, generally including the following:
Mask all interrupts and initialize the stack pointer: Interrupts should generally not occur during initialization.
Clear the system’s RAM area and display memory: Although it may not always be necessary, from a reliability and consistency perspective, especially to prevent unexpected errors, it is recommended to develop good programming habits.
Initialize I/O ports: Set the input/output modes of relevant I/O ports according to the project’s application requirements. For input ports, set their pull-up or pull-down resistors; for output ports, set their initial output levels to prevent unnecessary errors.
Set interrupts: For all interrupt sources required by the project, they should be enabled and their triggering conditions set, while unnecessary additional interrupts must be disabled.
Initialize other function modules: For all peripheral function modules of the MCU needed in the project, they must be set according to the project’s application requirements, such as setting the Baud Rate, data length, parity method, and Stop Bit length for UART communication, and for Programmable Timers, setting their clock source, prescaler, and Reload Data, etc.
Parameter initialization: After completing the hardware and resource initialization of the MCU, the next step is to initialize some variables and data used in the program. This part of initialization needs to be designed according to the specific project and the overall arrangement of the program. For applications using EEPROM to save project preset values, it is advisable to copy the relevant data into the MCU’s RAM during initialization to improve the program’s data access speed while reducing system power consumption (in principle, accessing external EEPROM increases power consumption).
2. Main program loop: Most MCUs operate continuously for long periods, so their main program body is generally designed in a loop. For applications with multiple working modes, multiple loops may exist, with transitions between them managed by state flags. In the main program body, the following modules are typically arranged:
Computational programs: Computational programs are generally time-consuming, so it is strongly advised not to handle them within any interrupts, especially division and multiplication operations.
Programs with low or no real-time requirements;
Display transmission programs: Mainly for applications with external LED, LCD drivers.
3. Interrupt handling programs: Interrupt programs are primarily used to handle tasks and events that require high real-time performance, such as detecting external burst signals, key detection and handling, timing counts, LED display scanning, etc.
Generally, interrupt programs should keep the code as concise and short as possible. For functionalities that do not need real-time processing, set trigger flags in the interrupt and let the main program execute specific tasks—this is very important, especially for low-power, low-speed MCUs, which must ensure timely responses to all interrupts.
4. Different task arrangements may vary by MCU:
For example, for low-speed, low-power MCUs (Fosc=32768Hz), considering that such projects are handheld devices using ordinary LCD displays, the response to key presses and display updates requires high real-time performance, thus usually employing timer interrupts to handle key actions and data displays; for high-speed MCUs, such as those with Fosc > 1MHz, the MCU has sufficient time to execute the main program loop, so various trigger flags can be set in the corresponding interrupts, and all tasks can be executed in the main program body.
5. In MCU program design, it is also crucial to: Prevent simultaneous access or setting of the same variable or data in both the interrupt and main program body. An effective prevention method is to arrange the processing of such data in a single module and determine whether to execute related operations based on trigger flags; in other program bodies (mainly interrupts), only set the trigger flags where data processing is needed. This ensures that data execution is predictable and unique.
Engineers’ Summary on Microcontroller Programming
1. Develop a good habit of summarizing; summarizing is not only a review of your learning but also deepens your understanding of the learning process, preventing repeated mistakes.
2. Before writing a program, first familiarize yourself with the project to have a clear understanding; outline a rough framework. Carefully consider how to layout, what the most reasonable layout is; this step is crucial. Analyze which module to start with, detail the specific steps for that module, how to name various functions, and how to connect with other modules. It is best to write down important processes on paper.
3. For modular programming in C language, first divide each module; program one module at a time, determining an order, and proceed in sequence. After a module is successfully written, then write its header file.
4. Do not ignore warnings; they indicate that there are unreasonable aspects in the program that need clarification and resolution. When searching for the source, be targeted, you can search online for related information or consult others. For example, you might have added the main function from another project to this one, or named a function redundantly. You might also analyze the reasons based on experimental phenomena, layer by layer. Sometimes, if a problem is unsolvable, taking a break to think can be beneficial. Even in simple areas, pay attention; mistakes can happen.
In microcontroller application development, issues such as code efficiency, anti-interference capability, and reliability still pose challenges. Here are some basic skills that should be mastered in microcontroller development.
Microcontroller Development Skills
1. How to Reduce Bugs in Programs
To reduce bugs in programs, consider the following parameters that should be managed during system operation.
-
Physical parameters: These parameters are mainly the system’s input parameters, including excitation parameters, operating parameters during processing, and result parameters after processing.
-
Resource parameters: These parameters mainly refer to the resources in the system’s circuits, devices, and functional units, such as memory capacity, storage unit length, and stack depth.
-
Application parameters: These application parameters often manifest as application conditions for some microcontrollers and functional units. Process parameters: refer to parameters that undergo orderly changes during system operation.
2. How to Improve the Efficiency of C Language Programming Code
Using C language for microcontroller program design is an inevitable trend in microcontroller development and application. To achieve the highest efficiency when using C programming, it is best to familiarize yourself with the C compiler you are using. First, experiment with the number of assembly language statement lines corresponding to each C language line after compilation, so you can clearly understand the efficiency. In future programming, use the statements with the highest compilation efficiency. Each C compiler has certain differences, so the compilation efficiency will also vary; excellent embedded system C compilers have code lengths and execution times that are only 5-20% longer than equivalent functionality written in assembly language.
For complex projects with tight development timelines, C language can be used, but you must be very familiar with the MCU system’s C language and C compiler, particularly the data types and algorithms supported by that C compilation system. Although C language is the most common high-level language, the C compilation systems vary among different MCU manufacturers, especially in operations involving special function modules. If you are unfamiliar with these features, debugging will lead to many problems, resulting in lower execution efficiency than assembly language.
3. How to Solve Microcontroller Anti-Interference Issues
The most effective way to prevent interference is to eliminate the source of interference and isolate the interference path, but this is often difficult to achieve, so we can only see if the microcontroller’s anti-interference capability is strong enough. While improving the hardware system’s anti-interference capability, software anti-interference is increasingly valued for its flexible design, saving hardware resources, and good reliability.
The most common phenomenon of microcontroller interference is reset; regarding program runaway, it can also be pulled back to reset state using software traps and watchdogs, so the most important aspect of microcontroller software anti-interference is to handle the reset state properly.
Generally, microcontrollers will have some status registers that can be used to determine the reason for the reset; additionally, you can embed some flags in RAM. Each time the program resets, you can determine different reset reasons by checking these flags; you can also directly jump to the corresponding program based on different flags. This allows the program to run continuously, and users will not notice that the program has been reset.
4. How to Test the Reliability of Microcontroller Systems
When a microcontroller system design is completed, different microcontroller system products will have different testing items and methods, but some tests are mandatory:
-
Test the completeness of the microcontroller software functions
-
Power-on and power-off tests
-
-
Sometimes, we can also simulate destructive situations that may occur during human use. For example, intentionally rubbing the microcontroller system’s contact ports with human skin or clothing to test its anti-static capability. Bringing a high-power electric drill close to the microcontroller system to test its anti-electromagnetic interference capability, etc.
In summary, microcontrollers have become an important aspect of computer development and application. The significance of microcontroller applications lies in fundamentally changing the traditional design concepts and methods of control systems.
Most functions that previously had to be implemented by analog or digital circuits can now be achieved using microcontrollers through software methods. This software replacement of hardware control technology is also known as microcontrol technology, marking a revolution in traditional control technology.
Moreover, during the development and application process, we must master skills to improve efficiency, enabling us to leverage its broader applications.
Operating on a chip mainly involves manipulating the chip’s internal registers, which all have their unique addresses mapped in memory for corresponding operations. When examining a chip, first look at the timing diagram, then understand the relevant registers, how to operate them, define the required ports (that the program can recognize), and write the read and write operation programs.
How to write data into the chip, how to read data out, and through which ports to input or read out (the most crucial aspects).
When connecting a chip through a bus, it is essential to understand the protocol of that bus. For example, when connecting a chip via I2C bus, the bus is primarily used to control that chip.
1. In a dot matrix, one 74hc595 is used for column selection, while the other two are used for color selection; the dot matrix is equivalent to a collection of diodes.
One end must provide a high level, while the other end must provide a low level for the diodes to light up. When selecting different ends, different colors light up.
Timer working mode selection: The upper four bits set Timer T1, while the lower four bits set T0. When setting two timers, be careful to use OR (|). When using interrupts, be mindful to clear any necessary flags upon entering the interrupt.
2. Serial port transmission and reception: Baud rate settings generally use mode 2 (automatic reload of initial values) since different devices have varying data handling capabilities. The baud rate is mainly set to accommodate low-speed devices and facilitate communication. The interrupt flag must be cleared in software. When setting serial port interrupts, either transmission or reception can enter the interrupt function, so be careful to set up the interrupt function (I generally feel it is best to set one function to act as either a master or slave).
If using interrupts for transmission, the first step is to determine how to enter the interrupt, so an initial transmission must occur; thereafter, you can enter the interrupt. Only one byte can be sent at a time, and the next byte can only be sent after TI is set.
3. For PCF8591 A/D conversion, there are four input channels. When reading from PCF8591, select which channel to read, and the voltage input for that channel will be read. The converted data is stored within the chip and can then be read out. During reading, first write the chip address, then write the device sub-address (0x40|channel number), followed by the data to be read.
4. For D/A conversion, first write the device address to the chip, then write the sub-address (0x40), followed by the digital value to convert. The device address is detailed in the chip documentation.
5. For LCD displays, once data is written, it will remain displayed without continuous refreshing; to change it, new input must be provided.
6. For the DS1302 clock chip, when reading data, the first bit of data is read on the eighth clock falling edge during data writing, and then preparations for the next output must be made; pay attention to the programming method and the position of the return value.
7. In DS1302, first specify the register, then write data into it. The registers indicated in the chip documentation are the addresses. (The write protection part of the program is still unclear; isn’t writing always available? Why enable write protection?)
(According to a previous expert, a flag can be set after initializing the time, so if this flag exists, there is no need to re-initialize the time. However, if the power is cut off, the MCU’s RAM cannot retain this flag, so the DS1302’s RAM can be used to save this flag, and it can be read after power on. I am also a beginner, and I plan to use DS1302 soon. I am unsure if this explanation is correct; I have yet to implement it specifically, so let’s communicate more.)
8. It is best to write initialization as well to prevent forgetting later. Pay attention to whether to operate on the lowest or highest bit when reading or writing; this can be determined by the timing diagram.
9. For infrared transmission and reception, the reception is determined by the length of time between two falling edges, which indicates whether it is high or low. When writing the program, first use a timer to determine the time length, save it, and then convert it to binary (this program’s writing method is worth looking into, it is very good).
10. Stepper motors: Mainly used for switching; the torque of stepper motors decreases as speed increases. They are mainly used in automatic feeding for machining parts on machine tools. They can also be used in places requiring high precision control.
Stepper motors convert electrical pulse signals into angular or linear displacement through open-loop control. Under non-overload conditions, the motor’s speed and stopping position depend solely on the frequency and number of pulse signals, unaffected by load changes. When the stepper driver receives a pulse signal, it drives the stepper motor to rotate a fixed angle in the set direction, known as the “step angle.” Its rotation occurs in fixed angle steps. By controlling the number of pulses, one can control the angular displacement to achieve precise positioning; simultaneously, controlling the pulse frequency can control the motor’s speed and acceleration for speed regulation.
11. Servo motors are motors that control the operation of mechanical components in a servo system, serving as an auxiliary motor for indirect speed regulation. Servo motors can achieve very accurate control of speed and position. They convert voltage signals into torque and speed to drive the controlled object. The rotor speed of a servo motor is controlled by the input signal and can respond quickly. In automatic control systems, they are used as actuators and feature small electromechanical time constants, high linearity, and starting voltage characteristics, converting received electrical signals into angular displacements or angular velocities on the motor shaft. They are divided into two main categories: DC and AC servo motors, characterized by no self-rotation phenomenon when the signal voltage is zero, and speed decreases uniformly with increasing torque.
DC motors have a wide range of applications, especially in small vehicles.
12. Overview of Chinese Characters:
To output Chinese characters on a display or printer, Chinese characters are designed as dot matrix graphics, resulting in corresponding dot matrix codes (glyph codes).
The unified encoding method for representing Chinese characters in computers is called internal code (such as national standard code), which is unique (equivalent to the character’s ID number). The encoding formed for the convenience of inputting Chinese characters is called input code, which belongs to the external code of Chinese characters and varies due to different encoding methods. The encoding formed for displaying and printing Chinese characters is called glyph code, and the computer finds the glyph code of the Chinese character through the internal code in the glyph library to achieve conversion.
According to national standard code regulations, each Chinese character has a determined binary code. However, this code may conflict with ASCII code during internal computer processing. To solve this problem, the first bit of each byte of the national standard code is incremented by 1. Since ASCII code only uses 7 bits, this leading “1” serves as a marker for identifying Chinese character codes. When the computer processes a code with a leading “1,” it interprets it as Chinese character information, while it interprets a code with a leading “0” as ASCII code. The processed national standard code (internal code) is the machine internal code.
If we replace the “口” character’s dot with “0,” we can visually obtain the glyph code for “口”: 0000H 0004H 3FFAH 2004H 2004H 2004H 2004H 2004H 2004H 2004H 2004H 2004H 3FFAH 2004H 0000H 0000H. When the computer outputs “口,” it first finds the starting address of the display font library, calculates the glyph code of “口” based on its internal code, and then finds the glyph code (in binary) for scanning on the screen, where the binary code’s “1” indicates a lit pixel and “0” indicates an unlit pixel, thus forming the character graphic for “口.”
The glyphs of Chinese characters are arranged in the order of national standard code and stored in binary file format in memory, forming the Chinese character glyph library, also known as the Chinese font library.
Two encoding methods can be seen in the header file.
GB1616.h//------------------ Definition of Chinese Character Glyph Data Structure ------------------------//struct typFNT_GB16 //Chinese Character Glyph Data Structure{ unsignedchar Index[3]; //Chinese Character Internal Code Index unsignedchar Msk[32]; //Dot Matrix Data };/////////////////////////////////////////////////////////////////////////// Chinese Character Glyph Table //// Chinese Character Library: Songti 16.dot, horizontal sampling from the left high bit, data arranged: from left to right and top to bottom ///////////////////////////////////////////////////////////////////////////conststruct typFNT_GB16 codeGB_16[]= //Data Table{/*------------------------------------------------------------------------------; Source File / Text: Xu; Width × Height (pixels): 16×16------------------------------------------------------------------------------*/