The resources of an 8-bit 51 MCU are often a maximum of K-100K of flash. 100-Kbytes of RAM, IO, serial port, timer, 8-bit data bus, AD, and other simple resources. The goal is clear, the structure is simple, and the instructions are simple. These characteristics also contribute to its deep-rooted presence in people's minds. It remains the leading experimental platform in colleges and universities and is also an application platform for many companies.
With the rise of STM32 based on the Cortex-M3 core in China, it attracted attention from 51 users. When I first entered, I thought STM32 was very fast, with large flash and RAM. It could operate an SD card, equivalent to the microcontroller's hard disk. USB function, something never seen before on 51, can now communicate with the computer without a serial port. With so many timers, how much PWM control can I do? The 16-bit FSMC bus enables high-resolution LCDs and high-speed control. No longer, 51 can only use some low-resolution and expensive LCMs like 12864. UCOS and UCGUI, previously thought of on 51, can run on STM32. There are many good features, and the controller can easily implement CAN communication and Ethernet applications that were previously difficult to combine circuits. This is the true meaning of the SOC chip in the field of micro-control.
Initially entering STM32, perhaps the most familiar is the Keil used in 51, called Keil C51 for 51 and RealView MDK-ARM for ARM, referred to as MDK, the current version is MDK4.22. The operation method is basically similar to Keil. Besides editing the project and compiling the code, we also use download and debug. For 51, there may be very few people using the simulation function, because 51 is simple enough, and the brain is often what you see. Downloading directly to the target board is faster for you. So the most common one for 51 is the downloader. But in the ARM phase, the resources are complex and the registers are complicated. There are so many variables, and without an emulator, you will feel helpless. Therefore, users of Cortex-M3 basically have emulators, which are generally divided into ST-LINK, ULINK, and JLINK, especially JLINK is most popular in China. The reason we all understand, the cost performance of JLINK V8 is the best among these. So, you need to have a JLINK after you get the MDK. It not only supports STM32, but also supports the vast majority of ARM chips.
When a 51 user first enters STM32, there is a short process in which platform conversion brings confusion, which is a cognitive difference caused by a large change in device type.
But adjust it, this discomfort will soon pass.
â–¶â–¶1 First look at the 51 and STM32 which are the same type of resources. Depending on how familiar you are with 51, you will see it in the STM32 manual. These tend to be simpler and the easiest to understand. Such as IO line control, and so on.
â–¶â–¶2 STM32 Advanced resources often require more energy to understand. This can be learned after getting started, such as USB, SDIO, etc.
â–¶â–¶4 Different programming methods, such as 51, can be easily controlled by setting or resetting instructions. In STM32, the function of all resources is related to the operation of the 32-bit register group corresponding to the resource. Therefore, for the setting and operation of resources, it is necessary to operate one or more registers. If multiple instructions are used to control, it will cause reading obstacles, and the code maintenance will be complicated in the future. Therefore, ST Company introduced the concept of library functions. Solve the problem of complex resource operations by executing library functions.
â–¶â–¶4 The MDK project of the STM32 routine has a similar program structure. Combined with the manual to see the routines, you will quickly form an understanding of the STM32 routine template. Once this understanding is formed, the remaining code details are like the blank fill you predicted.
When you are ready to learn a new platform, then don't hesitate to invest in the embrace of CORETEX-M3. It will make you progress to a new level. Bring you a pleasant technical enjoyment.
How to quickly get started with STM32 microcontroller?
There is a big god on the Internet saying that if you can 51 SCM and C language can get started STM32, only one day, is it really so fast. This depends on the standard of getting started for yourself.
Getting started in my eyes: (provided you have learned 51 microcontrollers and C language)
â–¶â–¶1 Know what information to refer to the official to learn, rather than getting caught in a lot of information.
â–¶â–¶2 know how to refer to the official manual and the official code to write your own program independently, instead of blindly seeing the code written by others, it feels very good.
â–¶â–¶3 Eliminate the fear of STM32 and eliminate the fear of library development. Learning is a happy and fulfilling process.
When you read this article, it will work better with the GPIO chapter of the STM32 Chinese Reference Manual, especially when it comes to register descriptions.
1, 51 and STM32 Introduction51 is an entry-level classic MCU in embedded learning. Because of its simple structure, easy to teach, and can be programmed through serial port without additional emulator, it is widely used in teaching. So many universities are embedded in it. It is still used in teaching. 51.51 Born in the 1970s, it belongs to the traditional 8-bit single-chip microcomputer. Nowadays, after years of baptism, it has both its brilliance and its shortcomings. Today's market products are highly competitive and extremely cost sensitive, and the requirements for MCUs are more demanding: more features, lower power consumption, easy-to-use interfaces and multitasking. Faced with these demands, 51 existing resources seem to be stretched. Therefore, no matter whether it is college teaching or market demand, a new MCU is urgently needed to inject new vitality into this field.
Based on the needs of this market, ARM introduced its new 32-bit Cortex-M3 microcontroller core based on the ARMv7 architecture. Shortly thereafter, ST (STMicroelectronics) introduced the MCU-STM32 based on the Cortex-M3 core. With its diversified product line, cost-effective, and easy-to-use library development, STM32 quickly became the brightest star in many Cortex-M3 MCUs. As soon as the STM32 went on the market, it quickly occupied the middle and low-end MCU market. It was favored by the market and engineers, and it was quite a spark.
As a qualified embedded engineer, in the face of emerging technologies, we are not deaf to the ears, but to meet the needs of the market as soon as possible, to keep up with the trend of technology. The emergence of STM32 is now a trend, a trend, what we have to do is to catch this fast train and make our technology more competitive.
51 Differences with STM32 architecture
We first popularize a concept, what is inside the microcontroller (ie MCU). The most important thing for a person is the brain, and all parts of the body work under the command of the brain. The MCU is very similar to the human body. Simply put, it is composed of one of the most important cores and other peripherals. The kernel is equivalent to the human brain, and the peripherals are like the various functional organs of the human body.
Let us briefly introduce the structure of the 51 and STM32.
1 51 System Structure
51 System Block Diagram
Figure 1 51 System block diagram
We say 51 generally refers to the 51 series of single-chip microcomputers, there are many models, the common ones are STC89C51, AT89S51, of which the most used in China is STC89C51/2, below we will explain with STC89C51, and with 51 abbreviation.
Kernel
51 consists of an IP core and on-chip peripherals. The IP core is the CPU in the above figure. The on-chip peripherals are as shown in the above figure: clock circuit, SFR and RAM, ROM, timer/counter, parallel I/O port, serial I/O port, interrupt system. The IP core is connected to the peripheral by the system bus and is 8bits with limited speed.
The 51 core was designed by Intel in the 1970s, and the speed is only 12M. The peripherals are added by the IC manufacturer (STC) based on the kernel. Different IC manufacturers will add different peripherals on the core to design Each unique microcontroller. Here intel belongs to the IP core manufacturer, and STC belongs to the IC manufacturer. The same is true for STM32, which we will talk about later. ARM is an IP core vendor, ARM is authorized by ST, and ST has designed an STM32 microcontroller based on the Cortex-M3 core.
Peripheral
When we were learning 51, there were fewer contacts on the kernel, and the most used ones were on-chip peripherals, which we operated while programming.
The registers that are operated during programming are located in the SFR and RAM sections, where the SFR (Special Function Register) occupies 128 bytes (actually only 26 bytes are used, only 26 registers, others belong to the reserved area), and the RAM occupies 128 bits. Bytes, the variables we define in the program are placed in RAM. The SFR and RAM are coincident in address, all in the 80~FF address range, but they are separated in the physical range, so the 51 RAM has 256 bytes.
The programmed program is written to the ROM area. The remaining peripherals are the IO ports we are familiar with, serial ports, timers, and interrupts.
2 STM32 system structure
STM32 system block diagram
Figure 2 STM32 system block diagram
Kernel
In terms of system architecture, STM32 and 51 are both microcontrollers, which are composed of core and on-chip peripherals. Only the Cortex-M3 core used by STM32 is much more complex and superior than 51. It also supports much more peripherals than 51, and the bus width also rises to 32bit, regardless of speed, power consumption, and peripherals.
From the block diagram, there is only one bus for comparing the 51 cores, and the fetch and fetch are shared. There are several bus interfaces inside the Cortex-M3 so that the CM3 can simultaneously address and access (access memory). They are:
Instruction memory bus (two), system bus, private peripheral bus. There are two code memory areas that are responsible for accessing the code memory area (ie, the FLASH peripheral), the I-Code bus and the D-Code bus.
I-Code is used for indexing, and D-Code is used for table lookup operations, which are optimized for optimal execution speed.
The system bus is used to access memory and peripherals. The area covered includes SRAM, on-chip peripherals, off-chip RAM, off-chip expansion devices, and part of the system-level memory area.
The private peripheral bus is responsible for accessing a portion of the private peripherals, primarily accessing the debug components. They are also in system level storage.
There is also an MDA bus. Literally, DMA is the meaning of data memory access. It is a bridge between the kernel and peripherals. It can access peripherals, memory, transfer without CPU control, and is two-way communication. In short, this guy is a data porter who is fast and not controlled by the boss. This is not available in 51.
Peripheral
From the block diagram, STM32 has much more peripherals than 51. There are 51 STM32 peripherals such as serial port, timer, and IO port. STM32 also has a number of special peripherals: FSMC, SDIO, SPI, I2C, etc. These peripherals are mounted on the three buses AHB, APB2, and APB1 according to the speed.
3, summary
Comparing the two aspects of the kernel and peripherals, STM32 is an upgraded version of the microcontroller. It adapts to the market, drains the trend, and shines in the low-end and mid-range microcontrollers.
2, the difference between learning methodsLearn 51 to learn the STM32 library with registers.
When we learn before 51, using a method of programming a register, want to achieve what effect, directly to the registers inside the assignment, the advantage is intuitive, simple and crude, know exactly did what, my heart at ease.
The reason why the direct operation register is feasible at 51, I think there are two reasons:
â–¶â–¶1 51 The main frequency is not high, the resources are limited, you must pay attention to the efficiency of program execution, and you can only operate the registers directly. The key places have to be compiled, not suitable for firmware libraries.
We must know that when we were learning 51 microcontrollers, we used assembly, even the current C programming is not, let alone library function programming.
â–¶â–¶2 51 The function is simple and there are not many registers. Domestic most widely popular STC89C52 for example, register all add up to less than 30. According to the function distinction, you can read the back of each register, and the function of each bit of the register can be remembered, and it is done at the time of programming.
Nowadays, from the 51 to the STM32 learning, many people still like to follow the 51 learning method. I can't accept the library. When I was learning the library, I was lost in confusion. After a few months of going back and forth, I didn't know if I had learned STM32. Because I was calling the library function in this way, I didn't operate the register at all. The heart is very unreliable. In fact, when you call the library function, there is no doubt in the mind, how is the bottom of the library implemented? Is there no courage to find out the bottom of the library? Finally, when we start tracking the bottom of the library function, we see a bunch of macro definitions, structures, pointers, various file inclusions, and the comments are all in English, is not a jealous.
In view of this, I want to summarize the reasons why many beginners are afraid that the library will not use the library for two reasons.
â–¶â–¶1 lack of C language knowledge points
The macro definition used by the library in implementing the register image, the cast, the structure used in defining the register, the pointer used in the peripheral initialization function, and the C language knowledge used in organizing the header file, etc., in the university course It is rarely involved, and most teachers are basically not talking about it. This knowledge is rarely used in some simple 51 microcontroller programming. Learning microcontrollers, doing embedded development, in fact, 80% of the work is related to C language programming, the remaining 20% ​​of the work is to read a variety of data manuals, familiar with a variety of hardware peripherals. So mastering these basic C language knowledge is a hurdle in embedded learning, and the STM32 library gives us a chance to improve C. Anything that can be found in books, I believe that we can basically learn, many beginners are not smart or diligent, just lack of directional guidance. For this lack of knowledge, we can take a little time to master, and the rest is constantly practicing debugging. Here I recommend a C book "C and Pointer" for everyone.
â–¶â–¶2 lack of program architecture design ideas
This is more difficult to do, and many people who learn well in C language are more difficult to master. Fortunately, we encountered the STM32 library, which gave us an excellent opportunity to learn and improve the C language. How the entire architecture of the library is built, how the code is written step by step: from the register image, to the encapsulation of the register, and then to the writing of the function, to the driver file corresponding to each peripheral function, which A lot of conditional compilation, the idea of ​​the file, is a nightmare for beginners who have just written a few lines of 51 microcontrollers. However, if you understand the relationship of this series, then you can understand the whole structure of the library, and you don't have to talk about operating the registers in the future.
If you don't like to use the library at first, it is taboo for library development, then ask yourself: Is my C language not good enough? The library is a new way of learning, it is a trend, I see it as another experience and promotion with the C language. Whether you use the library, only you have a shiny look.
3, use the register to light the LEDIn order to make a smooth transition to library development, at the beginning of STM32 programming, we will show you how to illuminate an LED with 51. How to use STM32 to illuminate an LED by operating the register, and then slowly explain what is the library, let Everyone knows the relationship between the library and the register.
1 illuminate an LED with 51
Before lighting an LED with STM32, let's review how to illuminate an LED with 51.
On the hardware, we assume that the 0th bit of the P0 port of the 51 MCU is connected to an LED, and the negative logic is on. If we want to light this LED, we will write this on the code:
Here we use the bus operation method, which means that 8 IOs of P0 port operate simultaneously, but only P0^0 is active.
In addition to this bus operation method, we also learn the bitwise operation. Using the 51 compiler's keyword sbit, we can define a bit variable:
Then LED = 0; the LED is lit, LED = 1; the LED is turned off. In order for the program to appear to be known, we define two macros:
The code to illuminate and turn off the LED becomes:
The above bus and bit manipulation methods, friends who have learned 51 are very familiar and easy to understand.
Then let's talk about a few points of knowledge that are easy to ignore.
â–¶â–¶ 1 What is a register?
When lighting the LED, we all use the method of operating the register to achieve, then everyone thought about it, what is this register? Why can we operate P0 directly?
Before answering the above questions, let's briefly introduce the main components of the 51 MCU, which is good for us to learn other MCUs.
Taking the domestic STC89C51 as an example, the MCU is mainly composed of 51 cores, peripheral IP, and bus. The kernel is produced by Intel Corporation. The peripheral IP is added by STC based on the kernel, such as timer, serial port, IO port, etc. The bus is the interface unit used to connect the kernel and peripherals. Intel is an IP nuclear design company here, and STC is an IC design company. There are only a handful of companies in the world that can design IP cores. The ARM company we are very familiar with is an IP core design company. ARM licenses other companies. Other IC companies design unique MCUs on the ARM core. The STM32 we will learn later belongs to the MCU based on the ARM core. .
The registers are built into each IP peripheral and are a type of memory used to configure peripheral functions. They are a type of memory and have the address you want to correspond to. Learning C language, we know that to operate these memories, you can use the pointers in the C language to operate these memory-registers with special functions by addressing. For example, if the address corresponding to the P0 port is 0X80, then if we want to modify the content of the memory corresponding to the address 0X80, we can do this according to common sense:
But when we compile, the compiler will report an error. In 51, only the SFR and SBIT keywords can be used to implement the register image. The address corresponding to the register cannot be directly manipulated. This is 51 different from STM32.
51 These registers of MCU are located in addresses 80H~FFH, corresponding to 128 addresses, but not every address is valid. There are 21 single-chip microcomputers of 51 series and 26 of 52 series. Others are reserved areas. .
Figure 3 51 Register Map
â–¶â–¶2 register map
In fact, when we are programming, we do not operate the registers by pointers, but directly assign values ​​to the port registers P0 and P1. Then how these peripheral resources establish a one-to-one correspondence with the address (register map definition), which benefits from the two unique keywords of 51: SFR and sbit, other microcontrollers do not, can only use other ways to implement registers Mapping. These two keywords help us to define all the registers, so we can manipulate the registers like one of the normal variables. In fact, the code that lights up the LEDs we mentioned at the beginning should look like this:
For convenience, we can write all the register maps in a header file, instead of defining them once for each register. In fact, this work does not need us to do, we will add a header file at the beginning when programming:
This header file has been implemented to define all the registers, which are included with the keil and can be found in the installation directory: Keil\C51\INC. This file implements the definition of byte registers and bit registers.
▶▶3 startup file—STARTUP.A51
There is also a startup code, this is also a place that many beginners can easily ignore. For this part, we mainly summarize its functions, and explain the code inside.
After the MCU resets, the first execution is the startup file - STARTUP.A51, instead of the main function we usually see. When we create a new 51 project, there will be a prompt: whether to copy the startup code to the current project, we generally choose Yes.
Figure 4 Whether to add the startup code
The startup code is written in assembly language and mainly implements the following functions: clear internal data memory, clear external data memory, clear external page memory, initialize re-stack and pointer in small mode, initialize re-stack in large mode, and Pointers, initialize restacks and pointers in compact mode, initialize 8051 hardware stack pointers, pass control commands that initialize global variables, or pass commands to the main function when global variables are not initialized. Then the program jumps to the main function and comes to the familiar C world.
â–¶â–¶4 summary
When explaining the use of 51 to illuminate the LED, we added what is the contents of the register, register mapping, and startup code. These three parts were originally explained in STM32, but considering that everyone already has the basis of 51. And I am familiar with 51, then I will add some content, everyone is naturally not so resistant, and can practice according to the above, and learn more deeply. Then when I explain these things in STM32, everyone will compare and learn, and there is no such jealousness for STM32.
2 Light up an LED with STM32
In contrast to the 51 way to illuminate the LED, we first use the STM32 to illuminate an LED, and then step through the code to build the simplest library function, let us know how the library is built.
Before we write the code, let's build a project. It should be noted that although 51 and STM32 are both keil, the MCU is different. The software should be installed in different directories during installation and cannot be installed in the English directory, otherwise it will conflict. We are using keil5, MDK5.15 version here.
â–¶â–¶1 New construction
Create a new project with KEIL5, put the project in a pre-built folder, and save the project named REG. Then add the startup file in the project directory: startup_stm32f10x_hd.s, which can be found from the KEIL5 installation directory, or from the ST library, and then add the startup file to the project.
▶▶2 startup file—startup_stm32f10x_hd.s
The startup file is written in assembly language, and the specific function is similar to the startup file in 51: STARTUP.A51.
The startup file of STM32 is mainly implemented:
1. Set the initial SP.
2, set the initial PC = Reset_Handler
3. Set the vector table entry address and initialize the vector table.
4. Call the library function SystemInit to configure the system clock to 72M. SystemInit is defined in the library file system_stm32f10.c.
5. Jump to the label _mian and finally come to the world of C. Here we first remove the details of the branches, focus on the main points, mainly understand the Test Chamber,Climatic Test Chamber,Humidity Test Chamber,Temperature Humidity Test Chamber Wuxi Juxingyao Trading Co., Ltd , https://www.juxingyao.com