The basic principles and functions of any MCU are similar, and the only difference is the configuration and number of its peripheral function modules, instruction system, etc. For the instruction system, although the form seems to be very different, it is actually just a different symbol. The meaning, function to be completed and addressing method are basically similar. Therefore, for any MCU, it should be understood and mastered from the following aspects:
* MCU features: To understand an MCU, you first need to know its ROM space, RAM space, number of IO ports, number of timers and timing methods, peripheral function modules (Peripheral Circuit) provided, interrupt sources, operating voltage and power consumption, etc.
* After understanding these MCU Features, the first step is to compare the functions of the selected MCU with the functions required for the actual project development, and clarify which resources are currently needed and which are not used in this project. For the functions that are needed in the project but not provided by the selected MCU, you need to carefully understand the relevant information of the MCU in order to achieve them indirectly. For example, if the developed project needs to communicate with the COM port of the PC, and the selected MCU does not provide a UART port, you can consider using external interrupts to achieve it;
* For the resources needed for project development, you need to carefully understand and read their manuals*, and 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 use this MCU for the first time, they may encounter many unclear descriptions of the MCU functions. For such problems, there are two ways to solve them. One is to write a special verification program to understand the functions described in the information; the other is to temporarily ignore it and write the program according to your current understanding in the program design, leaving it for modification and improvement during debugging. The former method is suitable for projects with more time and beginners, while the latter method is suitable for people with certain MCU development experience or when the project schedule is tight;
* Do not spend special time to understand the instruction system. The instruction system is just a symbol of logic description. When programming, you only need to check the relevant instructions according to your own logic and the logic requirements of the program. As the programming progresses, you will become more and more proficient in the instruction system and even memorize it unconsciously.
Basic functions of MCU:
For the vast majority of MCUs, the following functions are the most common and basic. For different MCUs, the description methods may be different, but in essence they are basically the same:
* Timer: Although there are many types of Timer, they can be summarized into two categories: one is a fixed time interval Timer, 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 (programmable timer), as the name implies, the timing of this type of Timer can be controlled by the user's program. The control methods include: clock source selection, frequency division number (Prescale) selection and pre-set number setting, etc. Some MCUs have all three, while others may have one or two of them. This type of Timer application is very flexible, and the actual use is also varied. One of the most common applications is to use it to achieve PWM output (specific applications will be introduced later). Since the clock source can be freely selected, this type of Timer is generally combined with the Event Counter (event counter);
* IO port: 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 and can only be input or output. It cannot be set in real time by software;
** Direct read and write IO port: 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 implement some bus-level applications, such as I2C bus, various LCD, LED Driver control bus, etc.;
** For the use of IO ports, it is important to remember that for the input port, there must be a clear level signal to ensure that it cannot float (this can be achieved by adding pull-up or pull-down resistors); and for the output port, its output state level must take into account its external connection conditions, and it should be ensured that there is no
pull current or sink current in the standby or static state.
* 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 state detection. The interrupt mode includes rising edge triggering, falling edge triggering and level triggering. External interrupts are generally implemented through input ports. If it is an IO port, the interrupt function will only be enabled when it is set as an input; if it is an output port, the external interrupt function will be automatically disabled (there are some exceptions in ATMEL's ATiny series, and the interrupt function can also be triggered 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.; the other is the need to save power;
** Measurement of signal frequency; in order to ensure that the signal is not missed, external interrupts are the most ideal choice;
** Data decoding: 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 Manchester and PWM encoding decoding;
** Key detection and system wake-up: For MCUs that enter the Sleep state, it is generally necessary to wake up through external interrupts. The most basic form is to press a key, and the level change is generated by the action of the key;
* Communication interface: The communication interface provided by the 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 layman's terms, it depends on who provides the clock signal. The party that provides the clock is the Master, and the other party is the Slaver;
** UART (Universal Asynchronous Receive Transmit): It is the most basic asynchronous transmission interface. Its signal lines are only 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 the data, the data check method (odd check, even check or no check), the length of the stop bit (Stop Bit) and the Baud Rate can be flexibly set through program programming. The most common way of this type of interface is to communicate data with the serial port of a PC.
** I2C interface: I2C is a data transmission protocol developed by Philips, which is also implemented using two signals: SDAT (serial data input and output) and SCLK (serial clock). Its biggest advantage is that multiple devices can be connected to this bus and identified and accessed by address; one of the biggest advantages of the I2C bus is that it is very convenient
to implement it through the IO port with software. The data rate of its transmission is completely controlled by SCLK, which can be fast or slow, unlike the UART interface, which has strict rate requirements.
* 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), while some MCUs use specific methods to determine whether it is turned on or not, such as Samsung's KS57 series. As long as the program accesses the watchdog register, it will automatically turn on and cannot be turned off. Generally speaking, the reset time of the watchdog can be set by the program. The most basic application of Watchdog is to provide a self-recovery capability for MCU when it crashes due to unexpected failures.
MCU program writing:
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 an efficient program code and a designer who likes to use assembly language, 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: initialization (this is the biggest difference between MCU programming and PC), main program loop and interrupt handler (see Figure 1 a and b), which are explained as follows:
* Initialization: For all MCU program designs, initialization is the most basic and important step, which generally includes
** Shielding all interrupts and initializing the stack pointer: The initialization part generally does not want any interrupts to occur;
** Clearing the system's RAM area and display memory: Although it may not be completely necessary sometimes, from the perspective of reliability and consistency, especially for preventing unexpected errors, it is recommended to develop good programming habits;
** Initialization of IO ports: According to the application requirements of the project, set the input and output mode of the relevant IO ports. For the input port, you need to set its pull-up or pull-down resistor; for the output port, you must set its output level to prevent unnecessary errors;
** Interrupt setting: For all interrupt sources required by the project, you should enable and set the trigger conditions for the interrupt, and for the redundant interrupts that are not used, you must turn them off;
** Initialization of other functional modules: For all peripheral functional modules of MCU that need to be used, corresponding settings must be made according to the requirements of the project application. For example, for UART communication, Baud Rate, data length, check mode and Stop Bit length need to be set, and for Programmer Timer, its clock source, frequency division number and Reload Data must be set;
** Parameters are created: After completing the creation of MCU hardware and resources, the next step is to initialize some variables and data used in the program. This part of the initialization 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 numbers, it is recommended to copy the relevant data to the MCU's RAM during initialization to increase the program's access speed to data and reduce the system's power consumption (in principle, accessing external EEPROM will increase the power consumption of the power supply).
* Main program loop: Most MCUs run uninterruptedly 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 loops, which are converted to each other through status flags. For the main program body, the following modules are generally arranged:
** Calculation program: Calculation program is generally time-consuming, so it is strongly opposed to put it in any interrupt for processing, especially multiplication and division operations;
** Processing program with low or no real-time requirements;
** Display transmission program: Mainly for applications with external LED and LCD Driver;
* Interrupt processing program: Interrupt program is mainly used to process 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 ensure that the code is 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 execute the specific transaction-this is very important, especially for low-power and low-speed MCUs, which must ensure timely response to all interrupts.
* 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 all handheld devices and use ordinary LCD displays, the response to keys and displays requires high real-time performance, so timed interrupts are generally used to process key actions and data displays; for high-speed MCUs, such as applications with Fosc>1MHz, since the MCU has enough time to execute the main program loop body, various trigger flags can be set in the corresponding interrupts, and all tasks can be placed in the main program body for execution;
* In MCU programming, one thing that needs special attention is to prevent the same variable or data from being accessed or set simultaneously in the interrupt and main program body. An effective prevention method is to arrange the processing of such data in one module, and determine whether to execute the relevant operations of the data by judging the trigger flag; and in other program bodies (mainly interrupts), only set the trigger flag where the data needs to be processed. - This can ensure that the execution of data is predictable and unique.
In short, for MCU development, we must remember one thing: "All roads lead to Rome", there is nothing that cannot be done, the key is to see whether the method is correct! And then do more, do more, and think more.
Previous article:Procedure for converting between Gregorian calendar date and lunar calendar date
Next article:Note on issues when using parallel port JTAG to simulate downloading of MSP430
Recommended ReadingLatest update time:2024-11-16 15:39
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How FRAM and LED lighting can illuminate stadiums
- 【ufun learning】01 Ufun unpacking and testing + development and compilation environment
- EEWORLD University Hall ---- Comments on the 2020 EDA Elite Competition New Thinking Competition Questions
- C2000 Software Serial Interface (SCI) Implementation Method
- 【ST NUCLEO-H743ZI Review】(1) Must-have lights for first experience
- [nRF52840 DK Review] +52840 NFC (Part 2)
- The compilation of the program sent by serial communication always fails
- How to select external clock for TI DSP?
- EEWORLD University Hall ---- Industrial innovation applications based on DLP
- About the power consumption optimization design in embedded DSP design