Several aspects to understand and master the new MCU

Infineon / Mitsubishi / Fuji / Semikron / Eupec / IXYS

Several aspects to understand and master the new MCU

Posted Date: 2024-01-21

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. Therefore, for any MCU, we should mainly understand and master it from the following aspects:

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 MCU's RAM 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).

Characteristics of MCU: 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 function Consumption and so on.

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 use indirect methods to implement them. For example, the project being developed needs to communicate with the COM port of the PC, and If the selected MCU does not provide a UART port, you can consider using an external interrupt 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 functions of the MCU. 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 can be temporarily ignored, and the program design is written 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 MCU development experience 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 check 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 familiar with the instruction system, and you can even Remember it unconsciously.

Basic functions of MCU:

For most MCUs, the following functions are common and basic. For different MCUs, the way they are described may be different, but they are essentially the same:

TImer (timer): Although there are many types of TImer, they can be summarized into two categories: one is TImer with fixed time intervals, that is, its timing time is set by the system and cannot be controlled by the user program. The system only provides Several fixed time intervals are provided for user programs to choose from, 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 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 number (Prescale) and setting of preset number, etc. , 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 common applications is to use it to implement 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.

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; direct read and write IO port: such as the IO port of MCS-51 belongs to this Class IO port. When the read IO port instruction is executed, it is the input port; when the write IO port instruction is executed, it is automatically the output port; the input and output direction is set by programming: 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 and LED driver control buses, etc.; for the use of IO ports, an important point must be kept in mind: for the input port, there must be a clear level signal, ensure that it cannot float (can be achieved by adding a pull-up or pull-down resistor); for the output port, its output status level must consider its external connection conditions, and it should be ensured that it is not in the Standby or static state. There is either sourcing or sinking current.

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, the 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, the output port The interrupt function can also be triggered). 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.; and the other is the need to save power; measurement of signal frequency: in order to ensure that the signal Not to be missed, external interrupt is the 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 the decoding of Manchester and PWM encoding; button Detection and system wake-up: For the MCU that enters the Sleep state, it generally needs to be woken up through an external interrupt. The basic form is a button, which produces level changes through the action of the button; Communication interface: The communication interface provided by the MCU Generally include SPI interface, UART, I2C interface, etc., which are described as follows:

SPI interface: This type of interface is a 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. The popular saying is that it depends on who provides the clock signal. The party that provides the clock is the Master, and the opposite party is It is a Slaver; UART (Universal Asynchronous Receive Transmit): It is a 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, data check mode (odd check, even check or no check), stop bit (Stop Bit) length and Baud Rate can be flexibly set through program programming. Certainly. The commonly used method for 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 advantage is that multiple devices can be connected to this bus and identified and accessed through addresses; one of the benefits of the I2C bus is that it is very convenient to use software to implement it through the IO port, and its transmission data rate is completely controlled by SCLK. , can be fast or slow, unlike the UART interface, which has strict speed 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 MCUs), while some MCUs use a specific method to decide whether to open it, such as Samsung's KS57 series. As long as the program accesses the Watchdog register, it will automatically is turned on and cannot be turned off again. Generally speaking, the reset time of watchdog can be set programmably. The basic application of Watchdog is to provide a self-recovery capability for MCUs that crash due to unexpected failures.

Writing of MCU program:

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 Still a concise and effective programming language. For MCU programming, the basic framework can be said to be roughly the same, which is generally divided into three parts: the initialization part (this is the difference between MCU programming and PC), the main program loop body and the interrupt handler (see Figure 1 a and b), which are explained as follows:

Initialization: For the design of all MCU programs, initialization is a basic and important step, which generally includes the following aspects:

Mask all interrupts and initialize the stack pointer: the initialization part generally does not want any interrupts to occur; clear the RAM area of ​​the system and display the Memory: although sometimes it may not be completely necessary, from the perspective of reliability and consistency, especially to prevent In case of unexpected errors, it is still recommended to develop good programming habits; Initialization of IO ports: According to the requirements of the project application, set the input and output modes of the relevant IO ports. For the input port, you need to set its pull-up or pull-down resistor; For the output port, the level output must be set 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 interrupt should be set. Redundant interrupts that are not used must be turned off; initialization of other functional modules: For all peripheral function modules of the MCU that need to be used, corresponding settings must be made according to the requirements of the project application, such as UART communication, which needs to be set Baud Rate, data length, verification method and Stop Bit length, etc. For Programmer Timer, you must set its clock source, frequency division and Reload Data, etc.; Parameter initialization: Complete the birth of MCU hardware and resources. Finally, 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 MCU's RAM 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).

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 loops. bodies, and are converted between each other through status flags. For the main program body, the following modules are generally arranged:

Calculation program: 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 programs: mainly for the presence of external LEDs, LCD Driver application.

Interrupt handler: Interrupt programs are mainly used to handle tasks and events with high real-time requirements, such as detection of external sudden signals, detection and processing of keystrokes, 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 execute the specific transaction - this is very important. Especially for low-power, low-speed MCUs, timely response to all interrupts must be ensured.

Different MCUs have different processing methods for the arrangement of different task bodies. 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 button presses and display response requires high real-time performance, so this Generally, timed interrupts are used to handle button actions and data display; for high-speed MCUs, such as applications where Fosc is greater than 1MHz, since the MCU has enough time to execute the main program loop, it can only be executed in the corresponding Set various trigger flags in interrupts and place all tasks in the main program body for execution; in MCU programming, one thing that needs special attention is to prevent simultaneous access or settings in interrupts and the main program body. the same variable or data. 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), the data needs to be processed The processing place only sets the triggered flag. --This ensures that data execution is predictable and consistent.

In short, for MCU development, you must remember one thing: "All roads lead to Rome", nothing is impossible, the key is to see if the method is correct! The next step is to do more, do more, and think more.

#aspects #understand #master #MCU