STM32 study notes (2)-familiar with the working environment

  First of all, you should be familiar with which parts of your development board are composed and how to use your development board. I use Wei Xue's F429 development board, which is a development board composed of a motherboard and the smallest system board: (Due to the epidemic situation, a few punctual atom development boards cannot be displayed, only this board , This board is not a good development board in the strict sense)

 

  To use the development board, you must know the power supply interface and download interface of the development board. The DC 5V in the upper left corner of this board is the power supply interface. The 20Pin JTAG port on the minimum system board is the download interface. In order to run according to the developer's wishes.

  Then talk about how to create a new STM32 software project, open the STM32CubeMX software, select New Project, enter your chip model in the search box that pops up, here F429IGT6 as an example:

 

 

     

 Choose the download method in the following interface:

 

  Then configure some details of the project:

 

 

   Note: The path and name must not have special symbols or Chinese characters.

  There are some details:

 

 

   Checking these two helps reduce the project volume and modular programming.

  Finally, generate the code:

  

 

 

 

  You can see that Project01 has been generated in the corresponding folder:

 

  Enter the project directory:

 

  Inc is a directory for storing header files. The header files automatically generated by the system are all in the inc, and the user's own header files can also be placed in the inc (of course, it is not necessary).

 

  src is the directory where source files are stored. The source files automatically generated by the system are all in src;

  MDK-ARM stores the keil project file. Open the keil project file to open the entire project:

  

 

  After Keil opens, as shown in the figure:

 

  First pay attention to the project area: this area shows the code files in the project. There are four directories in Project01, three of which are driver codes, that is, do not need to look at the series. For this Project01 project, we only need to look at Application / User. Main.c under the directory:

 

 

   Open main.c and you can see that a lot of code has been generated and marked with many comments. A series of copyright notices and disclaimers at the beginning do not need to read the series. Then look down, you will find this comment is very regular: first include includes, namely the header file, and then add gpio.h and main.h. Then annotate private includes, then USER CODE BEGIN includes and USER CODE END includes, translated is the private header file, the user private header file definition code begins, and the user private header file definition code ends. According to this note, we can know that if we want to add a header file in main.c, we need to write between the two comments of USER CODE BEGIN includes and USER CODE END includes. Then there are structure definition, pre-defined, macro definition, variable definition and so on. From here, you can see that the generated code is based on modular thinking, so when writing the program, you need to write according to the ST's comments, that is, the corresponding code is written in the corresponding code interval. If it is not written according to the comments, it is divided into two cases: the first one happens to be written between the previous END and the next BEGIN, then the next time CubeMX is used to generate code, the code in the wrong place will be cleared. This kind of situation often occurs when a novice debugs a program, then writes a bug, and then regenerates the code. The more the bug is generated, the more ridiculous it is; the second type is wrong in different categories, such as the header file declaration written in the structure definition In the interval, this will not cause the first problem, but it is recommended to write in the corresponding interval, which is a kind of polishing of the code style.

  Common code intervals that need to be understood are:

  USED ​​CODE 2: This section usually writes the user's initialization code;

  USER CODE 3: This section is the code in the While cycle;

  USER CODE 4: This section usually writes the interrupt callback function code.

  The above is a general understanding of the project. After completing these, start compiling the project:

 

  There are three buttons in the compilation area from left to right: translation, compilation, and recompilation.

  Translation is to convert the code into assembly. This step will check the syntax error of the code. If the code can pass, the translation will not report an error, and the corresponding assembly code will be generated but will not generate an executable file. Translation is often used during simulation ;

  Compiling is to compile only the files last modified in the project and other modules that depend on these modified files, and relink to generate an executable file. If it is a new project, the first compilation operation is to recompile;

  Recompile, compile all files once and generate an executable file. Recompilation takes longer than compiling, especially the code generated using CubeMX, so when you need to repeatedly modify the code and download and debug, don't make a mistake to recompile.

  Then adjust the download settings:

 

 

  

   The project downloader generated by default is ST-Link. If you use Jlink or Jlink OB, you need to modify the downloader.

  After connecting the downloader and the development board, click Setting to modify the download configuration:

 

  This window shows the information of the downloader and the chip. On the left is the SN code of the downloader. Note that Project is configured for SWD download mode, so the download port should also be configured as SW. On the right is the serial number of the chip. If the device is connected correctly, it should be as shown in the above figure. Usually, the missing serial number of the chip may be the chip is damaged or the SWD connection of the downloader is bad.

  Set it to automatically reset after downloading. If you do not set it to be manually reset after each download is completed, otherwise the old program still runs. Sometimes it is thought that the modified code has no effect may be caused by the reset:

 

 

   Then click to download:

 

 

 

 

   The text in the lower left corner shows that the download was successful. At this point, you have mastered the basic development methods of STM32, but don't worry, the way to go is still long.

  

 

 

Guess you like

Origin www.cnblogs.com/Wishengine/p/12670883.html