STM32F407 Basic Summary Series (1)

I. Introduction

突然间想记录一下这些简单实用的东西,因为我发现曾经拥有的、奇妙的抑或是一些引以为豪、熟练的不能再熟练的东西终将会变成脑海中一种自我良好的感觉,当要提取出来用到的时候才发现你依旧是那个小白白,依旧要重新来过。技术时代,时间宝贵,方向远比努力重要,只有更优秀才能更优秀。
本章记录的都是一些超级简单,很基础却也是很有用的东西,旨在帮助那些和我一样曾经迷茫、陷入其中的人,当然有大佬批评指教一下最好。
刚开始记录因此东西杂乱无章,没关系,好的文章是要经过不断地不断地修改才能出来的。记录的东西不详尽没有固定时间,没关系,烂开头好过一动不动(其实是懒)。记录的东西很低级,没关系,都是从新手入门过来然后在来回之间反复横跳(哼,瞧不起谁)。

2. Some basic knowledge of STM32F407 (to be sorted out)

1 Peripheral settings related

1.1 GPIO use

The first step of getting started with single-chip microcomputer is to use the register to light the lamp. The register code of the light operation is as follows:
Take the low level of PF6 as an example

	/*开启 GPIOF 时钟,使用外设时都要先开启它的时钟*/
	RCC_AHB1ENR |= (1<<5);		
	/*PF6 MODER6 = 01b 输出模式*/
	GPIOF_MODER |= (1<<2*6);	
	/*PF6 OTYPER6 = 0b 推挽模式*/
	GPIOF_OTYPER |= (0<<1*6);
	/*PF6 OSPEEDR6 = 11b 速率100MHz*/
	GPIOF_OSPEEDR |= (3<<2*6);
	/*PF6 PUPDR6 = 01b 上拉模式*/
	GPIOF_PUPDR |= (1<<2*6);	
	/*PF6 BSRR寄存器的 BR6置1,使引脚输出低电平*/
	GPIOF_BSRR |= (1<<16<<6);

The process is to turn on the peripheral clock -> configuration mode register (input or output) -> output type register (if this is required for configuration output mode) -> output speed register (generally open the maximum if there is no requirement) -> pull-down register (generally Pull-up is high level, pull-down is low level) -> Set reset register (directly operate the pin as high and low level)

1.2 Timer PWM wave

configuration process

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//TIM时钟配置结构体
TIM_OCInitTypeDef TIM_OCInitStructure;//TIM PWM配置结构体
GPIO_InitTypeDef GPIO_InitStructure; //GPIO配置结构体
// 开启定时器相关的GPIO 外设时钟*/
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA,ENABLE);//GPIO时钟
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3,ENABLE);//TIM3定时器时钟
 /* 指定引脚复用功能*/
 GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3);//功能复用,注意这里GPIO_PinSource6和GPIO_Pin_6不能混淆。这个函数和GPIO_Init()先后无所谓。
 /* 定时器功能引脚初始化*/
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//GPIO_PuPd_NOPULL;//上拉下拉无所谓 都可以
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ================== 时基结构体初始化=================== */
 // 累计TIM_Period+1 个计数后产生一个更新或者中断
 // 当定时器从0 计数到1023,即为1024 次,为一个定时周期
 TIM_TimeBaseStructure.TIM_Period = 1024-1;
 // 高级控制定时器时钟源TIMxCLK = HCLK=42MHz
 // 设定定时器频率为=TIMxCLK/(TIM_Prescaler+1)=100KHz
 TIM_TimeBaseStructure.TIM_Prescaler = 420-1;
 // 时钟分频,在计算死区时间的时候会用到
 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
 // 计数方式
 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
 // 重复计数器,这里没使用
 TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
 // 初始化定时器TIMx, x[1,8]
 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

 /* ================== 输出结构体初始化=================== */
 // 配置为PWM 模式1,先输出高电平,达到比较值的时候再改变电平
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
 // 主输出使能
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 // 配置比较值
 TIM_OCInitStructure.TIM_Pulse = 0;
 // 主输出高电平有效
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 // 通道初始化	
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
 // 使能通道重装载
 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
// 使能定时器
 TIM_Cmd(TIM3, ENABLE);
// 主动输出使能
// TIM_CtrlPWMOutputs(TIM3, ENABLE);

The overall process is to open GPIO, TIM clock -> configure GPIO parameters (note whether the function is multiplexed) -> configure TIM timer parameters (mainly frequency division and counting - that is, input clock and counting period) -> TIM timer PWM wave Configuration (including duty cycle, mode, high and low level and channel) -> function open

1.3 Modification of External Crystal Oscillator Parameters

Different boards may have different crystal oscillators, and the places that need to be modified are as follows:

First modify the HSE_VALUE (external high-speed clock) in the stm32f4xx.h file, and then modify the
insert image description here
PLL_M (phase-locked loop frequency division coefficient) in system_stm32f4xx.c.
insert image description here
1s may be fast or slow.

2 Language and usage environment related

2.1 C/C++

Adding a header file in the project shows c/c++ mixed compilation and needs to modify the way of compiling the file.
Sometimes this kind of problem occurs. It is very strange. Obviously adding the header file and pressing F12 can also jump to the corresponding function. It just reports an error. Contrary to you, for this kind of problem, consider the problem of setting the price type of the header file that may be called (caused by c/c++ mixed compilation). For example, the main.c file type is
insert image description here
set to a c++ source file, and bsp_led.h is called in main. The source file corresponding to bsp_led.h is bsp_led.c, and its type is a c source file. After compiling, the above error will be caused. (Just change it to the c++ source file type, and the c++ modular idea will inevitably be used in the future)
insert image description here
insert image description here
insert image description here

2.2 F407 startup file

Sometimes we will encounter projects transplanted with different F4 chips, and the startup files are different. At this time, it will be embarrassing whether to replace the startup files. The actual measurement does not need to be replaced, just modify the macro definition in the magic wand. It is
insert image description here
insert image description here
not a big problem. Just modify the Define in C/C++ in the magic wand option. The startup file of 429 is used as follows, and the macro definition of 407 is defined when using it. Enough
insert image description here

3 Test verification related

3.1 PWM configuration IO pin

Configure the PWM wave IO pin to set pull-up or pull-down or not pull, it has no effect, the waveform is still output (maybe the level of the waveform is different for the first time)

3.2 General IO pins

After the measured pins are multiplexed, the use of the pin set reset function is invalid //GPIO_SetBits()|GPIO_ResetBits(), and the function multiplexing is equivalent to a switch that can only be operated individually.
insert image description here
According to some tutorials, the default level of GPIO pull-up and pull-down mode setting is different. After the execution of this code, the IO pin is still in a state of no level (not 0v, not 3.3v). It is best if there is a need Just manually set a set or reset later.
insert image description here

4 Troubleshooting related

4.1 Tips for reliable debugging

Some important debugging skills to ensure that the hardware circuit is fine. I once spent several hours debugging a program, from top-level logic investigation to bottom-level IO configuration, and finally found that there was a problem with the external circuit of the core board. It is also possible to prepare multiple boards for comparison and verification.
When the problem has not been checked out for a long time, it is best to list and record all possible problems one by one. Try to have a clear idea, from big to small, from simple to difficult. Two LEDs can be placed on the communication port to indicate communication
. Industrial-grade communication modules are equipped with two-way sending and receiving indicators, which is great. If there is a problem, you can look at the indicator to quickly locate the problem on the outside or inside.

3. Small ending

前边说的很虚,其实就是最近帮朋友看一个定时器输出PWM的基础问题,我找了半天没给人家解决,不仅啪啪打脸而且引发了一系列调试问题把我陷进去了【泪流满面】,最终我发现我连一个灯都点不起来,沮丧的亚批(可以想象一下一个单片机程序员半夜坐在那里在台灯的照射下对着电脑发呆)。星光不问赶路人,好在最后发现了问题(嘿嘿)。
要多开源记录一些东西帮自己的同时也是帮别人。

4. References

null

Guess you like

Origin blog.csdn.net/weixin_43058521/article/details/119481542