How to quickly master a new MCU

Publisher:kappa20Latest update time:2014-12-23 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:MCU Reference address:How to quickly master a new MCU

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

Taxi billing simulation schematic diagram + MCU source program
The taxi billing simulation schematic is as follows The microcontroller source program is as follows: #include reg51.h #include intrins.h unsigned code num ={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; unsigned char data x_pos, y_pos; sbit rs=P2^0; sbit rw=P2^1; sbit e=P2^2; sbit Control=P3^5; bit  
[Microcontroller]
Taxi billing simulation schematic diagram + MCU source program
The function of microcontroller watchdog circuit
The function of the watchdog: The watchdog timer is a counter whose basic function is to restart the system after a software problem or program runaway occurs. The watchdog counter counts automatically when it is working normally, and the program flow resets it to zero regularly. If the system is stuck or runaway somew
[Microcontroller]
The function of microcontroller watchdog circuit
Implementation of remote control searchlight system based on Holtek microcontroller
On stage, sometimes it is necessary to turn off all live lights except the searchlights to create a special effect. The searchlight must shine on the actor. Since the searchlight is hung high on the ceiling, when the actor moves, it is very inconvenient to rely on manual adjustment of the direction of the searchlight,
[Microcontroller]
Temperature alarm design based on 51 microcontroller
Abstract: Single-chip microcomputer technology has been popularized in our lives, work, scientific research, and various fields, and has become a relatively mature technology. This article will introduce a digital thermometer based on single-chip microcomputer control. This thermometer is a multi-functional thermomete
[Microcontroller]
Temperature alarm design based on 51 microcontroller
Design of intelligent management system for nickel-cadmium rechargeable batteries based on single chip microcomputer
As an alkaline battery, nickel-cadmium rechargeable batteries have special requirements for use, management and maintenance. If they are not managed well, used improperly, or maintained in time, they can easily lead to battery aging, failure, or even scrapping. Aiming at the different types of nickel-cadmium recharg
[Microcontroller]
Design of intelligent management system for nickel-cadmium rechargeable batteries based on single chip microcomputer
Accurate programming method for single chip microcomputer timer interrupt
The interrupt response delay time of the MCS-51 microcontroller depends on whether other interrupt service routines are in progress, or on what instruction is being executed. The interrupt response time in a single interrupt system is 3 to 8 machine cycles . Regardless of the cause of the error, their impact must be c
[Microcontroller]
A design scheme of RGB LED backlight source based on single chip microcomputer
0 Introduction Liquid crystal display (LCD) is a light modulating device that does not emit light by itself. Therefore, in order to clearly see the display content of the transmissive LCD, a backlight source needs to be matched to the LCD screen. The current backlight module generally uses a cold cathode tu
[Microcontroller]
A design scheme of RGB LED backlight source based on single chip microcomputer
PIC microcontroller temperature sensor DS18B20 C language program code
/*This temperature sensor is written by myself, the timing has been corrected, and the self-test delay time uses the Tianxiang temperature conversion module algorithm */ #include pic.h __CONFIG(0x3B31); #define uint unsigned int #define uchar unsigned char #define DQ RC1 #define DQ_DIR TRISC1 #define DQ_HIGH() TRIS
[Microcontroller]
PIC microcontroller temperature sensor DS18B20 C language program code
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号