Know STM32 and how to build STM32 projects

Introduction to STM32

What is a microcontroller

Single-chip microcomputer (Single-Chip Microcomputer) is an integrated circuit chip, which combines the central processing unit CPU with data processing capabilities, random access memory RAM, read-only memory ROM, various /0 ports and interrupt systems, timers/counters and other functions ( It may also include a display driver circuit, pulse width modulation circuit, analog multiplexer, A/D converter and other circuits) integrated into a silicon chip to form a small and complete microcomputer system, which is widely used in the field of industrial control.

STM32 naming rules

ST -> STMicroelectronics

M -> Microelectronics Microelectronics

32 -> bus length

The follow-up bloggers about STM32 related blogs use the model STM32F103C8T6

STM32F103C8T6 MCU

The difference between the standard peripheral library and the HAL library

Standard Peripheral Library

The previous version of the STM32 standard peripheral library is also called the firmware function library or the firmware library for short. It is a firmware function package, which consists of programs, data structures and macros, including the performance characteristics of all peripherals of the microcontroller. The function library also includes driver descriptions and application examples for each peripheral, providing an intermediate API for developers to access the underlying hardware. By using the firmware function library, developers can easily apply each peripheral without having to master the details of the underlying hardware. set up. Therefore, using the solid-state function library can greatly reduce the user's programming time, thereby reducing the development cost. Each peripheral driver consists of a set of functions that cover all the functionality of the peripheral. The development of each device is driven by a common API (application programming interface) that standardizes the driver's structure, functions, and parameter names.

  • Encapsulate the underlying operations of registers and provide a complete set of interfaces (APIs) for developers to call

  • Each chip has written a library file, that is, stm32F1xx... and the like in the project file

  • Configure the variable members of the structure to modify the configuration registers of the peripherals to select different functions

  • Greatly reduce the difficulty of microcontroller development, but it is inconvenient to transplant between different chips

HAL library

HAL库 ,HAL是Hardware Abstraction Layer的缩写,中文名称是:硬件抽象层,HAL库工程一般使用STM32CubeMX软件来生成工程。HAL库是ST公司为STM32的MCU最新推出的抽象层嵌入式软件,更方便的实现跨STM32产品的最大可移植性。优势就是不需要开发工程师再关注所用MCU型号,只需要专注所以要的功能软件开发工作。而且是未来主推的方向,正在不断的推出更新。HAL库推出的同时,也加入了很多第三方的中间件,有RTOS,USB,TCP / IP和图形等等。和标准库对比起来,STM32的HAL库更加的抽象,ST最终的目的是要实现在STM32系列MCU之间无缝移植,甚至在其他MCU也能实现快速移植。

  • ST公司目前主力推的开发方式,新的芯片已经不再提供标准库

  • 为了实现在不同芯片之间移植代码

  • 为了兼容所有芯片,导致代码量庞大,执行效率低下

GPIO外设

GPIO是通用输入输出端口的简称,简单来说就是STM32可控制的引脚STM32芯片的GPIO引脚与外部设备连接起来,从而实现与外部通讯、控制以及数据采集的功能。

GPIO相关寄存器

每个GPI/O端口有两个32位配置寄存器(GPIOx_CRL,GPIOx_CRH),两个32位数据寄存器

(GPIOx_IDR和GPIOx_ODR),一个32位置位/复位寄存器(GPIOx_BSRR),一个16位复位寄存

器(GPIOx_BRR)和一个32位锁定寄存器(GPIOx_LCKR)。

GPIO的工作模式

输入浮空

输入浮空状态下,I/O口的电平状态是不确定的,完全是由外部的输入确定的,如果在该引脚浮空的情况下读取该引脚的电平是不确定的。

输入上拉

I/O口在无输入的状态下,保持着高电平。

输入下拉

I/O口在无输入的状态下,保持着低电平。

模拟输入

输入信号不经施密特触发器直接输入,输入信号为模拟量而不是数字量,而其他输入方式均是输入数字量。

推挽输出

当给予一个高(低)电平信号时,可以真正的输出高3.3v(低0v)电平。

开漏输出

  • 当给予一个低电平信号时,可以真正的输出低电平0v。

  • 当给予一个高电平信号时,无法真正的输出高电平,即高电平没有驱动能力,需要借助外部上拉电阻完成对外驱动,才能输出高电平5v。

推挽复用功能

此时I/O受内部外设控制,如UART的RX、TX等等

开漏复用功能

此时I/O受内部外设控制,如IIC的SCL、SDA等等

复位和时钟(RCC)

复位

系统复位

除了时钟控制器的RCC_CSR寄存器中的复位标志位和备份区域中的寄存器以外,系统

复位将复位所有寄存器至它们的复位状态。

系统复位事件

当发生以下任一事件时,产生一个系统复位:

  • NRST引脚上的低电平(外部复位)

  • 窗口看门狗计数终止(WWDG复位)

  • 独立看门狗计数终止(IWDG复位)

  • 软件复位(SW复位)

  • 低功耗管理复位

电源复位

电源复位将复位除了备份区域外的所有寄存器。

电源复位事件

当发生以下任一事件时,产生一个系统复位:

  • 上电/掉电复位(POR/PDR复位)

  • 从待机模式中返回

备份区复位

备份区域拥有两个专门的复位,它们只影响备份区域。

备份区复位事件

当发生以下任一事件时,产生一个系统复位:

  • 软件复位,备份区域复位可由设置备份域控制寄存器 (RCC_BDCR)中的BDRST位产生。

  • 在VDD和VBAT两者掉电的前提下,VDD或VBAT上电将引发备份区域复位。

时钟(RCC)

时钟打开,对应的设备才会工作。

时钟来源

三种不同的时钟源可被用来驱动系统时钟(SYSCLK):

  • HSI振荡器时钟(高速内部时钟)

  • HSE振荡器时钟(高速外部时钟)

  • PLL时钟(锁相环倍频时钟)

二级时钟源:

  • 40kHz低速内部RC(LSIRC)振荡器

  • 32.768kHz低速外部晶体(LSE晶体)

时钟树

对于时钟的学习,后续用到不同的外设时再对时钟进行不同深度的学习。

使用STM32CubeMX配置HSE时钟

  1. 配置HSE时钟为晶体/陶瓷谐振器(Crystal/Ceramic Resonator)

  1. 配置时钟

如何构建STM32工程(点灯)

  1. 使用STM32CubeMX新建一个工程

  1. 左上角输入板子对应的型号,然后选择型号STM32F103C8T6

  1. 配置SYS,Debug方式选择串行线

  1. 根据板子的原理图,找到LED灯对应的引脚

  1. 如原理图将(LED1、LED2)PB8、PB9配置成输出引脚,输出低电平

  1. 配置工程名称、工程路径

  1. 选择固件库

  1. 生成工程

打开工程

如何下载程序到STM32单片机

  1. 插入STLink烧录器,手动安装驱动程序

  1. 创建hex文件

  1. Debug方式选择ST-Link

  1. 编译和烧录文件

Guess you like

Origin blog.csdn.net/weixin_54076783/article/details/129244776