STM32 study notes_2 download, GPIO introduction

download

Keil compilation routine

Compile two buttons, one down for partial compilation and two down arrows for full compilation. For uncompiled files both buttons are equivalent.

After clicking compile, linking is a link, and the meaning of several data in the result represents the size:

type of data Occupies Flash or SRAM illustrate
Code Flash Code (occupies the size of FLASH)
RO-Date Flash Read-only data, generally refers to const modified data
RW-Data Flash and SRAM Readable and writable data whose initial value is non-zero
DAY-Date SRAM Readable and writable data whose initial value is 0

The sum of the first three items is occupied by FLASH, and the last two are occupied by SRAM.

Double-click the project name, a .map file will be opened, and the size of the above items and the occupied size of FLASH SRAM are also displayed at the end of the file.

Register-based method: the same method as the 51 single-chip microcomputer, the program directly controls the register, and the underlying method. However, there are too many STM32 registers, so this method is not suitable.

Based on library functions: STM32 self-encapsulated library functions. Helps improve development efficiency.

Method based on HAL library: It can be developed through a graphical interface. But the underlying logic is hidden.

register based

After we use the same method as the 51 microcontroller to create a new project file, it cannot be used directly. Startup files need to be added.

After adding the startup file and header file, create a new User folder. The code template is as follows:

#include "stm32f10x.h"                  // Device header
int main(){
    
    
	while(1){
    
    
		
	}
}

The encoding in Configration is set to UTF-8 to prevent Chinese garbled characters.

Magic Wand-Debug-Use is changed to STLink download method. On the right side of Setting-Flash Download- check Reset and Run, so that it will automatically reset and run after each download.

After the STLINK is connected to the corresponding line, the power light should be always on, and the test light should be flashing.

After Build and Load, the test light on the board should stop blinking. Because there is currently nothing in the program.

Next try lighting the lights. Three registers are required.

  1. RCC register. The RCC peripheral clock enable register can be seen from the manual as the peripheral of APB2, which is configured in RCC_APB2ENR.
image-20230131151014485

4 IOPC EN Enable turns on the GPIOC clock. Namely assignment: 0000 0000 0001 0000=0x00000010

  1. PC13 port. MODE13 is to configure PC13 port.

CNF13 should be set as push-pull output mode, that is, 00. MODE should be configured as output mode, the maximum speed is 50MHZ 11. So the register assignment is 0x0030 0000

image-20230131151852829
  1. Port output register write data. Port 13 is set to low level to light up. Namely: 0x00000000. 0x0000 2000 will be off.
image-20230131152925336
#include "stm32f10x.h"                  // Device header

int main(){
    
    
	RCC->APB2ENR=0X00000010;
	GPIOC->CRH=0X00300000;
	GPIOC->ODR=0X00000000;
	while(1){
    
    
		
	}
}

It can be seen that register-based programming is very troublesome! We need to constantly check the manual, and we can't affect other bits, we can't assign values ​​directly like this, we need to use & | to ensure that other bits are not affected.

Based on library functions

Create a new Library folder and add the library function files given by the teacher. Then add the group in MDK.

Open the STM32F10X.H file again, there is a sentence in it:

#ifdef USE_STDPERIPH_DRIVER
  #include "stm32f10x_conf.h"
#endif

That is to say, we need to define this thing before we can include this header file. Define it manually.

image-20230131154446413

Remember to include path too. Files marked with a key are read-only.

#include "stm32f10x.h"                  // Device header

int main(){
    
    
	//查询函数的通用方法:右键-转到定义查看,如果详细信息在注释里,复制注释关键词 crtl f 搜索。
	//enable clk
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//第一个参数:外设;第二个参数:新的状态。
	//set PC13 pin
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//通用推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	//set e level of PC13
	GPIO_SetBits(GPIOC,GPIO_Pin_13);//high level
	//GPIO_ResetBits(GPIOC,GPIO_Pin_13);//low level
	while(1){
    
    
		
	}
}

The contents written by the register part to find the pins are encapsulated in the library function, we only need to query and use.

Which startup file should I choose? STM32F100 is a super value series, select the startup file with VL, and then select LD MD HD according to the size of FLASH. STM32F101/102/103 choose the one ending with D. STM32F105/107 selects CL.

image-20230131202710800

Summarize the routine of new construction:

  1. Create a new project and select the corresponding chip model.
  2. Create a new start library folder and copy related configuration files.
  3. Create a new corresponding folder in the project and add the corresponding configuration file.
  4. include relevant path header files.
  5. Define USE_STDPERIPH_DRIVER in define
  6. Select the corresponding debugger (STLINK) in debug, and check settings-flash download-reset and run.

Of course, you can also create your own style projects in the future.

The principle of starting the program:

image-20230131203440962

Reset interrupt: program entry. When it starts at reset, it will call system_xx.c (init in our program) and main.c, and then it will end.

Other interrupts are also initialized in it. Defined in stm32f10x_it.c. If you want to define the interrupt system yourself, it is recommended to write it at PPP_IRQHandler in stm32f10x.c.

init sets up the startup of the microcontroller, initializes the flash interface, etc., and needs to be called only after reset.

Then the user files written by myself are also initialized in the purple part. Conducive to program modularization.

On the right are passively executed resources. Upper right corner: Peripherals, Kernel Peripherals. Bottom right corner: packaged library function file. The conf header file contains all header files and is included by stm32f10x.h.

GPIO General IO

General Purpose Input Output.

Can be configured as 8 input and output modes. Usually 0~3.3V, some pins allow 5V.

Serial download

flymcu software.

The configuration is as follows, must be exactly the same (the serial port can be different). I haven't been successful because I didn't see it clearly because of the low level reset.

image-20230412215004208

F1 baud rate can be up to 460800. F4 can be 76800.

Start mode (M3 M4)

BOOT1 BOOT0 corresponds to x0: Boot from main flash memory.

01: System memory starts booting (ISP download).

11: The built-in SRAM starts to boot.

image-20230412221036808

To run, it must be configured as x0 and stored in the main flash memory. Why can our circuit download to main memory by connecting 00 only once?

image-20230412221623258

If you don't download the circuit with one key, we have to do the same as the above steps, first 01 and then x0.

DAP download

Leave it blank for now. Because the blogger didn't buy an emulator.

Guess you like

Origin blog.csdn.net/jtwqwq/article/details/130143875
Recommended