开启外部晶振: 在Pinout&Configuration -> System Core -> RCC 页面,将 High Speed Clock (HSE) 配置为 Crystal/Ceramic Resonator
配置时钟频率: 在Clock Configuration 页面,将PLL Source 选择为 HSE,将System Clock Mux 选择为 PLLCLK,然后在HCLK (MHz) 输入72并回车,将HCLK频率配置为 72 MHz
配置I2C1: Pinout&Configuration -> Connectivity -> I2C1,将I2C模式选择为I2C,并在下方Configuration -> Parameter Settings,将I2C Speed Mode选择为Fast Mode
配置生成单独.c/.h文件: 在Project Manager -> Code Generator页面中,勾选Generate peripheral initialization as … per peripheral
打开串口2外设: Pinout&Configuration -> Connectivity -> USART2,将Mode选择为Asynchronous
启用float打印: 在cubeIDE菜单栏中,Project Properties -> C/C++ Build -> Settings -> Tool Settings -> MCU Settings,勾选Use float with printf … -nano默认情况下,sprintf函数不能打印小数。因此我们需要配置一下编译器,使其能够打印小数
配置ADC: 在Pinout&Configuration -> Analog -> ADC1 -> Configuration中
- ADC_Settings -> Continuous Conversion Mode设为Enable,使ADC转换持续进行,不需要每次获取之前手动触发转换
- ADC_Regular_ConversionMode -> Rank -> Sampling Time设为239.5 Cycles,最长采样时间,可以获得更稳定的转换结果
旋转编码器
- 配置TIM1: 在Pinout&Configuration -> Timers -> TIM1
- Mode -> Combined Channels设为Encoder Mode,具体channel都是disable使TIM1进入“编码器模式”
- Configuration -> Encoder -> Input Filter 设为 15,最大程度滤波,可以获得更稳定的效果
超声波测距
- 配置TIM1: 在Pinout&Configuration -> Timers -> TIM1
- Mode -> Clock Source 设为 Internal Clock,Channel3 设为 Input Capture direct mode,即输入捕获
- Configuration -> Parameter Settings -> Counter Settings -> Prescaler 设为 72-1,使定时器计数周期刚好为 1 us
- (可选)开启输入滤波,以提高稳定性:Configuration -> Parameter Settings -> Input Capture Channel 3 -> Input Filter,填写范围0 - 15,数值越大,滤波效果越强
- Configuration -> NVIC Settings -> 勾选TIM1 capture compare interrupt,开启捕获中断
CUBEMX常用配置
Author: XIAODENGWANG
Permalink: http://example.com/2024/08/23/CUBEMX%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE/
License: Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan: Do you believe in DESTINY?