STM32F0xx 微控制器的时钟配置

合集下载

STM32系列MCU硬件实时时钟(RTC)应用笔记说明书

STM32系列MCU硬件实时时钟(RTC)应用笔记说明书

2017年6月Doc ID 018624 Rev 1 [English Rev 5]1/45AN3371应用笔记在 STM32 F0、F2、F3、F4 和 L1 系列MCU 中使用硬件实时时钟(RTC )前言实时时钟 (RTC) 是记录当前时间的计算机时钟。

RTC 不仅应用于个人计算机、服务器和嵌入式系统,几乎所有需要准确计时的电子设备也都会使用。

支持 RTC 的微控制器可用于精密计时器、闹钟、手表、小型电子记事薄以及其它多种设备。

本应用笔记介绍超低功耗中等容量、超低功耗大容量、F0、F2和 F4 系列器件微控制器中嵌入式实时时钟 (RTC) 控制器的特性,以及将 RTC 用于日历、闹钟、定时唤醒单元、入侵检测、时间戳和校准应用时所需的配置步骤。

本应用笔记提供了含有配置信息的示例,有助于您快速准确地针对日历、闹钟、定时唤醒单元、入侵检测、时间戳和校准应用配置 RTC 。

注:所有示例和说明均基于 STM32L1xx 、STM32F0xx 、STM32F2xx 、STM32F4xx 和STM32F3xx 固件库,以及 STM32L1xx (RM0038)、STM32F0xx (RM0091)、STM32F2xx (RM0033)、STM32F4xx (RM0090)、STM32F37x (RM0313) 和 STM32F30x(RM0316) 的参考手册。

本文提到的STM32 指超低功耗中等容量、超低功耗大容量、F0、F2 和 F4 系列器件。

超低功耗中等 (ULPM) 容量器件包括 STM32L151xx 和 STM32L152xx 微控制器,Flash 容量在 64 KB 到 128 KB 之间。

超低功耗大 (ULPH) 容量器件包括 STM32L151xx 、STM32L152xx 和 STM32L162xx 微控制器,Flash 容量为 384 KB 。

F2 系列器件包括 STM32F205xx 、STM32F207xx 、STM32F215xx 和 STM32F217xx 微控制器。

STM32入门教程--系统时钟配置

STM32入门教程--系统时钟配置

STM32 系统时钟配置一、STM32的时钟系统时钟是什么?时钟通常是振荡器(如晶振)产生的特定频率的方波信号,时钟周期是时钟频率的倒数,时钟频率1MHz时钟周期为1/1000000=1us。

时钟周期是MCU处理指令的最小时间单元,每个程序指令都需要若干个时钟周期,MCU的时钟频率越快,完成一个指令的时间就越短,速度就越快。

时钟是MCU运行的基础,好比MCU的脉搏,是MCU性能的重要参数。

每个MCU 都是在某个特定的时钟频率下进行工作的,如C51单片机时钟频率为12MHz,而STM32F103 的系统时钟频率是72MHz。

STM32的时钟系统STM32时钟频率较高,时钟越快功耗越大,同时抗电磁干扰能力也会越弱。

而且STM32外设非常多,而通常外设是不需要像系统时钟那么高的频率的,比如看门狗和RTC 只需要几十K的时钟即可。

另外实际使用的时候通常只会用到有限的几个外设,STM32可以只给需要启动的外设分配时钟,以此来降低功耗。

由此可看出STM32 的时钟系统较为复杂,它采用了多个时钟源的方法来解决这些问题。

STM32 有4个独立时钟源:HSI、HSE、LSI、LSE。

①、HSI是高速内部时钟,RC振荡器,频率为8MHz,精度不高。

②、HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外部时钟源,频率范围为4MHz~16MHz。

③、LSI是低速内部时钟,RC振荡器,频率为40kHz,提供低功耗时钟。

④、LSE是低速外部时钟,接频率为32.768kHz的石英晶体。

其中LSI是作为IWDGCLK(独立看门狗)时钟源和RTC时钟源而独立使用,HSI高速内部时钟 HSE高速外部时钟 PLL锁相环时钟这三个经过分频或者倍频作为系统时钟来使用基本时钟源(图中绿色箭头指出):(1)HSI高速内部时钟,RC振荡器,8MHz。

(2)HSE高速外部时钟,石英/陶瓷谐振器,8MHz。

(3)LSI低速内部时钟,RC振荡器,40kHz。

STM32F0xx 微控制器的时钟配置介绍

STM32F0xx 微控制器的时钟配置介绍

2 年 05 月
文档 ID 022837 第 1 版
1/16

目录
目录
AN4055
1
术语表 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 专家模式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4
已知限制 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
文档 ID 022837 第 1 版
3/16
3
图片索引
图片索引
AN4055
图 1. 图 2. 图 3. 图 4. 图 5. 图 6. 图 7.
时钟结构图 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 I2S 时钟结构图 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 向导模式用户界面 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 选择时钟源 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 文件生成错误 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 专家模式用户界面 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 超出系统时钟频率 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

STM32时钟和GPIO配置

STM32时钟和GPIO配置

STM32时钟和GPIO配置STM32F1xx 系统时钟来源:STM32F1中⽂参考⼿册 6.2时钟时钟的作⽤决定了程序执⾏的速度,给芯⽚提供⼀个稳定的执⾏频率1. STM32F103R8 最⾼速率是多少??72 MHz maximum frequency2. 如果采⽤最⾼频率:执⾏⼀条指令 1/72M s ==> 1/72us3. 精简指令集:⼏乎所有的指令都是消耗⼀个时钟节拍(1/72 us)执⾏R8的时钟来源1. ⾼速外部时钟信号(HSE) 4 – 16M 给系统时钟提供时钟信号2. ⾼速内部时钟信号(HSI) 内部RC振荡器3. 低速外部时钟信号(LSE) 32.768 给RTC实时时钟提供时钟信号4. 低速内部时钟信号(LSI)系统时钟来源1. HSI振荡器时钟2. HSE振荡器时钟3. PLL时钟R8的时钟树和外设分布来⾃:STM32F103R8数据⼿册 2.1 Device overviewSTM32时钟的配置1. PLL的倍频因⼦:HSE * PLLMUL = 72M Hz2. AHB的频率:72M Hz3. APB2的频率:72M Hz4. APB1的频率:36M Hz修改HSE_VALUE设置函数static void SetSysClockTo72(void)中的值GPIO的配置GPIO的作⽤GPIO通⽤的输⼊输出外设数字接⼝:0/10 -- TTL电平:0v~1.5v1 -- TTL电平:2.5v~5vSTM32F103C8 : 0 => 0v ± 0.1 1 => 3.3v ± 0.3vSTM32 中GPIO⼝如何表⽰(理解)PA0 PA1 PA2..... PA15 ; PB0 .....P port 端⼝A B C.... 端⼝号0 1 ..... 15 端⼝位每个端⼝最多有16个端⼝位PA1 端⼝A 的第1位PA0 端⼝A 的第0位GPIO的相关模式输⼊:4种模拟输⼊:输⼊的模拟量,⽤于ADC转化。

STM32入门系列-STM32时钟系统,时钟使能配置函数

STM32入门系列-STM32时钟系统,时钟使能配置函数

STM32⼊门系列-STM32时钟系统,时钟使能配置函数 之前的推⽂中说到,当使⽤⼀个外设时,必须先使能它的。

怎么通过库函数使能时钟呢?如需了解寄存器配置时钟,可以参考《STM32F10x中⽂参考⼿册》“复位和时钟控制(RCC)”章节,其中有详细的寄存器介绍。

固件库已经把时钟相关寄存器的使能配置都封装好,放在stm32f10x_rcc.c和stm32f10x_rcc.h中。

只需要打开stm32f10x_rcc.h⽂件,会发现有很多的宏定义和时钟使能函数的声明。

这些时钟函数可⼤致分为三类。

⼀类是外设时钟使能函数,⼀类是时钟源和倍频因⼦配置函数,还有⼀类是外设复位函数。

当然还有⼏个获取时钟源配置的函数。

下⾯就来简单介绍下这些函数的使⽤。

⾸先看⼀下时钟使能函数,时钟使能函数包括外设时钟使能和时钟源使能。

外设时钟使能相关函数如下:void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);上⾯3个时钟使能函数也正是STM32的3条总线(这个在前⾯介绍存储器与寄存器章节讲过)。

由于STM32的外设都是挂接在AHB和APB 总线上的,所以要使能外设时钟,也就是使能对应外设所挂接的总线时钟。

⽐如GPIO外设它是挂接在APB2总线上的,如果使⽤GPIO外设,就需要先使能APB2总线时钟,使能时钟代码如下。

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph,FunctionalState NewState);要想哪个外设挂接在哪个总线上呢,可以通过STM32中⽂参考⼿册查找,还可以在固件库stm32f10x_rcc.h⽂件中查找。

STM32F时钟配置1

STM32F时钟配置1
//前面要想得到的 SYSCLK 在这里就知道了。因为 PLL 输出作为系统时钟,即 //SYSCLK=PLLCLK=36M
//程序中需要用的其他时钟也可以确定了。 //因为 APB1 Prescaler=1(没有分频),所以 TIMxCLK=PLCK1=36M //因为 APB2 Prescaler=1(没有分频),所以 TIM1CLK=PLCK2=36M //在 SystemInit 中 ADCPRE:ADC 预分频 00:PCLK2 2 分频后作为 ADC 时钟,即 ADCCLK=PCLK2/2,所以 ADCCLK=18M
FLASH->ACR |= FLASH_ACR_PRFTBE;
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
CanolaFlower
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
RCC->CIR = 0x009F0000;
SetSysClock(); }
static void SetSysClockTo36(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
//注释为:SYSLCK,HCLK,PCLK2 和 PCLK1 的配置
RCC->CR |= RCC_CR_PLLON;
while((RCC->CR & RCC_CR_PLLRDY) == 0) { }
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;

STM32时钟详细配置

STM32时钟详细配置

STM32时钟配置STM32时钟配置步骤// 开启HSI时钟寄存器操作1).开启高速时钟HSE // 设置时钟控制寄存器RCC_CR 位16 置1使能RCC->CR|= 0x00010000;位16 :HSEON:外部高速时钟使能当进入待机和停止模式时,该位由硬件清零,关闭4-16MHz外部振荡器。

当外部4-16MHz 振荡器被用作或被选择将要作为系统时钟时,该位不能被清零。

2).等待高速时钟就绪// 读取时钟控制寄存器RCC_CR位17为1就位while(!(RCC-> CR>>17));位17:HSERDY:外部高速时钟就绪标志由硬件置’1’来指示外部4-16MHz振荡器已经稳定。

在HSEON位清零后,该位需要6个外部4-25MHz振荡器周期清零。

3).设置APB1,APB2,AHB分频系数// 设置时钟配置寄存器RCC_CFGRRCC_CFGR=0x00000400;(AHB :位4-7, (低速)APB1 :位8-10, (高速)APB2 :位11-13)位7:4:HPRE[3:0]:AHB预分频(AHB Prescaler)0xxx:SYSCLK不分频位10:8:PPRE1[2:0]:低速APB预分频(APB1) 100:HCLK 2分频位13:11:PPRE2[2:0]:高速APB预分频(APB2) 0xx:HCLK不分频4).设置PLL倍频// 配置时钟配置寄存器RCC_CFGR 位18-21RCC_CFGR|=7<<18;位21:18:PLLMUL:PLL倍频系数0111:PLL 9倍频输出5).PLL输入时钟源选择// 配置时钟配置寄存器RCC_CFGR 位16RCC_CFGR|=1<<16;位16:PLLSRC:PLL输入时钟源(PLL entry clock source) 1:HSE时钟作为PLL输入时钟。

由软件置’1’或清’0’来选择PLL输入时钟源。

STM32F2xx的时钟说明

STM32F2xx的时钟说明

STM32F207的时钟说明请尽可能参阅英文原文件STM32F207有三个可选的时钟源,具体如下:(1)HIS时钟源(内部集成的时钟源)(2)HSE时钟源(外部高速晶体振荡器提供的时钟源或来自其他设备的时钟源)(3)PLL(PLL的时钟输入可以是HIS或者HSE时钟源)时钟控制器在选择内核及外设的时钟时具有高度的灵活性,以使他们都可以工作在最高频率上,并且能够保证给那些需要特殊频率的外设提供合适的频率,如Ethernet, USB OTG FS 和HS,I2S和SDIO。

三个可选的时钟源都可以独立地打开或关闭,以降低系统的功耗。

设备还有两个次要的时钟源,具体如下:(1)32KHz低速内部RC,可以驱动独立的看门狗和可以选择性使用的RTC(可用于在停止或待机状态下自动唤醒设备)(2)32.768KHz的低速外部晶体,可以作为RTC的时钟源。

几个预分频器用于配置AHB总线的频率,高速APB总线(APB2)和低速APB总线(APB1)所控制的区域的频率。

AHB总线的最高频率是120MHz,APB2总线的最高频率是60MHz,APB1总线的最高频率是30MHz。

所有的外设时钟都来源于系统时钟(SYSCLK),以下的几个除外:(1)USB OTG FS时钟(48MHz),随机模拟产生器(RNG)时钟(<=48MHz),SDIO时钟(<=48MHz),这些时钟来源于一个特殊的PLL(PLL2)的输出(PLL48CLK)。

(2)I2S时钟:为了达到高品质的音频效果,I2S时钟可能从特殊的PLL(PLLI2S)或外部接在I2S_CKIN引脚上的时钟得到。

(3)USB OTG HS(60MHz)时钟,由外部硬件提供。

(4)Ethernet MAC时钟(TX,RX和RMII)可以从外部硬件得到。

Ethernet使用时,AHB总线时钟不得低于25MHz。

RCC模块将AHB时钟8分频后提供给了Cortex系统定时器System Timer。

STM32F0系列寄存器操作02RCC时钟配置

STM32F0系列寄存器操作02RCC时钟配置

STM32F0系列寄存器操作02RCC时钟配置RCC(Reset and Clock Control)模块是STM32F0系列微控制器中的一个重要模块,用于配置系统时钟。

在这里,我们将详细介绍如何通过寄存器操作来配置RCC模块。

RCC模块的寄存器位于设备的的地址空间中,通过写入特定的值来配置和控制系统时钟。

以下是与RCC模块相关的一些重要寄存器:1. RCC_CR(RCC Control Register):用于配置内部和外部时钟、使能外部时钟和使能内部时钟等。

2. RCC_CFGR(RCC Configuration Register):用于配置系统时钟源、时钟分频等。

3. RCC_AHBENR(RCC AHB Peripheral Clock Enable Register):用于使能或禁用AHB总线上的外设时钟。

4. RCC_APBENR(RCC APB Peripheral Clock Enable Register):用于使能或禁用APB总线上的外设时钟。

根据需求,我们可以按照以下步骤配置RCC模块:1.确定系统时钟源:使用RCC_CFGR寄存器来选择主时钟源,可以从内部时钟(HSI或HSI48)、外部时钟(HSE或HSE32)或PLL时钟中选择。

2.配置时钟分频:使用RCC_CFGR寄存器来设置HCLK、PCLK1和PCLK2的分频系数,以确定这些时钟频率。

3.使能外设时钟:使用RCC_AHBENR和RCC_APBENR寄存器来使能或禁用需要使用的外设时钟。

下面是一个示例代码,用于配置STM32F0系列微控制器的RCC模块:```c#include "stm32f0xx.h"void RCC_Config(void)//使能外部时钟RCC->CR,=RCC_CR_HSEON;//等待外部时钟稳定while(!(RCC->CR & RCC_CR_HSERDY));//配置PLL时钟RCC->CFGR,=RCC_CFGR_PLLSRC_HSE_PREDIV;RCC->CFGR,=RCC_CFGR_PLLMUL6;//使能PLL时钟RCC->CR,=RCC_CR_PLLON;//等待PLL时钟稳定while(!(RCC->CR & RCC_CR_PLLRDY));//配置系统时钟源为PLL时钟RCC->CFGR,=RCC_CFGR_SW_PLL;//等待系统时钟源切换完成while((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);//配置时钟分频RCC->CFGR,=RCC_CFGR_HPRE_DIV1;//AHB时钟不分频RCC->CFGR,=RCC_CFGR_PPRE_DIV2;//APB1时钟分频为2//使能外设时钟RCC->AHBENR,=RCC_AHBENR_GPIOAEN;RCC->APBENR,=RCC_APBENR_TIM2EN;```在这个示例中,首先使能外部时钟(HSE)并等待其稳定,然后配置PLL时钟和分频系数。

时钟配置工具为STM32F2xx微控制器

时钟配置工具为STM32F2xx微控制器

November 2011Doc ID 018603 Rev 21/21AN3362Application noteClock configuration tool for STM32F2xx microcontrollersIntroductionThis application note presents the clock system configuration tool for the STM32F2xx microcontroller family.The purpose of this tool is to help the user configure the microcontroller clocks, taking into consideration product parameters such as power supply and Flash access mode.The configuration tool is implemented in the “STM32F2xx_Clock_Configuration_VX.Y .Z.xls” file which can be downloaded from .This tool supports the following functionalities for the STM32F2xx:●Configuration of the system clock, HCLK source and output frequency●Configuration of the Flash latency (number of wait states depending on the HCLK frequency).●Setting of the PCLK1, PCLK2, TIMCLK (timer clocks), USBCLK, and I2SCLK frequencies.●Generation of a ready-to-use system_stm32F2xx.c file with all the above settings(STM32F2xx CMSIS Cortex-M3 Device Peripheral Access Layer System Source File).The STM32F2xx_Clock_Configuration_VX.Y .Z.xls is referred to as “clock tool” throughout this document.Before using the clock tool, it is essential to read the STM32F2xx microcontroller reference manual (RM0033). This application note is not a substitute for the reference manual.This tool supports only revision B and revision Y of the STM32F2xx.Note:For VX.Y .Z, please refer to the tool version, example V1.0.0Contents AN3362Contents1Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.1Software requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2Hardware requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2.2Clock scheme for STM32F2xx microcontrollers . . . . . . . . . . . . . . . . . . . 72.2.3I2S clock generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.1Wizard mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.1.1Step-by-step procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.2Expert mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 4Known limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 6Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202/21Doc ID 018603 Rev 2AN3362List of tables List of tablesTable 1.Definition of terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Table 2.Number of wait states according to Cortex-M3 clock frequency. . . . . . . . . . . . . . . . . . . . . . 9 Table 3.Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Doc ID 018603 Rev 23/21List of figures AN3362 List of figuresFigure 1.Clock scheme for Silicon revision B and revision Y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 2.I2S clock generator architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 3.Wizard mode user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 4.HSE value out of range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Figure 5.VDD out of range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Figure 6.HCLK error message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 7.Select the clock source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 8.No possible configuration error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 9.File generation error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 10.Expert mode user interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Figure 11.System clock frequency is exceeded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Figure 12.The PLL input frequency is exceeded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Figure 13.The I2S frequency is out of range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 4/21Doc ID 018603 Rev 2AN3362Glossary 1 GlossaryTable 1.Definition of termsTerm DescriptionHCLK AHB clockPCLK1APB1 clockPCLK2APB2 clockTIMCLK Timer clockUSB OTG FS USB on-the-go at full-speedF CPU Cortex-M3 clockExt.Clock External clockV DD Power supplyHSI High-speed internal clockHSE High-speed external clockMCLK Master clockI2S Integrated interchip soundFs Sampling frequencyI2SCLK I2S clockRNG Random number generatorSDIO Secure digital input/output interfaceDoc ID 018603 Rev 25/21Getting started AN33626/21Doc ID 018603 Rev 22 Getting startedThis section describes the requirements and procedures needed to start using the clocktool.2.1 Software requirementsTo use the clock tool with Windows operating system, a recent version of Windows, such asWindows XP®, Vista® or Windows7® must be installed on the PC with at least 256 Mbytes of RAM.Before starting to use the clock tool, make sure that Microsoft Office® is installed on your machine and then follow these steps:●Download the latest version of the clock tool for the STM32F2xx product from .●Enable macros and ActiveX controls:Excel® 1997-2003 version1.Click Tools in the menu bar 2. Click Macro 3. Click Security4. Click Low (not recommended)Note:If ActiveX controls are not enabled, a warning message is displayed asking you to enable ActiveX. In this case, you should click “OK” to enable it.Excel 2007 version1.Click the Microsoft Office button and then click Excel options .2. Click Trust Center , click Trust center settings , and then click Macro settings .3. Click Enable all macros (not recommended, potentially dangerous code can run).4. Click Trust Center , click Trust center settings , and then click ActiveX settings .5. Click Enable all controls without restrictions and without prompting (notrecommended; potentiality dangerous controls can run).6. Click OK .Note:For more information about how to enable macros and ActiveX controls please refer to the Microsoft Office website.AN3362Getting startedDoc ID 018603 Rev 27/212.2 Hardware requirements2.2.1 IntroductionThe clock tool is designed to configure the system clocks and generate thesystem_stm32f2xx.c file for STM32F2xx microcontrollers.The system_stm32f2xx.c file is provided as a template system clock configuration file which can be easily modified to select the corresponding system clock frequency and to configure the Flash latency.2.2.2 Clock scheme for STM32F2xx microcontrollersThis section describes the system clock scheme that is dependent on the voltagerequirements (V DD ) versus the system clock frequency and Flash latency versus the system clock frequency.Three different clock sources can be used to drive the system clock (SYSCLK):1.HSI (16 MHz) oscillator clock2. HSE (4 MHz to 26 MHz) oscillator clock3.Main phase-locked loop (PLL) clock with a PLL voltage-controlled oscillator (PLLVCO) input frequency which must be between1 and 2 MHz (2 MHz is recommended to limit the PLL jitter) and with division factors M, N, P , and Q.All peripheral clocks are derived from the SYSCLK except for:1.The USB OTG FS clock (48 MHz), the RNG clock (48 MHz), and the SDIO clock (48 MHz) which come from a specific output of PLL (PLL48CLK).2.The I2S clock. To achieve high-quality audio performance, the I2S clock can be derived either from a specific PLL (PLLI2S) or from an external clock mapped on the I2S_CKIN pin.Getting started AN3362The number of Flash memory wait states (latency) is defined according to the frequency ofthe CPU (Cortex-M3) and indirectly by the supply voltage of the device (V DD) (see Table2:Number of wait states according to Cortex-M3 clock frequency).8/21Doc ID 018603 Rev 2AN3362Getting startedDoc ID 018603 Rev 29/212.2.3 I2S clock generatorThis section describes the I2S clock generator that is dependent on the master clock MCLK (enable or disable), the frame wide, and the I2S peripheral clock (I2SCLK).Figure 2.I2S clock generator architectureThe audio sampling frequency may be 192 kHz, 96 kHz, 48 kHz, 44.1 kHz, 32 kHz,22.05 kHz, 16 kHz, 11.025 kHz or 8 kHz. To reach the desired frequency, the linear divider (DIV) needs to be programmed according to the formulas below:When the master clock is generated (MCKOE in the SPI_I2SPR register is set):●Fs = I2SxCLK/[(16*2)*((2*I2SDIV)+ODD)*8)] when the channel frame is 16-bit wide ●Fs = I2SxCLK/[(32*2)*((2*I2SDIV)+ODD)*4)] when the channel frame is 32-bit wideWhere ODD is an odd factor for the prescaler.When the master clock is disabled (MCKOE bit cleared):●FS = I2SxCLK/[(16*2)*((2*I2SDIV)+ODD))] when the channel frame is 16-bit wide ●FS = I2SxCLK/[(32*2)*((2*I2SDIV)+ODD))] when the channel frame is 32-bit wideTable 2.Number of wait states according to Cortex-M3 clock frequencyWait states (WS)(LATENCY)HCLK - Cortex-M3 clock frequency (MHz)2.7 V ≤ V DD ≤3.6 V 2.4 V ≤ V DD <2.7 V 2.1V ≤ V DD <2.4V 1.8(1) V ≤ V DD <2.1 V 1.If IRROFF is set to V DD on STM32F20xx devices, this value can be lowered to 1.65 V when the deviceoperates in a reduced temperature range.0 WS (1 CPU cycle)0 < HCLK ≤ 300 < HCLK ≤ 240 < HCLK ≤ 180 < HCLK ≤ 161 WS (2 CPU cycle)30 < HCLK ≤ 6024 < HCLK ≤ 4818 < HCLK ≤ 3616 < HCLK ≤ 322 WS (3 CPU cycle)60 < HCLK ≤ 9048 < HCLK ≤ 7236 < HCLK ≤ 5432 < HCLK ≤ 483 WS(4 CPU cycle)90 < HCLK ≤ 12072 < HCLK ≤ 9654 < HCLK ≤ 7248 < HCLK ≤ 644 WS(5 CPU cycle)96 < HCLK ≤ 12072 < HCLK ≤ 9064 < HCLK ≤ 805 WS (6 CPU cycle)90 < HCLK ≤ 10880 < HCLK ≤ 966 WS (7 CPU cycle)108 < HCLK ≤ 12096 < HCLK ≤ 1127 WS (8 CPU cycle)112 < HCLK ≤ 1208-bit divider +linear CKODDI2SDIV[7:0]I2SxCLKCHLENI2SMOD reshaping stageDivider by 4Div210MCKOE MCKOEMCK1Getting started AN3362 This tool performs the best configuration of the PLLI2S_N and PLLI2S_R with the minimumerror on the sampling frequency and according to I2S parameters (frame wide, MCKO, andsampling frequency).Note:Only the PLLI2S_N and PLLI2S_R are configured in the “system_stm32f2xx.c” file.This tool does not configure the I2S register.The sampling frequency error is computed as an indicator according to the I2S parameterswhich are not configured in the output file “system_stm32f2xx.c”.10/21Doc ID 018603 Rev 2AN3362Tutorials Doc ID 018603 Rev 211/213 TutorialsThis section describes step by step how to use the clock tool to configure all system clocksand generate the system_stm32f2xx.c file.Two modes are available: Wizard and Expert . The selection is made in the Configurationmode list box.3.1 Wizard modeThis mode (default mode) guides you through a series of steps to obtain the desired clocksystem configuration quickly and easily.Figure 3.Wizard mode user interfaceNote:The ‘View ’ button permits viewing of the xls file in full screen to be activated or deactivated.The ‘Reset ’ button permits the system clock to the default configuration to be set.3.1.1 Step-by-step procedure1.If the HSE is used in your application, set its frequency between:–a minimum of 4 MHz and – a maximum of 26 MHz if a crystal oscillator is used for STM32F2xxNote: The definition, HSE_VALUE, in the stm32f2xx.h file must be modified eachtime the user changes the HSE oscillator value.If the frequency entered is out of range, an error message is displayed, as shown inFigure 4. A valid frequency must be entered.Tutorials AN336212/21Doc ID 018603 Rev 2Figure 4.HSE value out of range 2. Enterthe V DD power supply voltage range which is between 1.8 V and 3.6 V (refer to Figure 3: Wizard mode user interface ). If the V DD voltage is out of range, an errormessage is displayed as shown in Figure 5.Figure 5.V DD out of range3. Configure the Prefetch buffer , Instruction Cash and the Data cash (select ON orOFF from the list box).4.Specify if the PLLI2S is needed. If it is needed, enable it and follow step 9, 10, 11 and 12. Otherwise, go to step 5.5. Specify if a 48 MHz clock is needed for USB OTG FS, RNG or SDIO operations. If it isneeded, this adds a constraint to the parameter setting in PLL configuration. If it is notneeded, no USB constraint has been added.6.Set the desired HCLK frequency. The maximum frequency of HCLK must be less than120 MHz. If the entered value is higher than 120 MHz an error message is displayed asshown in Figure 6.AN3362Tutorials Figure 6.HCLK error message7. Select the PCLK1 and PCLK2prescaler settings from the list box to obtain the desiredPCLK1 and PCLK2 frequencies. The TIMCLK frequencies are configured automaticallydepending on the PCLK1 and PCLK2 prescaler settings.8. See step 79. Select the I2S clock source from the I2S source. Ignore steps 10, 11, and 12 if theexternal clock is selected as the clock source for the I2S peripheral.10. If the PLLI2S is selected as the I2S clock source, select the frame wide (16 or 32 bits).11. Specify if the master clock is enabled or disabled (Select ON/OFF from the list box).12. Select the Fs from the list box. The Fs value can be 192 kHz, 96 kHz, 48 kHz, 44.1 kHz,32 kHz, 22.05 kHz, 16 kHz, 11.025 kHz, and 8 kHz.13. Click on the RUN button.If more than one clock source is possible, a message box displays the clock sources thatcan be selected. (see Figure7). Choose HSE, HSI or PLL (which are sourced by the HSI orHSE).Figure 7.Select the clock sourceNote:When the USB and/or the I2S are enabled (checkbox selected in the clock tool) and the selected HCLK frequency is not possible, a message box is displays proposes the nearestHCLK clock frequency to use.Doc ID 018603 Rev 213/21Tutorials AN336214/21Doc ID 018603 Rev 2Figure 8.No possible configuration errorConfigure the Flash Latency: after running the application, the number of wait states is configured automatically with the best value (lowest possible value) which can be modifiedby another number higher then the selected one.Clicking the RUN button displays a progress bar.14. Finally, click on the Generate button to automatically generate the system_stm32f2xx.cfile.The system_stm32f2xx.c is generated in the same location as the clock tool. Display the fileto verify the value of the system clock, SystemCoreClock, and the values of HCLK, PCLK1,PCLK2, Flash access mode, and other parameters which are defined in the SetSysClockfunction.The system_stm32f2xx.c file must be added to the working project to be built .If the file is not generated, an error message is displayed as shown Figure 9.Figure 9.File generation error3.2 Expert modeThis mode provides more flexibility regarding the configuration setup but, it is up to the userto ensure that configuration is correct.AN3362Tutorials Figure 10.Expert mode user interfaceNote:The ‘View’ button permits viewing of the xls file in full screen to be activated or deactivated.The ‘Reset’ button permits the system clock to the default configuration to be set.1.Configure the SYSCLK frequency as follows:a) If the HSE is used in your application, set its frequency between:– a minimum of 4 MHzand– a maximum of 26 MHz if a crystal oscillator is used for STM32F2xx.Note: The definition, HSE_VALUE, in the stm32f2xx.h file must be modified eachtime the user changes the HSE oscillator value.If the frequency entered is out of range, an error message is displayed, as shown inFigure4. A valid frequency must be entered.b) Enter the V DD power supply voltage range which is between 1.8 V and 3.6 V (referto Figure10: Expert mode user interface).c) Configure the SYSCLK source (PLL, HSE or HSI). If the clock source selection isinvalid (the HCLK frequency is too high) an error message is displayed as shownin Figure11.Doc ID 018603 Rev 215/21Tutorials AN336216/21Doc ID 018603 Rev 2Figure 11.System clock frequency is exceeded d)If PLL is selected as the SYSCLK source, it is necessary to select the source clockfor the PLL (HSE or HSI).e) If the PLL is selected as the SYSCLK source, configure the main PLL(M) divisionfactor to acheive a PLLVCO frequency between 1 MHz and2 MHz. If the selected division factor is invalid, an error message is displayed asshown in Figure 12. If the I2S frequency is higher than 192 MHz an error messageis displayed as shown in Figure 13.Figure 12.The PLL input frequency is exceededAN3362Tutorials Figure 13.The I2S frequency is out of rangef) Set the HCLK prescaler using the AHBPrescaler list box to obtain the desiredHCLK frequency.g) Select the PCLK1prescaler settings from the list box to obtain the desired PCLK1frequency. The TIMCLK frequencies are configured automatically depending onthe PCLK1 prescaler settings.h) Select the PCLK2prescaler settings from the list box to obtain the desired PCLK2frequency. The TIMCLK frequencies are configured automatically depending onthe PCLK2 prescaler settings.i) Configure the Flash Latency: after setting the HCLK prescaler, the number ofFlash wait states is configured automatically with the best value (lowest possiblevalue) which can be modified by an other number higher then the selected one.j) Generate the clock configuration files by clicking on the Generate button.2. Configure the I2S clock frequency as follows:a) If the PLLI2S is needed, enable it and fellow the steps below.b) If the external clock source is selected as the I2S clock source, the following stepscan be ignored.c) If the PLLI2S is selected as the clock source for the I2S peripheral, configure thePLLI2S(N) multiplication factor.d) If the PLLI2S is selected as the clock source for the I2S peripheral, configure thePLLI2S(R) division factor.e) If the PLLI2S is selected as the I2S clock source, select the frame wide (16 or 32bits) and specify if the master clock is enabled or disabled.f) Select the Fs from the list box. The Fs value can be 192 kHz, 96 kHz, 48 kHz, 44.1kHz, 32 kHz, 22.05 kHz, 16 kHz, 11.025 kHz and 8 kHz.3. Configure the USB OTG FS, RNG or SDIO clockConfigure the PLL(Q) division factor for USB OTG FS, SDIO and the RNG.4. Optional configurationConfigure the Prefetch buffer, Instruction Cash and the Data cash.5. Generate the system_stm32f2xx.c fileClick on the Generate button to automatically generate the system_stm32f2xx.c file.The system_stm32f2xx.c file is generated in the same location as the clock tool. It canbe displayed to verify the value of the SYSCLK“SystemCoreClock” and the values ofHCLK, PCLK1, PCLK2, Flash access mode, and other parameters which are defined inthe “SetSysClock” function.The system_stm32f2xx.c file must be added to the working project to be built.Doc ID 018603 Rev 217/21Known limitations AN336218/21Doc ID 018603 Rev 24 Known limitationsThis sections describes the known limitations of the clock configuration tool.This tool does not support configurations that use the HSE external clock source (HSEbypass).AN3362Conclusion 5 ConclusionThis application note provides a description of how to use the clock tool with respect toSTM32F2xx microcontroller devices. Using either one of the two configuration modes, thistool generates a source code file system_stm32f2xx.c to configure the clock system of theSTM32F2xx.Two modes are available.Wizard mode is the first mode and provides a quick and easy way to configure the systemclocks.Expert mode is the second mode. I t offers more flexiblity in setting up the system clockconfiguration while still respecting all the product constraints.Doc ID 018603 Rev 219/21Revision history AN336220/21Doc ID 018603 Rev 26 Revision historyTable 3.Document revision history DateRevision Changes 18-Apr-20111Initial release04-Nov-20112New Figure 3: Wizard mode user interface on page 11New Figure 4: HSE value out of range on page 12Changed Note: on page 13Changed Figure 10: Expert mode user interface on page 15Changed Section 4: Known limitations on page 18AN3362Please Read Carefully:Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice.All ST products are sold pursuant to ST’s terms and conditions of sale.Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein.No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY TWO AUTHORIZED ST REPRESENTATIVES, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST.ST and the ST logo are trademarks or registered trademarks of ST in various countries.Information in this document supersedes and replaces all information previously supplied.The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.© 2011 STMicroelectronics - All rights reservedSTMicroelectronics group of companiesAustralia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of AmericaDoc ID 018603 Rev 221/21。

浅谈STM32F10X芯片RTC实时时钟

浅谈STM32F10X芯片RTC实时时钟

1、介绍系统复位后,对后备寄存器和RTC的访问被禁止,这是为了防止对后备区域(BKP)的意外写操作。

执行以下操作将使能对后备寄存器和RTC的访问:●设置寄存器RCC_APB1ENR的PWREN和BKPEN位,使能电源和后备接口时钟(调用:RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE));●设置寄存器PWR_CR的DBP位,使能对后备寄存器和RTC的访问(调用:PWR_BackupAccessCmd(ENABLE))。

2、RTC注意事项●RTC_PRL、RTC_ALR、RTC_CNT和RTC_DIV寄存器仅能通过备份域复位信号复位;系统复位或电源复位不会影响他们的值;●RTC提供APB1接口通ABP1读取RTC寄存器的值,但必须等待RTC_CRL寄存器中的RSF(同步标志位)位被硬件置“1”之后进行;●RTC的配置必需在前一次写操作结束(判断RTC_CR寄存器中的RTOFF是否为1,为1表示更新完成),并设置RTC_CRL寄存器中的CNF位,使RTC进入配置模式后,才能写入RTC_PRL、RTC_CNT、RTC_ALR寄存器,清除CNF标志位时,写操作才实际有效(说明RTC是动态配置的,即是在RTC 运行起来之后再进行配置);●RTC中的任何标志位都将保持挂起状态(因为OWF、ALRF、SECF和RSF只能由硬件置位由软件清零),直到适当的RTC_CR请求位被软件复位,表示所有请求的中断已经被接受;●若ALRF=1且ALRIE=1,则允许产生RTC全局中断,如果EXTI控制器中允许产生EXTI线17中断,则允许产生RTC全局中断和RTC闹钟中断,在这种情况下,一般设置闹铃中断优先级高于全局中断,如果全局中断优先级高于闹铃中断,则在全局中断中必须清除闹钟中断标志之后,才能进入闹钟中断处理函数进一步处理(因为不清除标志,则会一直引发中断,而全局中断优先级高,就会一直在全局中断中无法跳出来);●若ALRF=1,如果在EXTI控制器中设置了EXTI线17的中断模式,则允许产生RTC闹钟中断;如果在EXTI控制器中设置了EXTI线17的事件模式,则这条线上会产生一个脉冲(不会产生RTC闹钟中断);●当APB1时钟不运行时,OWF、ALRF、SECF和RSF位不被更新;●系统复位时禁止所有中断,无挂起中断请求,可以对RTC寄存器进行写操作;●对RTC的写操作必须使用如下过程之一与RTC秒标志同步:使用RTC闹钟中断,并在中断处理程序中修改RTC闹钟和/或RTC计数器;等待RTC控制寄存器中秒标志SECF置位,再更改RTC闹钟和/或RTC计数器。

STM32时钟配置方法详解

STM32时钟配置方法详解

STM32时钟配置方法详解STM32是意法半导体(STMicroelectronics)公司推出的一系列32位Flash微控制器,被广泛应用于各种嵌入式系统中。

时钟是STM32微控制器的核心部分,正确配置时钟可以确保系统正常工作并达到预期的性能。

本文将详细介绍STM32时钟配置的方法。

1.时钟源:STM32微控制器提供了多个时钟源,包括内部时钟(HSI、LSI)和外部时钟(HSE、LSE)。

其中,HSI(高速内部时钟)是一个高频率(通常为8MHz)的内部RC振荡器,适用于低功耗应用;LSI(低速内部时钟)是一个低频率(通常为40kHz)的内部RC振荡器,用于RTC(实时时钟)模块;HSE(高速外部时钟)是一个外接的高频晶振,用于提供更精确的时钟信号;LSE(低速外部时钟)是一个外接的低频晶振,适用于RTC模块。

2.主频和系统时钟:主频是指CPU的时钟频率,系统时钟是指STM32微控制器的总线时钟,包括AHB(高性能总线)、APB1(低速外设总线)和APB2(高速外设总线)。

在进行STM32时钟配置之前,需要按照以下几个步骤来完成。

1.启用对应的时钟源:根据具体需求,选择合适的时钟源并启用相应的时钟。

可以通过设置RCC_CR寄存器和RCC_APB1ENR/RCC_APB2ENR寄存器来实现。

例如,要使用HSE作为时钟源,需要首先启用HSE时钟。

2.配置时钟分频器:为了使系统时钟不超过芯片规格要求的最大频率,需要对时钟进行分频。

分频器有两个,即AHB分频器和APB分频器。

可以通过设置RCC_CFGR寄存器来实现。

例如,将AHB分频器设置为8,将APB1和APB2分频器分别设置为4,可以将主频分别分频为8MHz、32MHz和64MHz。

3.等待时钟稳定:当启用外部时钟源时,需要等待时钟稳定。

可以通过读取RCC_CR寄存器的特定标志位来判断时钟是否稳定。

4. 配置Flash存储器的延时:根据主频的不同,需要设置Flash存储器的访问延时,以确保正常读写数据。

STM32时钟配置方法详解

STM32时钟配置方法详解

STM32时钟配置方法详解时钟树是STM32微控制器中一系列时钟源和时钟分频器的组成部分。

时钟树包括系统时钟、外设时钟和内核时钟。

系统时钟用于驱动整个微控制器系统的核心,外设时钟用于驱动各种外设,内核时钟用于驱动CPU的运算。

在进行时钟配置之前,首先需要了解系统所需的时钟频率。

在STM32中,系统时钟可以通过多种方式进行配置,例如使用外部晶体、外部时钟、内部RC振荡器或者PLL(锁相环)等方式。

外部晶体是一种常用的时钟源,可以提供高精度的时钟频率。

在使用外部晶体时,首先需要设置PLL的时钟源为外部晶体,并设置PLL输入除频器的分频系数。

然后,再根据系统所需的时钟频率,设置PLL的倍频系数,以得到最终的系统时钟频率。

外部时钟是从外部提供的时钟信号,一般用于测试和调试。

使用外部时钟时,需要设置PLL的时钟源为外部时钟,并设置PLL的倍频系数,以得到所需的系统时钟频率。

内部RC振荡器是一种低成本的时钟源,但是其频率不如外部晶体稳定和精确。

在使用内部RC振荡器时,需要设置PLL的时钟源为内部RC振荡器,并设置PLL的倍频系数,以得到所需的系统时钟频率。

PLL是一种用于产生稳定高频时钟的电路,可以从一个低频时钟源产生一个高频时钟源。

使用PLL时,需要设置其输入时钟源和倍频系数。

系统时钟的分频系数可以通过RCC_CFGR寄存器进行设置。

RCC_CFGR寄存器的各个位域用于配置系统时钟的分频系数,包括分频因子、APB1的分频系数、APB2的分频系数等。

外设时钟是用于驱动外设的时钟,可以由系统时钟分频得到。

外设时钟的分频系数可以通过RCC_CFGR寄存器及各个外设的控制寄存器进行设置。

内核时钟是用于驱动CPU的运算的时钟。

在STM32微控制器中,CPU 时钟可以由系统时钟分频得到,分频系数可以通过RCC_CFGR寄存器和FLASH_ACR寄存器进行设置。

除了上述方法之外,STM32还可以使用时钟配置工具进行时钟配置。

你应该知道的STM32F04x单片机时钟切换教程

你应该知道的STM32F04x单片机时钟切换教程

你应该知道的STM32F04x单片机时钟切换教程本次编写的教程主要是针对STM32F04x系列的单片机,对于STM32单片机的时钟,想必使用过STM32的人都比较清楚,STM32它是基于 Cortex®-Mx系列内核的微处理器,芯片内部集成了丰富的外设。

同时也集成了内部的时钟源。

也就是说芯片不需要外接晶振就可以运行。

但是大多数的使用情况都是使用了芯片外部的晶振,包括官方的标准固件库也都是默认使用外部晶振,但是有些特殊情况下,不需要使用外部晶振,这样可以节省一个晶振和两个谐振电容,最主要的是可以节省PCB板子的面积。

但如果使用内部晶振,就得要编写程序,更改相关寄存器的值,切换至内部晶振才可以。

对于芯片时钟源切换这样的事情,对高手来说是很简单,对于新手来说就有些搞不清了,因此本教程叙述一下STM32F04x系列单片机时钟源切换的流程。

既然是切换单片机的时钟源,就不能不了解下芯片内部的时钟结构,几乎所有的STM32芯片,官方在手册中都会描述一下芯片内部的时钟结构。

STM32F04x也不列外,下面是STM32F04x系列单片机的时钟树:(摘自官方手册)图中序号说明:①:外部时钟源的输入端。

这个时钟源是系统时钟,可用的时钟源频率范围是(4~32)MHz。

②:外部实时时钟的时钟源输入端。

这个时钟源是计时使用,所以它的频率是固定在32.768KHz。

③:芯片内部时钟输出端。

可以控制相关的寄存器,选择内部的时钟信号从这个端口对外输出。

④:内部高速48MHz的时钟源。

该频率不需要倍频就可以直接给芯片的提供48MHz的系统时钟信号。

⑤:内部低速8MHz的时钟源。

该频率可以倍频或不倍频给系统提供时钟信号。

(但是STM32F04x芯片最高频率是48MHz)。

第8章 STM32F10x的主要模块设计1时钟

第8章 STM32F10x的主要模块设计1时钟

RCC寄存器组在库中的宏定义


文件中包含如下宏定义: #define PERIPH_BASE ((uint32_t)0x4000 0000) #define AHBPERIPH_BASE (PERIPH_BASE + 0x2 0000) #define RCC_BASE (AHBPERIPH_BASE + 0x1000) #define RCC ((RCC_TypeDef *) RCC_BASE) 先看外设基地址宏PERIPH_BASE,宏展开为uint32_t类型 0x4000 0000;宏RCC_BASE基地址展开为:0x4002 1000; (RCC_TypeDef *) 把RCC_BASE 地址转换为RCC_TypeDef结 构体指针类型。 RCC寄存器组中各寄存器地址是:基址+地址偏移量,如: RCC->CR 的空间地址为0x4002 1000; RCC->CFGR 的空间地址为0x4002 1004; … …
RCC寄存器组映像图
8.1. 2 RCC寄存器组在库中的宏定义

RCC寄存器组在stm32f10x.h中1081行的宏定义:
typedef struct { __IO uint32_t CR; __IO uint32_t CFGR; __IO uint32_t CIR; __IO uint32_t APB2RSTR; __IO uint32_t APB1RSTR; __IO uint32_t AHBENR; __IO uint32_t APB2ENR; __IO uint32_t APB1ENR; __IO uint32_t BDCR; __IO uint32_t CSR; #ifdef STM32F10X_CL __IO uint32_t AHBRSTR; __IO uint32_t CFGR2; #endif /* STM32F10X_CL */ … … } RCC_TypeDef;

STM32外部时钟更改后程序设置方法

STM32外部时钟更改后程序设置方法

FLASH_SetLatency(FLASH_Latency_2);//设置 FLASH 存储器延时时钟周期数 FLASH_Latency_2 2 延时周期 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//选择 FLASH 预取指缓存的模,预取指缓存使能
RCC_DeInit();//将外设 RCC 寄存器重设为缺省 RCC_HSICmd(ENABLE);//设置外部高速晶振(HSE) HSE 晶振打开(ON) while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY_HCLKConfig(RCC_SYSCLK_Div1);//设置 AHB 时钟(HCLK) RCC_SYSCLK_Div1 ----- AHB 时钟 = 系统时钟 RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速 AHB 时钟(PCLK2) RCC_HCLK_Div1 ----- APB2 时钟 = HCLK RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速 AHB 时钟(PCLK1) RCC_HCLK_Div2 ----- APB1 时钟 = HCLK/2
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速 AHB 时钟(PCLK2) RCC_HCLK_Div1 ----- APB2 时钟 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速 AHB 时钟(PCLK1) RCC_HCLK_Div2 ----- APB1 时钟 = HCLK/2
#endif /* HSE_VALUE */
注意:以上是以外部晶振为 16MHz 为例
方法(2): 使用 STM32F10x 内部时钟来配置 STM32F10x 各个模块的系统时钟。(不推荐使用,内部 8MHz RC 振荡器不稳定)
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

5
结论 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6
版本历史 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3
教程 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1 向导模式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
㟇$+%ᘏ㒓ǃ‫ݙ‬Ḍǃ ᄬ‫఼ټ‬੠'0$ 㟇FRUWH[㋏㒳ᅮᯊ఼ )+&/.&RUWH[㞾⬅䖤㸠ᯊ䩳
3&/.
㟇$3%໪䆒
4/16
文档 ID 022837 第 1 版
AN4055
1
术语表
表 1.
术语定义 术语
HCLK PCLK1 PCLK2 TIMCLK FCPU Ext.Clock VDD HSI HSE MCLK I2S Fs I2SCLK
AHB 时钟 APB1 时钟 APB2 时钟 定时器时钟 Cortex-M0 时钟 外部时钟 电源 高速内部时钟 高速外部时钟 主时钟 串行数字音频总线 采样频率 I2S 时钟
3.2 专家模式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4
已知限制 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.2 STM32F0xx 微控制器的时钟方案 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 I2S 时钟发生器 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1. 单击“Microsoft Office”按钮,然后单击“Excel 选项”。 2. 依次单击“信任中心”、“信任中心设置”、“宏设置”。 3. 单击“启用所有宏(不推荐;可能会运行有潜在危险的代码)”。 4. 依次单击“信任中心”、“信任中心设置”、“ActiveX 设置”。 5. 单击“无限制启用所有控件并且不进行提示(不推荐;可能会运行有潜在危险的控件)”。 6. 单击“确定”。
有关如何启用宏和 ActiveX 控件的更多信息,请参见 Microsoft Office 网站。
2.2
2.2.1
2
硬件要求
简介
时钟工具可以为 STM32F0xx 微控制器配置系统时钟并生成 system_stm32f0xx.c 文件。 system_stm32f0xx.c 文件是系统时钟配置文件模板,可以修改该文件以选择相应的系统时钟 频率和配置 Flash 等待周期。
2/16
文档 ID 022837 第 1 版
AN4055
表格索引
表格索引
表 1. 表 2.
术语定义 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 文档版本历史 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
所有外设始终均源自 SYSCLK。 Flash 等待周期(延迟)数根据 CPU (Cortex-M0) 的频率设置: - 如果 0 < SYSCLK <= 24 MHz,则等待周期设置为“0” - 如果 SYSCLK > 24 MHz,则等待周期设置为“1”
时钟结构图
+6, 6<6&/.
)/,7)&/. 㟇)ODVK㓪⿟᥹ষ
STM32F0xx 微控制器的时钟方案
本节介绍的系统时钟方案取决于电压工作范围 (VDD) 与系统时钟频率的关系以及 Flash 等待 周期与系统时钟频率的关系。
6/16
文档 ID 022837 第 1 版
AN4055
使用入门
注: 图 1.
可以使用三种不同的时钟源来驱动系统时钟 (SYSCLK): 1. HSI (8 MHz) 振荡器时钟。 2. HSE(4 MHz 到 32 MHz)振荡器时钟。 3. 主锁相环 (PLL) 时钟,具有 PLL 电压控制的振荡器 (PLLVCO) 输入频率。
2012 年 05 月
文档 ID 022837 第 1 版
1/16

目录
目录
AN4055
1
术语表 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Excel® 1997-2003 版
1. 单击菜单栏中的“工具”。 2. 单击“宏”。 3. 单击“安全性”。 4. 单击“低”(不建议使用)。
如果未启用 ActiveX 控件,将显示一条警告消息,询问是否启用 ActiveX。此时,应单击 “确定”以启用 ActiveX 控件。
注:
Excel 2007 版
说明
术语表
文档 ID 022837 第 1 版
5/16
15
使用入门
2
使用入门
AN4055
本节介绍使用时钟工具时的必要条件以及使用步骤。
2.1
软件要求
注:
要在 Windows® 操作系统中使用时钟工具,必须在至少有 256 MB RAM 的 PC 中安装最新 版本的 Windows,如 Windows XP、Vista 或 Windows 7。 在开始使用时钟工具前,确保已在计算机上安装了 Microsoft® Office,然后按照以下步骤操作: ● 从 下载最新版的适用于 STM32F0xx 产品的时钟工具。 ● 启用宏和 ActiveX® 控件:
AN4055 应用笔记
适用于 STM32F0xx 微控制器的时钟配置工具
简介
本应用笔记介绍了适用于 STM32F0xx 微控制器系列的时钟配置工具。 此工具是为了帮助用户配置微控制器时钟,并将电源和 Flash 访问模式等条件考虑在内。 此配置工具在“STM32F0xx_Clock_Configuration_VX.Y.Z.xls”文件中实现,该文件随 STM32F0xx 标准外设库提供,并可以从 下载。 此工具支持 STM32F0xx 的下列功能: ● 配置系统时钟、HCLK 源和输出频率 ● 配置 Flash 等待周期(等待周期数取决于 HCLK 频率) ● 设置 PCLK1、PCLK2、TIMCLK(定时器时钟)和 I2SCLK 频率 ● 生成含有上述全部设置的 system_stm32f0xx.c 文件(STM32F0xx CMSIS Cortex-M0 器
㟇,& 㟇 ,6
0+] +6, +6,5&

3//65& 3//08/
6:
+6,
3// [[
3//&/.
$+$%+% 乘ߚ䜡఼
[
+6(


&66
6<6&/.
/6(
㟇&(&
+&/.

$3% 乘ߚ䜡఼
2.2 硬件要求 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 简介 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
文档 ID 022837 第 1 版
3/16
3
图片索引
图片索引
AN4055
图 1. 图 2. 图 3. 图 4. 图 5. 图 6. 图 7.
时钟结构图 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 I2S 时钟结构图 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 向导模式用户界面 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 选择时钟源 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 文件生成错误 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 专家模式用户界面 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 超出系统时钟频率 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
相关文档
最新文档