STM32F4时钟配置的操作步骤

本文将介绍stm32f4时钟配置的操作步骤、并对比时钟配置前后led外设闪烁的快慢以及对应代码的讲解
一、使用默认时钟配置去执行代码、获取led闪烁的周期
如下图所示我们默认使用的是hsi(内部高速时钟)且不进行任何配置
最后我们系统时钟、以及外设获得的时钟频率为16mhz
主函数中的代码如下:
int main(void){ /* user code begin 1 */ /* user code end 1 */ /* mcu configuration--------------------------------------------------------*/ /* reset of all peripherals, initializes the flash interface and the systick. */ hal_init(); /* user code begin init */ /* user code end init */ /* configure the system clock */ systemclock_config(); /* user code begin sysinit */ /* user code end sysinit */ /* initialize all configured peripherals */ mx_gpio_init(); mx_usart3_uart_init(); /* user code begin 2 */ printf(this is clock system test2\\n); /* user code end 2 */ /* infinite loop */ /* user code begin while */ while (1) { mydelay(20); hal_gpio_togglepin(gpiof, gpio_pin_10); printf(togglepin\\n); /* user code end while */ /* user code begin 3 */ } /* user code end 3 */}自己写的延时函数如下:
void mydelay(uint8_t time){ uint8_t i; uint16_t j; for(i= 0; i < time; i++) { for(j = 0; j < 5000; j++) { } }}实际测得led闪烁的周期是在30ms左右
对stm32f4时钟进行配置后执行代码、获取led闪烁的周期
如下图所示我们对时钟进行配置:
由于时钟频率168mhz是16mhz 10.5倍,我们将mydelay(20)改为mydelay(210)
实际测得led闪烁的周期是在30ms左右
比对时钟配置前后、led闪烁周期的区别
由上可知led闪烁周期相同,但相同的延时周期内、配置后的168mhz系统时钟,运行的延时函数内的空指令数量是默认配置的10.5倍,综上时钟配置后使得指令运行的速度是原来的10.5倍
时钟部分的代码解析
systemclock_config();函数中就是我们在时钟树中设置的内容
void systemclock_config(void){ rcc_oscinittypedef rcc_oscinitstruct = {0}; rcc_clkinittypedef rcc_clkinitstruct = {0}; /** configure the main internal regulator output voltage */ __hal_rcc_pwr_clk_enable(); __hal_pwr_voltagescaling_config(pwr_regulator_voltage_scale1); /** initializes the rcc oscillators according to the specified parameters * in the rcc_oscinittypedef structure. */ rcc_oscinitstruct.oscillatortype = rcc_oscillatortype_hse; rcc_oscinitstruct.hsestate = rcc_hse_on; rcc_oscinitstruct.pll.pllstate = rcc_pll_on; rcc_oscinitstruct.pll.pllsource = rcc_pllsource_hse; rcc_oscinitstruct.pll.pllm = 25; rcc_oscinitstruct.pll.plln = 336; rcc_oscinitstruct.pll.pllp = rcc_pllp_div2; rcc_oscinitstruct.pll.pllq = 4; if (hal_rcc_oscconfig(&rcc_oscinitstruct) != hal_ok) { error_handler(); } /** initializes the cpu, ahb and apb buses clocks */ rcc_clkinitstruct.clocktype = rcc_clocktype_hclk|rcc_clocktype_sysclk |rcc_clocktype_pclk1|rcc_clocktype_pclk2; rcc_clkinitstruct.sysclksource = rcc_sysclksource_pllclk; rcc_clkinitstruct.ahbclkdivider = rcc_sysclk_div1; rcc_clkinitstruct.apb1clkdivider = rcc_hclk_div4; rcc_clkinitstruct.apb2clkdivider = rcc_hclk_div2; if (hal_rcc_clockconfig(&rcc_clkinitstruct, flash_latency_5) != hal_ok) { error_handler(); }}

智能软件仓储如何选择?华为云制品仓助力企业勇攀高峰
卫士通荣获鲲鹏应用创新大赛2022四川赛区-openEuler赛道二等奖
GD32单片机STM32远程下载手机程序升级固件下载局域网网页升级工具
超简易音频放大器的制作
char和string在西门子PLC中的格式
STM32F4时钟配置的操作步骤
Semaphore信号量概念及其介绍
医疗生物技术领域得到人工智能加持将会更加可靠
恩智浦推出灵活的物联网云平台,用于边缘设备的安全管理和连接
日本企业加速研发下一代电池 索尼为首
PLC供电电源加熔断器的必要性
5G VR时代来临,微型超高清OLED微显示器带来非凡体验
医疗保健产品将亮相2013 CES,新型便携设备时代来临
达实智能节能服务打造标杆案例
三大运营商携号转网服务即将开启,用户该如何选择
关于新能源汽车的优势和劣势
感觉“全世界”都在为5G让路?
地平线与MINIEYE达成战略合作,构建乘用车、商用车双市场智能最优解
大联大品佳集团推出基于Microchip产品的车载USB快速充电解决方案
直线电机的优缺点 直线电机和伺服电机的区别