MCU is Microcontroller Unit The abbreviation of microcontroller in Chinese, commonly known as single-chip microcomputer, is to appropriately reduce the frequency and specifications of the CPU, and integrate peripheral interfaces such as memory, counter, USB, A/D conversion, UART, PLC, DMA, and even LCD driver circuits on a single chip to form a chip-level computer. It can perform different combinations of control for different applications, such as mobile phones, PC peripherals, remote controls, automotive electronics, industrial stepper motors, and robot arm control. MCU can be seen in the figure.
one
A brief history of the development of microcontrollers
The history of microcontrollers is not long, but it has developed very rapidly. Its production and development are generally synchronized with the production and development of microprocessors (CPUs). Since Intel Corporation of the United States first launched a 4-bit microprocessor in 1971, its development so far can be roughly divided into five stages. The following is an introduction to the development of microcontrollers from Intel Corporation.
1971~1976
The initial stage of the development of microcontrollers. In November 1971, Intel first designed the Intel 4004, a 4-bit microprocessor with an integration of 2,000 transistors/chip, equipped with RAM, ROM and shift register, forming the first MCS-4 microprocessor. Then it launched the 8-bit microprocessor Intel 8008, and other 8-bit microprocessors launched by other companies.
1976~1980
Low performance microcontroller stage. Represented by the MCS-48 series launched by Intel in 1976, it uses a monolithic structure that integrates an 8-bit CPU, 8-bit parallel I/O interface, 8-bit timer/counter, RAM and ROM on a semiconductor chip. Although its addressing range is limited (no more than 4 KB), there is no serial I/O, RAM and ROM capacity is small, and the interrupt system is relatively simple, but its functions can meet the needs of general industrial control and intelligent instruments and meters.
1980~1983
High-performance microcontroller stage. The high-performance 8-bit microcontrollers launched at this stage generally have serial ports, multi-level interrupt processing systems, and multiple 16-bit timers/counters. The capacity of the on-chip RAM and ROM is increased, and the addressing range can reach 64 KB. Some chips also have an A/D conversion interface.
1983~late 1980s
16-bit microcontroller stage. In 1983, Intel launched the high-performance 16-bit microcontroller MCS-96 series. Due to its adoption of the latest manufacturing technology, the chip integration level is as high as 120,000 transistors/chip.
1990s
Microcontrollers are developing to a higher level in all aspects of integration, functionality, speed, reliability, and application fields.
two
Classification and application of single chip microcomputer
MCUs can be divided into two types according to their memory types: those without on-chip ROM and those with on-chip ROM. For chips without on-chip ROM, an external EPROM must be connected before they can be used (typically 8031); chips with on-chip ROM are divided into on-chip EPROM type (typical chip is 87C51), MASK on-chip mask ROM type (typical chip is 8051), on-chip Flash type (typical chip is 89C51) and other types.
According to the purpose, it can be divided into general-purpose and special-purpose types; according to the width of the data bus and the length of data bytes that can be processed at one time, it can be divided into 8, 16, and 32-bit MCUs.
At present, the most extensive domestic MCU application market is in the consumer electronics field, followed by the industrial field and the automotive electronics market. Consumer electronics includes household appliances, televisions, game consoles, audio and video systems, etc. Industrial fields include smart homes, automation, medical applications, and new energy generation and distribution. The automotive field includes automotive powertrain and safety control systems.
three
Basic functions of microcontroller
For most MCUs, the following functions are the most common and basic. For different MCUs, the way they are described may be different, but they are essentially the same:
1. TImer (timer): Although there are many types of TImer, they can be summarized into two categories: One type is TImer with fixed time intervals, that is, the timing time is set by the system and cannot be controlled by the user program. The system only provides several fixed time intervals for the user program to choose, such as 32Hz, 16Hz, 8Hz, etc. This type of TImer is more common in 4-bit MCUs, so it can be used to implement clock, timing and other related functions.
The other type is Programmable Timer. As the name suggests, the timing time of this type of Timer can be controlled by the user's program. The control methods include: selection of clock source, selection of frequency division (Prescale) and setting of preset number. Some MCUs have all three at the same time, while others may have one or two of them. This type of Timer application is very flexible, and its actual use is also ever-changing. One of the most common applications is to use it to implement PWM output.
Since the clock source can be freely selected, this type of Timer is generally combined with the Event Counter.
2. IO ports: Any MCU has a certain number of IO ports. Without IO ports, the MCU loses the channel to communicate with the outside world. According to the configurability of the IO port, it can be divided into the following types:
Pure input or pure output port: This type of IO port is determined by the MCU hardware design. It can only be input or output, and software cannot be used for real-time settings.
Directly read and write IO ports: For example, the IO port of MCS-51 belongs to this type of IO port. When the read IO port instruction is executed, it is an input port; when the write IO port instruction is executed, it is automatically an output port.
Programming to set the input and output direction: The input or output of this type of IO port is set by the program according to actual needs. The application is relatively flexible and can realize some bus-level applications, such as I2C bus, various LCD, LED Driver control buses, etc.
For the use of IO ports, an important point that must be kept in mind is: for the input port, there must be a clear level signal to ensure that it cannot float (this can be achieved by adding a pull-up or pull-down resistor); for the output port, the output status level must consider its external connection conditions, and it should be ensured that there is no current pulling or sinking in the standby or static state.
3. External interrupt: External interrupt is also a basic function of most MCUs. It is generally used for real-time triggering of signals, data sampling and status detection. The interrupt methods include rising edge trigger, falling edge trigger and level trigger. External interrupts are generally implemented through input ports. If it is an IO port, its interrupt function will be turned on only when it is set to input; if it is an output port, the external interrupt function will be automatically turned off (there are some exceptions in ATMEL's ATiny series, which can also trigger the interrupt function when it is an output port). The applications of external interrupts are as follows:
Detection of external trigger signals: One is based on real-time requirements, such as control of thyristors, detection of sudden signals, etc., while the other is the need to save power.
Measurement of signal frequency: In order to ensure that the signal is not missed, external interrupt is the ideal choice.
Decoding of data: In the field of remote control applications, in order to reduce the cost of design, it is often necessary to use software to decode various encoded data, such as the decoding of Manchester and PWM encoding.
Key detection and system wake-up: For MCUs that enter the sleep state, they generally need to be awakened through external interrupts. The most basic form is a key press, which produces level changes through the action of the key.
4. Communication interface: The communication interface provided by MCU generally includes SPI interface, UART, I2C interface, etc., which are described as follows:
SPI interface: This type of interface is the most basic communication method provided by most MCUs. Its data transmission is controlled by a synchronous clock. The signals include: SDI (serial data input), SDO (serial data output), SCLK (serial clock) and Ready signal; in some cases, there may be no Ready signal; this type of interface can work in master mode or Slave mode. In popular terms, it depends on who provides the clock signal. The party providing the clock is the master, and the opposite party is the Slaver.
UART (Universal Asynchronous Receive Transmit): It is the most basic asynchronous transmission interface. It has only two signal lines, Rx and Tx. The basic data format is: Start Bit + Data Bit (7-bits/8-bits) + Parity Bit (Even, Odd or None) + Stop Bit (1~2Bit). The time occupied by one bit of data is called Baud Rate.
For most MCUs, the length of data bits, data check mode (odd check, even check or no check), stop bit length and Baud Rate can be flexibly set through program programming. The most commonly used method of this type of interface is to communicate with the serial port of a PC.
I2C interface: I2C is a data transmission protocol developed by Philips. It is also implemented using 2 signals: SDAT (serial data input and output) and SCLK (serial clock). Its biggest benefit is that multiple devices can be attached to this bus and identified and accessed through addresses; one of the biggest benefits of the I2C bus is that it is very convenient to use software to implement it through the IO port. Its transmission data rate is completely controlled by SCLK, which can be fast or slow, unlike the UART interface, which has strict rate requirements.
5. Watchdog (watchdog timer): Watchdog is also a basic configuration of most MCUs (some 4-bit MCUs may not have this function). The Watchdog of most MCUs can only allow the program to reset it but not turn it off (some are set when the program is burned in, such as Microchip PIC series MCU), and some MCUs use a specific method to decide whether to turn it on, such as Samsung's KS57 series. As long as the program accesses the Watchdog register, it is automatically turned on and cannot be turned off again. Generally speaking, the reset time of watchdog can be set programmably. The most basic application of Watchdog is to provide a self-recovery capability for MCUs that crash due to unexpected failures.
Four
Global mainstream microcontroller manufacturer
(Ranked in no particular order, organized into mainstream manufacturers. If there is anything missing, please add it in the comment area)
Europe and America
1. Freescale+NXP (Freescale+NXP): Netherlands, mainly provides 16-bit and 32-bit MCUs. Applications: Automotive electronics, LED and general lighting, healthcare, multimedia convergence, home appliances and power tools, building automation technology motor control, power supplies and power converters, energy and smart grids, automation, computer and communication infrastructure.
2. Microchip+Atmel (Microchip Technology + Atmel): The United States, mainly provides 16-bit and 32-bit MCUs. Applications: automotive electronics, industrial use, motor control, automobiles, building automation, home appliances, home entertainment, industrial automation, lighting, Internet of Things, smart energy, mobile electronic equipment, computer peripherals.
3. Cypress+Spansion (Cypress + Spansion Semiconductor): The United States, mainly provides 8-bit, 16-bit, and 32-bit MCUs. Applications: automotive electronics, home appliances, medical, consumer electronics, communications and telecommunications, industry, wireless.
4. ADI (Analog Devices): United States, mainly provides 8-bit, 16-bit, and 32-bit MCUs. Applications: Aerospace and defense, automotive applications, building technology, communications, consumer electronics, energy, healthcare, instrumentation and measurement, motors, industrial automation, security.
5. Infineon: Germany, mainly provides 16-bit and 32-bit MCUs. Applications: automotive electronics, consumer electronics, engineering, commercial and agricultural vehicles, data processing, electric transportation, industrial applications, medical equipment, mobile equipment, motor control and drive, power supply, for motorcycles, electric bicycles and small electric vehicles, smart grids, lighting, solar system solutions, wind energy system solutions.
6. ST Microelectronics: Italy/France, mainly provides 32-bit MCU. Applications: LED and general lighting, transportation, healthcare, multimedia convergence, home appliances and power tools, building automation technology motor control, power supplies and power converters, energy and smart grids, automation, computer and communication infrastructure.
7. Qualcomm: The United States, mainly provides 16-bit and 32-bit MCUs. Applications: Smartphones, tablets, wireless modems.
8. Texas Instruments: United States, mainly provides 16-bit and 32-bit MCUs. Application scope: automotive electronics, consumer electronics, medical equipment, mobile devices, communications.
9. Maxim (Maxim): United States, mainly provides 32-bit MCU. Application scope: automotive electronics, consumer electronics, industrial applications, security.
Japan and South Korea
1. Renesas: Japan, mainly provides 16-bit and 32-bit MCUs. Application scope: computers and peripherals, consumer electronics, health and medical electronics, automotive electronics, industry, and communications.
2. Toshiba: Japan, mainly provides 16-bit and 32-bit MCUs. Application scope: automotive electronics, industrial use, motor control, wireless communications, mobile phones, computers and peripheral equipment, imaging and audio and video, consumer (home appliances), LED lighting, security, power management, entertainment equipment.
3. Fujitsu: Japan, mainly provides 32-bit MCU. Application scope: automobile, medical, machinery, home appliances.
4. Samsung Electronics: South Korea, mainly provides 16-bit and 32-bit MCUs. Applications: automotive electronics, industrial use, motor control, automobiles, building automation, home appliances, home entertainment, industrial automation, lighting, Internet of Things, smart energy, mobile electronic equipment, computer peripherals.
China region
Mainland China
1. Sigma Microelectronics: Mainly provides 32-bit MCU, application scope: telecommunications, manufacturing, energy, transportation, electricity, etc.
2. Zhuhai Orbit: Mainly provides 32-bit MCU, application scope: aerospace: satellite station ships, aircraft; high-end industrial control: embedded computers; ship control, industrial control, power equipment, environmental monitoring.
3. GigaDevice Innovation: Mainly provides 32-bit MCU, application scope: industrial automation, human-machine interface, motor control, security monitoring, smart home, Internet of Things.
4. Shengsi Microelectronics: Mainly provides 8-bit and 32-bit MCUs. Application scope: small household appliances, consumer electronics, remote controls, mice, lithium batteries, digital products, automotive electronics, medical instruments and measurement, toys, industrial control, smart home and security and other fields.
5. Chipsea Technology: Mainly provides 16- and 32-bit MCUs. Application scope: instrumentation, Internet of Things, consumer electronics, home appliances, and automotive electronics.
6. Lianhua Integrated Circuit: Mainly provides 8-bit and 16-bit MCUs. Application scope: consumer electronics, white home appliances, industrial control, communication equipment, automotive electronics, and computers.
7. Zhuhai Jianrong: Mainly provides 8-bit MCU, application scope: household appliances, mobile power supply.
8. Actions Technology: Mainly provides 8-bit to 32-bit MCUs, application range: tablet computers, smart home, multimedia, Bluetooth, wifi audio.
9. Aisco Microelectronics: Mainly provides 8-bit and 16-bit MCUs. Application scope: consumer chips, communication chips, information chips, and home appliances.
10. Huaxin Microelectronics: Mainly provides 8-bit and 4-bit MCUs. Applications: satellite receivers, mobile phone chargers, perpetual calendars, and all-in-one remote controls.
11. Shanghai Belling (Huada Semiconductor Holdings): Mainly provides 8-bit, 16-bit, and 32-bit MCUs. Applications: computer peripherals, HDTV, power management, small appliances, and digital appliances.
12. Haier Integrated Circuits: Mainly provides 14-bit, 15-bit, and 16-bit MCUs. Application scope: consumer electronics, automotive electronics, industry, and smart instruments.
13. Beijing Ingenic: Mainly provides 32-bit MCU, application scope: wearable devices, Internet of Things, smart home appliances, automobiles, consumer electronics, tablet computers.
14. China Microelectronics: Mainly provides 8-bit MCUs. Applications: smart home appliances, automotive electronics, security monitoring, LED lighting and landscape, smart toys, smart homes, and consumer electronics.
15. Shenzhou Loongson Integrated Circuit: Mainly provides 32-bit MCU, application scope: power monitoring, smart grid, industrial digital control, Internet of Things, smart home, data monitoring.
16. Ziguang Microelectronics: Mainly provides 8-bit and 16-bit MCUs. Application scope: smart home appliances.
17. Times Minxin: Mainly provides 32-bit MCU, application scope: car navigation, traffic monitoring, fishing vessel supervision, power telecommunications network.
18. China Resources Silicon Microelectronics (a subsidiary of China Resources Microelectronics): mainly provides 8-bit and 16-bit MCUs. Application scope: consumer electronics, industrial control, and home appliances.
19. National Core Technology: Mainly provides 32-bit MCU, application scope: information security field, office automation field, communication network field, information security field.
20. Zhongtian Micro: Mainly provides 32-bit MCUs. Applications: smartphones, digital TVs, set-top boxes, automotive electronics, GPS, e-readers, and printers.
21. China Resources Microelectronics: Mainly provides 8-bit and 16-bit MCUs. Application scope: general control circuits for home appliances, consumer electronics, and industrial automation control.
22. Zhongying Electronics: Mainly provides 4-bit, 8-bit, 16-bit, and 32-bit MCUs. Application scope: home appliances and motors.
23. Lingdong Microelectronics: Mainly provides 32-bit, application scope: motor control, Bluetooth control, high-definition display, wireless charging, drones, micro printers, smart labels, electronic cigarettes, LED dot matrix screens, etc.
24. Nuvoton Technology: Mainly provides 8-bit MCU, application scope: lighting, Internet of Things, etc.
25. Neusoft Carrier: Mainly provides 8-bit and 32-bit MCUs. Applications: home appliances, smart homes, instrumentation, LCD panel controllers, industrial control, etc.
26. Betley: Mainly provides 32-bit MCU, application scope: smart home, industrial control and consumer products.
27. Shengquan Technology: Mainly provides 8-bit MCU, application scope: small and medium-sized display panels for automotive, education, industrial control, medical, etc.
28. Hangshun Chip: Mainly provides 8-bit and 32-bit MCUs. Application scope: automobiles, Internet of Things, etc.
29. Fudan Microelectronics: Mainly provides 16-bit and 32-bit MCUs. Applications: smart meters, smart door locks, etc.
30. Huada Semiconductor: Mainly provides 8-bit, 16-bit, and 32-bit MCUs. Applications: industrial control, intelligent manufacturing, smart life, and the Internet of Things.
Taiwan, China
1. Hongjing Technology: mainly provides 32-bit MCU. Application scope: communications, industrial control, information appliances, and voice.
2. Holtek Semiconductor: mainly provides 8-bit and 32-bit MCUs. Application scope: consumer electronics, LED lighting, etc.
3. Lingyang Technology: mainly provides 8-bit and 16-bit MCUs. Application scope: home audio and video.
4. Zhongying Electronics: mainly provides 4-bit and 8-bit MCUs. Application scope: chargers, mobile power supplies, home appliances, industrial control.
5. Songhan Technology: mainly provides 8-bit and 32-bit MCUs. Application scope: remote controls, smart chargers, large and small systems, electronic scales, ear thermometers, blood pressure monitors, tire pressure gauges, various measurement and health equipment.
6. Winbond Electronics: mainly provides 8-bit and 16-bit MCUs. Application scope: automotive electronics, industrial electronics, network, computer, consumer electronics, Internet of Things.
7. Ten Speed Technology: mainly provides 4-bit, 8-bit, and 51-bit MCUs. Application scope: remote controls, small household appliances.
8. Youhua Microelectronics: mainly provides 4-bit and 8-bit MCUs. Application scope: recording integrated circuit products, consumer electronics, household products.
9. Yingguang Technology Microcontroller: mainly provides 4-bit and 8-bit MCUs. Application scope: machinery, automation, home appliances, robots.
10. Elan Electronics: mainly provides 8-bit and 16-bit MCUs. Application scope: consumer electronics, computers, smartphones.
five
Tips for learning microcontrollers
The basic principles and functions of any MCU are similar. The only difference is the configuration and number of peripheral function modules, instruction systems, etc.
For instruction systems, although they appear to be very different in form, they are actually just different symbols. The meanings they represent, the functions to be completed, and the addressing methods are basically similar.
To understand an MCU, you first need to know its ROM space, RAM space, number of IO ports, number of timers and timing methods, provided 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 functions required for actual project development, and clarify which resources are currently needed and which are not used in this project.
For functions that need to be used in the project but are not provided by the selected MCU, you need to carefully understand the relevant information of the MCU in order to implement them using indirect methods. For example, if the project you are developing needs to communicate with the PC COM port, and the selected MCU does not provide a UART port, you can consider using external interrupts to implement it.
For the resources needed for project development, you need to carefully understand and read the Manua*, while you can ignore or browse the unnecessary functional modules. For MCU learning, application is the key and the main purpose.
After clarifying the relevant functions of the MCU, you can start programming.
For beginners or designers who are using this MCU for the first time, they may encounter many unclear descriptions of the functions of the MCU. For such problems, there are two methods to solve them. One is to write a special verification program to understand the functions described in the data; the other is to temporarily ignore it and write the MCU program according to your current understanding, leaving it to be modified and improved during debugging. The former method is suitable for projects with loose time and beginners, while the latter method is suitable for people with certain experience in microcontroller development or when the project schedule is tight.
Don't take any special time to understand the command system. The instruction system is just a symbol of logical description. You can only view the relevant instructions according to your own logic and the logical requirements of the program during programming. As programming progresses, you will become more and more proficient in the instruction system, and you can even memorize it unconsciously.
six
Microcontroller programming
There is a big difference between MCU program writing and PC program writing. Although C-based MCU development tools are becoming more and more popular, for designers who have efficient program code and like to use assembly, assembly language is still the most concise and effective programming language.
For MCU programming, the basic framework can be said to be roughly the same. It is generally divided into three parts: the initialization part (this is the biggest difference between MCU programming and PC), the main program loop body and the interrupt handler. Their respective descriptions are as follows:
1. Initialization: For the design of all MCU programs, initialization is the most basic and important step, which generally includes the following:
Mask all interrupts and initialize the stack pointer: The initialization part generally does not want any interrupts to occur.
Clear the system's RAM area and display Memory: Although sometimes it may not be completely necessary, from the perspective of reliability and consistency, especially to prevent unexpected errors, it is recommended to develop good programming habits.
Initialization of the IO port: According to the application requirements of the project, set the input and output mode of the relevant IO port. For the input port, you need to set its pull-up or pull-down resistor; for the output port, you must set its initial level output to prevent unnecessary errors.
Interrupt settings: For all interrupt sources that need to be used in the project, they should be turned on and the trigger conditions for the interrupts should be set. For redundant interrupts that are not used, they must be turned off.
Initialization of other functional modules: For all peripheral function modules of the MCU that need to be used, the corresponding settings must be made according to the requirements of the project application. For example, for UART communication, the Baud Rate, data length, verification method, and Stop Bit length need to be set. For the Programmer Timer, its clock source, frequency division number, and Reload Data must be set.
Initialization of parameters: After completing the initialization of the MCU hardware and resources, the next step is to initialize some variables and data used in the program. The initialization of this part needs to be designed according to the specific project and the overall arrangement of the program. For some applications that use EEPROM to save project prefabricated data, it is recommended to copy the relevant data to the RAM of the MCU during initialization to improve the program's access speed to data and reduce the power consumption of the system (in principle, accessing external EEPROM will increase the power consumption of the power supply).
2. Main program loop body: Most MCUs run continuously for a long time, so their main program bodies are basically designed in a loop. For applications with multiple working modes, there may be multiple loop bodies, which are converted through status flags. For the main program body, the following modules are generally arranged:
Calculation programs: Calculation programs are generally time-consuming, so we are firmly opposed to processing them in any interrupt, especially multiplication and division operations.
Processing programs with low or no real-time requirements;
Display transmission program: mainly for applications with external LED and LCD drivers.
3. Interrupt handler: The interrupt program is mainly used to handle tasks and events with high real-time requirements, such as detection of external sudden signals, detection and processing of buttons, timing counting, LED display scanning, etc.
In general, the interrupt program should keep the code as concise and short as possible. For functions that do not need to be processed in real time, the trigger flag can be set in the interrupt, and then the main program will perform specific transactions. This is very important, especially for low-power, low-speed MCUs, which must ensure timely response to all interrupts.
4. For the arrangement of different task bodies, different MCUs have different processing methods:
For example, for low-speed, low-power MCU (Fosc=32768Hz) applications, considering that such projects are handheld devices and use ordinary LCD displays, the response to key presses and display response requires high real-time performance, so timed interrupts are generally used to process key presses. Actions and data display; for high-speed MCUs, such as Fosc>1MHz applications, since the MCU has enough time to execute the main program loop body at this time, it can only set various trigger flags in the corresponding interrupts and put all tasks in the main program body for execution.
5. In MCU programming, one thing that needs special attention is:
It is necessary to prevent the same variable or data from being accessed or set in the interrupt and main program bodies at the same time. An effective prevention method is to arrange the processing of such data in a module, and determine whether to perform related operations on the data by judging the trigger flag; in other program bodies (mainly interrupts), only set trigger flags where the data needs to be processed. --This ensures that data execution is predictable and unique.
seven Engineer's summary of microcontroller programming
1. Develop a good habit of summarizing. Summarizing is not only a summary of your own learning, but also a review and deepening of the learning process. It can also avoid making mistakes the second time.
2. Before writing a program, you must first have a familiar understanding of the project, be aware of it, and outline a general framework. It is very important to carefully consider how to lay out and what is the most reasonable layout. It is necessary to analyze which module to do first, the specific steps of this module, how to name each function, the connection with other modules, etc. It’s a good idea to get a piece of paper and jot down important processes.
3. For modular programming in C language, you must first divide each module, program module by module, determine a sequence, follow the sequence, and then write the next module after the module is successful. For header files, write the module's header file after the module is written.
4. Don’t ignore warnings when they appear. It means that there must be something unreasonable in the program. You need to find out the source and find a solution. Be specific when looking for sources. You can search the Internet for information in this area, or ask others for advice. For example, the main function in another project was actually added to this project. There are actually duplicate function names. There are also reasons to analyze the experimental phenomena and progress step by step. There was also the wrong interface selected when defining the port. Sometimes, it’s good to take a break and think about it if you really can’t solve it. No matter how simple it is, you should pay attention to it, as there may be mistakes.
In the development of microcontroller applications, problems such as code usage efficiency, anti-interference performance and reliability of microcontrollers are still troubled. Now we summarize several basic skills that should be mastered in the development of microcontroller.
eight
Microcontroller development skills
1 How to reduce bugs in programs
Regarding how to reduce program bugs, you should first consider the following out-of-range management parameters that should be considered during system operation.
- Physical parameters: These parameters are mainly input parameters of the system, which include excitation parameters, operating parameters during acquisition processing and result parameters at the end of processing.
- Resource parameters: These parameters are mainly the resources of circuits, devices, and functional units in the system, such as memory capacity, storage unit length, and stacking depth.
- Application parameters: These application parameters often appear as application conditions of some microcontrollers and functional units. Process parameters: refers to the parameters that change in an orderly manner during system operation.
2 How to improve the efficiency of C language programming code
Using C language for microcontroller programming is an inevitable trend in the development and application of microcontrollers. If you want to achieve maximum efficiency when programming in C, it is best to be familiar with the C compiler you are using. First test the number of lines of assembly language statements corresponding to each C language compilation, so that you can clearly know the efficiency. When programming in the future, use the statement with the highest compilation efficiency. Each C compiler will have certain differences, so the compilation efficiency will also be different. The code length and execution time of an excellent C compiler for embedded systems are only 5-20% longer than the same function written in assembly language.
For complex projects with tight development time, C language can be used, but the prerequisite is that you are very familiar with the C language and C compiler of the MCU system. Pay special attention to the data types and algorithms supported by the C compilation system. Although C language is the most common high-level language, different MCU manufacturers have different C language compilation systems, especially in the operation of some special function modules. Therefore, if you do not understand these features, there will be many problems during debugging, which will lead to lower execution efficiency than assembly language.
3 How to solve the anti-interference problem of microcontroller
The most effective way to prevent interference is to remove the interference source and block the interference path, but it is often difficult to do so, so we can only see whether the anti-interference ability of the microcontroller is strong enough. While improving the anti-interference ability of hardware systems, software anti-interference is receiving more and more attention because of its flexible design, saving of hardware resources, and good reliability.
The most common phenomenon of microcontroller interference is reset. As for the program running away, software traps and watchdogs can actually be used to bring the program back to the reset state. Therefore, the most important thing for microcontroller software to resist interference is to handle the reset state.
Generally, microcontrollers will have some flag registers that can be used to determine the cause of reset; in addition, you can also bury some flags in RAM yourself. Each time the program is reset, different reset causes can be determined by judging these flags; you can also jump directly to the corresponding program based on different flags. This allows the program to run continuously, and the user will not notice that the program has been reset when using it.
4 How to test the reliability of the microcontroller system
When a microcontroller system design is completed, there will be different test items and methods for different microcontroller system products, but some must be tested:
- Test the completeness of the microcontroller software function
- Power on and power off test
- Aging test
- Tests such as ESD and EFT
Sometimes, we can also simulate the damage that may occur during human use. For example, deliberately rub the contact port of the microcontroller system with the human body or clothing fabric to test the anti-static ability. Use a high-power electric drill to work close to the microcontroller system to test the ability to resist electromagnetic interference.
To sum up, the single-chip microcomputer has become an important aspect of computer development and application. The important significance of the application of single-chip microcomputer is that it fundamentally changes the traditional control system design ideas and design methods.
Most of the functions that must be implemented by analog circuits or digital circuits in the past can now be implemented through software methods using microcontrollers. This kind of control technology in which software replaces hardware is also called micro-control technology, which is a revolution in traditional control technology.
In addition, during the development and application process, we must master skills and improve efficiency so that it can be used for a wider range of purposes.
Nine
Chip operation summary The operations on the chip are mainly operations on the registers in the chip. The registers in the chip have their own unique addresses mapped on the memory, which is the operation on the corresponding address. When looking at the chip, first look at the timing diagram, then understand the corresponding registers, understand how they operate, define the required ports (which can be identified by the program), and write write operation procedures and read operation procedures.
How to write data into the chip, how to read data, and which port to input or read through (the most important thing).
When connecting chips through a bus, you must first understand the protocol of the bus. The chip connected to the I2C bus mainly controls the chip through this bus.
1. One 74hc595 in the dot matrix is used for column selection, and the other two are used for color selection. The dot matrix is equivalent to a collection of diodes.
Only when one end is given a high level and the other end is given a low level can the diode light up. It's just that when one end is selected differently, different colors will light up.
Selection of timer working mode: The high four bits set the timer T1, and the low four bits set T0. Then the last two digits of each mode set the working mode. When setting two timers, be careful to use or (|). When using interrupts, pay attention to clearing the ones that should be cleared after entering the interrupt.
2. Serial port transceiver: Mode 2 (automatic reload initial value) is generally used to set the baud rate. Because different devices have different data processing capabilities, setting the baud rate is mainly to take care of low-speed devices and communicate with each other. The interrupt flag bit must be cleared by software. When setting the serial port interrupt, no matter which one is generated by sending or receiving, it can enter the interrupt function, so pay attention to setting the interrupt function. (Self-feeling generally sets a function, as a host computer or a slave computer).
If you use an interrupt to send, you have to figure out how to enter the interrupt for the first time, so you have to send it once first, and then you can enter the interrupt. Only one byte can be sent at a time, and the next bit can only be sent after TI is set.
3. Pcf8591ad conversion has four channels of input. When reading pcf8591, which channel is selected, the voltage input by that channel is read. The converted data is stored in the chip and then read out. When reading, first write the address of the chip, then write the sub-address of the device (0x40|channel number), and then the read data.
4. Da conversion is to first write the device address into the chip, then write the sub-address (0x40), and then write the digital quantity to be converted. Device address chip information is introduced.
5. For the LCD display, after the data is written and displayed, it will always be displayed without continuous refresh. If you want to change it, you can only re-enter it.
6. For the ds1302 clock chip, when reading data, the first data is read at the falling edge of the eighth clock when writing data, and then prepares for the next output. Pay attention to the writing method of the program and the location of the return value.
7. First specify the register in Ds1302, and then write data to it. The register on the chip data indicates the address. (I still don’t quite understand the write protection program. Isn’t there always writing? Why is the write protection still turned on?)
(According to the previous hero, you can set a flag after the initialization time. If there is this flag, there is no need to initialize the time. However, if the power is turned off, the MCU's RAM cannot save this flag, so you can use the DS1302's RAM to save the flag and read it after powering on. I am also a beginner, and I plan to use DS1302 recently. I don't know if this is correct, and I haven't implemented it yet. Please share more)
8. It is best to write down the initialization in case you forget it later. Sometimes pay attention to whether the lowest bit or the highest bit is operated first when reading or writing, which can be judged according to the timing diagram.
9. For infrared transceiver, when receiving, it determines whether it is high level or low level based on the time between two falling edges. When writing a program, first use a timer to determine the time, save it, and then convert it into binary (read more about how to write this program, it is very good).
10. Stepper motor: It is mainly used for switching. The torque of the stepper motor decreases as the speed increases. It is mainly used for automatic feeding of parts processed on machine tools. It can also be used in control places with higher precision.
The stepper motor is an open-loop control element stepper motor device that converts electrical pulse signals into angular displacement or linear displacement. Under non-overload conditions, the motor's speed and stop position only depend on the frequency and number of pulses of the pulse signal, and are not affected by load changes. When the stepper driver receives a pulse signal, it drives the stepper motor to rotate in the set direction at a fixed angle, called the "step angle". Its rotation runs step by step at a fixed angle. The angular displacement can be controlled by controlling the number of pulses to achieve accurate positioning; at the same time, the speed and acceleration of the motor rotation can be controlled by controlling the pulse frequency to achieve speed regulation.
11. Servo motor: (servo motor) refers to the engine that controls the operation of mechanical components in the servo system. It is an indirect transmission device that assists the motor. Servo motors can control speed and position accuracy very accurately, and can convert voltage signals into torque and rotational speed to drive control objects. The rotor speed of the servo motor is controlled by the input signal and can respond quickly. In the automatic control system, it is used as an actuator and has the characteristics of small electromechanical time constant, high linearity, starting voltage, etc. It can convert the received electrical signal into the angular displacement or angular velocity output on the motor shaft. They are divided into two categories: DC and AC servo motors. Their main feature is that there is no rotation when the signal voltage is zero, and the rotational speed decreases at a constant speed as the torque increases. DC motor: Large range, all on small cars.
12. Overview of Chinese characters:
In order to output Chinese characters on a monitor or printer, the Chinese characters are designed into a dot matrix according to graphic symbols, and the corresponding dot matrix code (glyph code) is obtained.
The unified encoding method used to represent Chinese characters in the computer forms a Chinese character encoding called an internal code (such as a national standard code), and the internal code is unique (equivalent to the ID number of the character). The Chinese character encoding formed to facilitate the input of Chinese characters is an input code, which is an external code of Chinese characters. The input code is different due to different encoding methods and is diverse. The Chinese character code formed for displaying and printing out Chinese characters is a glyph code. The computer finds the glyph code of the Chinese character in the font model library through the Chinese character internal code and realizes its conversion.
In-camera code
According to the provisions of the national standard code, each Chinese character has a certain binary code, but this code will conflict with the ASCII code when processed internally by the computer. To solve this problem, add 1 to the first digit of each byte of the national standard code. Since the ASCII code only uses 7 bits, the "1" in the first place can be used as a mark to identify Chinese character codes. When the computer processes the code with "1" in the first place, it understands it as Chinese character information, and when it processes the code with "0" in the first place, it understands it as an ASCII code. The national standard code (internal code) processed in this way is the internal code.
If we replace the "." of this "口" graphic with "0", we can get the glyph code of "口" very vividly: 0000H 0004H 3FFAH 2004H 2004H 2004H 2004H 2004H 2004H 2004H 2004H2004H 3FFAH 2004H 0000H 0000H. When the computer wants to output "口", it first finds the first address of the display font library, calculates based on the internal code of "口", and then finds the glyph code of "口", and then scans the screen sequentially according to the glyph code (in binary) through the control of the character generator. Where the binary code is "0", the place where it is "0" is scanned, and the place where it is "1" is scanned to highlight, so the character pattern of "口" can be obtained.
Chinese character fonts are arranged in the order of the national standard code and stored in the memory in the form of binary files, forming a Chinese character font library, also called a Chinese character glyph library, or a Chinese character library.
Two encoding methods, see header file
GB1616.h//------------------ Chinese character font data structure definition ------------------------//structtypFNT_GB16 //Chinese character font data structure {unsignedcharIndex[3]; //Chinese character internal code index unsignedchar Msk[32];//dot matrix code data };
//////////////////////////////////////////////////////////////////////////////// Chinese character font table//// Chinese character library: Song Dynasty 16.dot, horizontally modulo left high bit, data arrangement: from left to right, from top to bottom///////////////////////////////////////////////////////////////////////////////////conststructtypFNT_GB16 codeGB_16[]= //data table{/*---------------------------------------------------------------------------------;source file/text :Xu;width×height (pixels):16×16--------------------------------------------------------------------------------*/"Xu",0x10,0x80,0x10,0x80,0x21 ,0x40,0x42,0x20,0x94,0x10,0x1B,0xEC,0x20,0x80,0x60,0x80,0xAF,0xF8,0x 20,0x80,0x22,0xA0,0x24,0x90,0x2A,0x88,0x21,0x00,0x00,0x00,0x00,0x00, This structure is very simple: one is the internal code, and one is the dot matrix sequence. The previous dot matrix library was placed in the order of the internal code, and no internal code index was needed. If only some Chinese characters were placed, the internal code index was needed. (The Chinese character "Xu" in the front is to find the dot matrix sequence of the word when outputting "Xu". This dot matrix sequence is written by myself. When displayed with 1602, because the chip has an English dot matrix sequence in the memory, there is no need to write it.) Generally, two bytes are enough for the internal code. If you use one more byte, you just add a trailing 0. In this way, the Chinese character string can be directly placed in the Chinese character internal code;
codeGB_16[k].Index[0] codeGB_16[k] indicates that there is an array of structure typFNT_GB16 called codeGB_16 codeGB_16[k] is the k+1 member of the array index is a member of structure typFNT_GB16, so it can be referenced with codeGB_16[k].Index. At the same time, index is an array, so it can be indexed[0] If((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])) && is a logical AND operator, which means that the values on both sides of the && symbol are true. Only the value of && is true, that is, true && true =true. This sentence means: codeGB_16[k].Index[0]==c[0] and codeGB_16[k].Index[1]==c[1] are established at the same time. If the following statement is executed, codeGB_16[] is a structure array, codeGB_16[k].Index[0] means the 0th element value of the index member of the Kth structure of the structure array.
13. 12864 LCD: Each display point corresponds to a binary number, 1 means on, 0 means off. The RAM that stores these dot matrix information is called display data memory. To display a certain graphic or Chinese character is to write the corresponding dot matrix information into the corresponding storage unit.
The address counter (AC) of the graphics RAM will only automatically increment the horizontal address (X axis) by one. When the horizontal address = 0FH, it will be reset to 00H. However, it will not automatically increment the vertical address with a carry. Therefore, when multiple data are written continuously, the program needs to determine whether the vertical address needs to be reset.
14. Drawing RAM (GDRAM) The drawing display RAM provides 128×8 bytes of memory space. When changing the drawing RAM, first write the horizontal and vertical coordinate values continuously, and then write two bytes of data to the drawing RAM. The address counter (AC) will automatically increase the horizontal address (X address) by one. When the horizontal address is 0XFH, it will be reset to 00H; the vertical address will not be automatically incremented by 1. The drawing display must be turned off during writing to drawing RAM,
[cpp] view plain copy//Display Chinese characters voiddispString (uchar X, Y,uchar *msg)//Which row is X and which column is Y. msg is Chinese characters {if(X==0) write_data(*msg++); //Display Chinese characters }}////////////////////////////////// //////////////// ////////////////// Display image voiddisppicture(uchar code *adder){ uint i,j;//*******Display the upper half screen content settings for(i=0;i<32;i++)//32 column addresses in the upper half of the screen { write_com(0x80 + i);//SET vertical address VERTICALADD write_com(0x80);//SET horizontal address HORIZONTAL ADDfor(j=0;j<16;j++) { write_data(*adder); adder++; }}//************Display content settings in the lower half of the screen for(i=0;i<32;i++) //{ write_com(0x80 + i); //SET vertical address VERTICALADD write_com(0x88); //SET horizontal address HORIZONTAL ADDfor(j=0;j<16;j++){write_data(*adder);adder++;} }} For C language, space is automatically allocated for a defined variable, and its address is the name of the variable. Through this name, the data can be retrieved in the memory and new data can be obtained through calculation. However, in assembly, the programmer needs to define the storage space and send the data to the accumulator for calculation. Every step requires the programmer's operation. In C language, these processes are completed by the compiler.
15. Some useful questions and answers ①. How is the memory allocation of variables carried out in microcontroller C language? Could it be that the compiler intelligently adds allocation and recycling code during the compilation process? The key point is that the program I made, how can I ensure that it does not have memory overflow errors? If I'm doing a recursive operation, then the memory requirements are difficult to calculate by myself.
②. Will the microcontroller C language be restricted in variable definition? For example, the multiplication and division operations of floating-point data are written through assembly, and the code is quite complicated. If written directly in C language, wouldn't it be too simple?
③. In the hex file generated by the C language of the microcontroller, is the address distribution of the instruction and data ROM automatically assigned by the compiler? Can users assign it?
Answer 1: A microcontroller program written in C language is first compiled by a program (it seems to be c51.exe). After the compilation is completed, the storage space size of the variables has been arranged, but the specific address has not yet been allocated (the address is floating). Next, another program (it seems to be a51.exe) is connected. After the connection, the specific address is determined.
If there are too many variables, the compiler will prompt that the data segment is too large. To ensure that there is no memory overflow error, the main consideration is whether the stack overflows, and it depends on experience.
The C language of microcontrollers generally prohibits recursion, and recursive operations are generally avoided. After all, microcontrollers are not PCs, which will affect the speed. If you want to recurse, it is more appropriate to use a DSP chip. In short, you must be able to choose the appropriate chip.
Answer 2: The size of the variable (number of digits) is generally the same as the number of digits in the chip accumulator. For example, 51 commonly uses 8 bits because it is an 8-bit microcontroller.
The microcontroller can define bit variables, but it cannot define bit arrays. Writing in C language seems simple, but actually generates the largest amount of code. The microcontroller used for control hardly uses floating point operations, which is not only slow but troublesome and takes up space. If it is a DSP chip, it will be much better if it has a suitable hardware structure.
Answer 3: Generally, it is automatically allocated. It can be programmed in a mixture of C language and assembly language, or it can be assembled online using Keil C. The data exchange between the chip and the outside is done through the port.
Company phone number: +86-0755-83044319
Fax/FAX: +86-0755-83975897
Email: 1615456225@qq.com
QQ: 3518641314 Manager Li
QQ: 332496225 Manager Qiu
Address: Room 809, Building C, Zhantao Technology Building, No. 1079 Minzhi Avenue, Longhua New District, Shenzhen