当前位置: 首页 > news >正文

STM32如何使用内部晶振作为晶振

目录


前言

首先说明一下芯片内部并没有时钟, 而是内部振荡。
使用内部振荡的好处是外部无需设计晶振电路 ,再说的简单点 ,不用外部晶振依然可以让单片机正常运转。

环境:

芯片:STM32F103C8T6

Keil:V5.24.2.0


1.打开任意keil工程.打开system_stm32f10x.c

2.找到systeminit函数 全部注释掉

3.然后粘贴以下代码

  #define  USE_HSI   1                        // 是否使用内部晶振  0 不使用  1使用
void SystemInit ( void )
{#if USE_HSI{
//设置使用内部晶振/* 开启HSI 即内部晶振时钟 */RCC->CR |= ( uint32_t ) 0x00000001;/*选择HSI为PLL的时钟源HSI必须2分频给PLL*/RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLSRC_HSI_Div2;/*PLLCLK=8/2*9=36MHz  设置倍频得到时钟源PLL的频率*/RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLMULL6;                         //设置倍频后的频率/* PLL不分频输出 ?*/RCC->CFGR |= ( uint32_t ) RCC_CFGR_HPRE_DIV1;/* 使能 PLL时钟 */RCC->CR |= RCC_CR_PLLON;/* 等待PLL时钟就绪*/while ( ( RCC->CR & RCC_CR_PLLRDY ) == 0 ){}/* 选择PLL为系统时钟的时钟源 */RCC->CFGR &= ( uint32_t ) ( ( uint32_t ) ~ ( RCC_CFGR_SW ) );RCC->CFGR |= ( uint32_t ) RCC_CFGR_SW_PLL;/* 等到PLL成为系统时钟的时钟源*/while ( ( RCC->CFGR & ( uint32_t ) RCC_CFGR_SWS ) != ( uint32_t ) 0x08 ){ }}
#else{
//设置使用外部8M晶振/* Reset the RCC clock configuration to the default reset state(for debug purpose) *//* Set HSION bit */RCC->CR |= ( uint32_t ) 0x00000001;/* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CLRCC->CFGR &= ( uint32_t ) 0xF8FF0000;
#elseRCC->CFGR &= ( uint32_t ) 0xF0FF0000;
#endif /* STM32F10X_CL *//* Reset HSEON, CSSON and PLLON bits */RCC->CR &= ( uint32_t ) 0xFEF6FFFF;/* Reset HSEBYP bit */RCC->CR &= ( uint32_t ) 0xFFFBFFFF;/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */RCC->CFGR &= ( uint32_t ) 0xFF80FFFF;#ifdef STM32F10X_CL/* Reset PLL2ON and PLL3ON bits */RCC->CR &= ( uint32_t ) 0xEBFFFFFF;/* Disable all interrupts and clear pending bits  */RCC->CIR = 0x00FF0000;/* Reset CFGR2 register */RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)/* Disable all interrupts and clear pending bits  */RCC->CIR = 0x009F0000;/* Reset CFGR2 register */RCC->CFGR2 = 0x00000000;
#else/* Disable all interrupts and clear pending bits  */RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
#ifdef DATA_IN_ExtSRAMSystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
#endif/* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers *//* Configure the Flash Latency cycles and enable prefetch buffer */#ifdef VECT_TAB_SRAM. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else3 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif}#endif
}

测试频率为24Mhz,功能是正常的

 

 


总结

http://www.lryc.cn/news/519432.html

相关文章:

  • 【Maui】导航栏样式调整
  • 【黑马程序员三国疫情折线图——json+pyechart=数据可视化】
  • 如何实现多级缓存?
  • Saas数据库迁移单租户数据
  • LeetCode100之括号生成(22)--Java
  • 阿里云ios镜像源
  • 芯片:为何英伟达的GPU能在AI基础设施领域扮演重要角色?
  • Linux系统之hostname相关命令基本使用
  • Domain Adaptation(李宏毅)机器学习 2023 Spring HW11 (Boss Baseline)
  • 在php中,Fiber、Swoole、Swow这3个协程都是如何并行运行的?
  • SQLite PRAGMA
  • 使用python调用JIRA6 REST API及遇到的问题
  • 基于STM32的智能电表可视化设计:ESP8266、AT指令集、python后端Flask(代码示例)
  • 图片和短信验证码(头条项目-06)
  • 2501,wtl显示html
  • 嵌入式C语言:什么是指针?
  • 解锁 KaiwuDB 数据库工程师,开启进阶之路
  • ffmpeg7.0 aac转pcm
  • 【Pandas】pandas Series rdiv
  • 线程安全问题介绍
  • 为AI聊天工具添加一个知识系统 之27 支持边缘计算设备的资源存储库及管理器
  • 初识verilog HDL
  • VS2015 + OpenCV + OnnxRuntime-Cpp + YOLOv8 部署
  • Notepad++上NppFTP插件的安装和使用教程
  • Kotlin | Android Provider 的实现案例
  • 频域自适应空洞卷积FADC详解
  • Edge浏览器内置的截长图功能
  • GAN的应用
  • Math Reference Notes: 希腊字母表
  • 高通,联发科(MTK)等手机平台调优汇总