STM32F10x - RCC - 图文

更新时间:2023-11-10 22:03:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

16.1 RTC寄存器结构....................................................................214 16.2 RTC库函数........................................................................215 No 函数名 描述 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

RCC_DeInit RCC_HSEConfig RCC_WaitForHSEStartUp RCC_AdjustHSICalibrationValue RCC_HSICmd RCC_PLLConfig RCC_PLLCmd RCC_SYSCLKConfig RCC_GetSYSCLKSource RCC_HCLKConfig RCC_PCLK1Config RCC_PCLK2Config RCC_ITConfig RCC_USBCLKConfig RCC_ADCCLKConfig RCC_LSEConfig RCC_LSICmd RCC_RTCCLKConfig RCC_RTCCLKCmd RCC_GetClocksFreq RCC_AHBPeriphClockCmd RCC_APB2PeriphClockCmd RCC_APB1PeriphClockCmd RCC_APB2PeriphResetCmd RCC_APB1PeriphResetCmd RCC_BackupResetCmd RCC_ClockSecuritySystemCmd RCC_MCOConfig RCC_GetFlagStatus RCC_ClearFlag RCC_GetITStatus RCC_ClearITPendingBit 将外设RCC寄存器重设为缺省值 设置外部高速晶振(HSE) 等待HSE起振 调整内部高速晶振(HSI)校准值 使能或者失能内部高速晶振(HSI) 设置PLL时钟源及倍频系数 使能或者失能PLL 设置系统时钟(SYSCLK) 返回用作系统时钟的时钟源 设置AHB时钟(HCLK) 设置低速AHB时钟(PCLK1) 设置高速AHB时钟(PCLK2) 使能或失能指定的RCC中断 设置USB时钟(USBCLK) 设置ADC时钟(ADCCLK) 设置外部低速晶振(LSE) 使能或失能内部低速晶振(LSI) 设置RTC时钟(RTCCLK) 使能或者失能RTC时钟 返回不同片上时钟的频率 使能或失能AHB外设时钟 使能或失能APB2外设时钟 使能或失能APB1外设时钟 强制或释放高速APB(APB2)外设复位 强制或释放低速APB(APB1)外设复位 强制或释放后备域复位 使能或失能时钟安全系统 选择在MCO管脚上输出的时钟源 检查 指定的RCC标志位 设置与否 清除 RCC的复位标志位 检查 指定的RCC中断 发生与否 清除 RCC的中断待处理位 15 复位和时钟设置(RCC)

RCC有多种用途,包括时钟设置,外设复位和时钟管理。

Section 15.1 RCC寄存器结构描述了固件函数库所使用的数据结构, Section 15.2 固件库函数介绍了函数库里的所有函数。

15.1 RCC寄存器结构

RCC 寄存器结构,RCC_TypeDeff,在文件“stm2f10x_map.h中定义如下:

typedef struct {

vu32 CR; vu32 CFGR; vu32 CIR;

vu32 APB2RSTR; vu32 APB1RSTR;

vu32 AHBENR; vu32 APB2ENR; vu32 APB1ENR; vu32 BDCR; vu32 CSR; }RCC_TypeDef;

Table 336. RCC寄存器 寄存器 描述 CR 时钟控制寄存器 CFGR 时钟配置寄存器 CIR 时钟中断寄存器 APB2RSTR APB2外设复位寄存器 APB1RSTR APB1外设复位寄存器 AHBENR AHB外设时钟使能寄存器 APB2ENR APB2外设时钟使能寄存器 APB1ENR APB1外设时钟使能寄存器 BDCR 备份域控制寄存器 CSR 控制/状态寄存器 RCC 外设声明于文件“sm32f10x_map.h:

#define PERIPH_BASE ((u32)0x40000000) #define APB1PERIPH_BASE PERIPH_BASE

#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000) #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) #define RCC_BASE (AHBPERIPH_BASE + 0x1000) #ifndef DEBUG ...

#ifdef _RCC

#define RCC ((RCC_TypeDef *) RCC_BASE) #endif /*_RCC */ ...

#else /* DEBUG */ ...

#ifdef _RCC

EXT RCC_TypeDef *RCC; #endif /*_RCC */ ... #endif

使用Debug模式时,初始化指针RCC于文件:

#ifdef _RCC

RCC = (RCC_TypeDef *) RCC_BASE; #endif /*_RCC */

为了访问RCC寄存器,, _RCC必须在文件“stm2f10x_conf.h中定义如下:

#define _RCC

15.2 RCC库函数

Table 337. RCC库函数 【函数间首页】

15.2.1 函数RCC_DeInit

Table 338.函数RCC_DeInit(1)(2) 函数名 RCC_DeInit 函数原形 void RCC_DeInit(void) 功能描述 将外设RCC寄存器重设为缺省值 输入参数 无 输出参数 无 返回值 无 先决条件 无 被调用函数 无 1. 该函数不改动寄存器RCC_CR的HSITRIM[4:0]位。 2. 该函数不重置CC_BDCR和RCC_CSR。 例:

/* Deinitialize the RCC registers */ RCC_DeInit(); 函数原型如下:

void RCC_DeInit(void)//对CR、CFGR、CIR进行复位 {

/* Set HSION bit */

RCC->CR |= (u32)0x00000001;// RCC_CR.HSION(Bit0)=1,开启HSI

/* Reset SW[1:0](Bit1-0), HPRE[3:0](Bit7-4), PPRE1[2:0](Bit10-8), PPRE2[2:0] (Bit13-11), ADCPRE[1:0](Bit15-14) MCO[2:0](Bit26-24) bits */【USBPRE、PLL相关设置 未复位】 RCC->CFGR &= (u32)0xF8FF0000;

/* Reset HSEON(Bit16), CSSON(Bit19) and PLLON(Bit24) bits */ RCC->CR &= (u32)0xFEF6FFFF;

/* Reset HSEBYP(Bit18) bit */

RCC->CR &= (u32)0xFFFBFFFF; //RCC_CR.HSEON=0时,才能写该位。

/* Reset PLLSRC(Bit16), PLLXTPRE(Bit17), PLLMUL[3:0] (Bit21-18) and USBPRE(Bit22) bits */ RCC->CFGR &= (u32)0xFF80FFFF;// 【USBPRE、PLL相关设置 复位:PLLMUL[3:0]在RCC_CR.PLLON=0时才可写入】

/* Disable all interrupts */

//对应LSI、LSE、HSI、HSE、PLL、CSS的Ready Interrupt Flag、Interrupt Enable、Ready Interrupt Clear RCC->CIR = 0x00000000; }

15.2.2 函数RCC_HSEConfig

Table 339.函数RCC_HSEConfig 函数名 RCC_HSEConfig 函数原形 void RCC_HSEConfig(u32 RCC_HSE) 功能描述 设置外部高速晶振(HSE) RCC_HSE: HSE的新状态 输入参数 参阅Section:RCC_HSE查阅更多该参数允许取值范围 输出参数 无 返回值 无 先决条件 如果HSE被直接或者通过PLL用于系统时钟,那么它不能被停振被调 用函数 无 RCC_HSE :该参数设置了HSE的状态(见Table 340. )。 Table 340. RCC_HSE定义 RCC_HSE 描述 #defined值 对应CR位置 (u32)0x00000000 RCC_HSE_OFF HSE晶振OFF 为何不是0xFFFEFFFF? Bit16 RCC_HSE_ON HSE晶振ON (u32)0x00010000 RCC_HSE_Bypass HSE晶振被外部时钟旁路 (u32)0x00040000 Bit18 例: /* Enable the HSE */

RCC_HSEConfig(RCC_HSE_ON); 函数原型如下:

void RCC_HSEConfig(u32 RCC_HSE) {

/* Check the parameters */

assert_param(IS_RCC_HSE(RCC_HSE));

/* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/ /* Reset HSEON bit */

RCC->CR &= CR_HSEON_Reset; /* 定义HSE 定义值 CR中位置 备注 #define CR_HSEBYP_Reset ((u32)0xFFFBFFFF) Bit18 Bit16=0时,才能写入 #define CR_HSEBYP_Set ((u32)0x00040000) #define CR_HSEON_Reset ((u32)0xFFFEFFFF) Bit16 #define CR_HSEON_Set ((u32)0x00010000) #define CR_HSITRIM_Mask ((u32)0xFFFFFF07) Bit7-3 */ /* Reset HSEBYP bit */

RCC->CR &= CR_HSEBYP_Reset;//CR_HSEON先写0,才能写该位。//关闭旁路

/* Configure HSE (RCC_HSE_OFF is already covered by the code section above) */

switch(RCC_HSE) {

case RCC_HSE_ON: /* Set HSEON bit */ RCC->CR |= CR_HSEON_Set; break;

case RCC_HSE_Bypass: /* Set HSEBYP and HSEON bits */

RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;//HSEBYP必须在HSEON=0的前提下写入 break;

default: break; } }

15.2.3 函数RCC_WaitForHSEStartUp

Table 341.函数RCC_WaitForHSEStartUp 函数名 RCC_WaitForHSEStartUp 函数原形 ErrorStatus RCC_WaitForHSEStartUp(void) 等待HSE起振 功能描述 该函数将等待直到HSE就绪,或者在超时的情况下退出 输入参数 无 输出参数 无 一个ErrorStatus枚举值: 返回值 SUCCESS:HSE晶振稳定且就绪;ERROR:HSE晶振未就绪 先决条件 无 被调用函数 例: ErrorStatus HSEStartUpStatus; /* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready and if Time out is reached exit */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) {

/* Add here PLL ans system clock config */ } else {

/* Add here some code to deal with this error */ }

函数原型如下:

ErrorStatus RCC_WaitForHSEStartUp(void)

//typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; {

ErrorStatus status = ERROR;

/* Wait till HSE is ready and if Time out is reached exit */ do {

HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);

//函数FlagStatus RCC_GetFlagStatus(u8 RCC_FLAG);具体见下面被调用函数

StartUpCounter++; //static vu32 StartUpCounter = 0;

} while((HSEStatus == RESET) && (StartUpCounter != HSEStartUp_TimeOut));

// #define HSEStartUp_TimeOut ((u16)0x01FF)//最多等待512次

/*如果HSEStatus一直为RESET(且还没判断512次),那么继续循环,等待HSEStatus为SET后退出。如果512次后还是RESET,表明HSE没有激活。

if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET) { status = SUCCESS; } else

{ status = ERROR; }

return (status); }

被调用的RCC_GetFlagStatus(u8 RCC_FLAG)函数原型如下:

FlagStatus RCC_GetFlagStatus(u8 RCC_FLAG)

无(内部配置有调用函数:RCC_GetFlagStatus(u8 RCC_FLAG)) {

u32 tmp = 0;

u32 statusreg = 0;

FlagStatus bitstatus = RESET;

/* Check the parameters */

assert_param(IS_RCC_FLAG(RCC_FLAG)); /* No RCC_FLAG种类 1 #define RCC_FLAG_HSIRDY 2 #define RCC_FLAG_HSERDY 3 #define RCC_FLAG_PLLRDY 4 #define RCC_FLAG_LSERDY 5 #define RCC_FLAG_LSIRDY 6 #define RCC_FLAG_PINRST 7 #define RCC_FLAG_PORRST 8 #define RCC_FLAG_SFTRST 9 #define RCC_FLAG_IWDGRST 10 #define RCC_FLAG_WWDGRST 11 #define RCC_FLAG_LPWRRST 意义 -- 定义值 ((u8)0x21) ((u8)0x31) ((u8)0x39) ((u8)0x41) ((u8)0x61) ((u8)0x7A) ((u8)0x7B) ((u8)0x7C) ((u8)0x7D) ((u8)0x7E) ((u8)0x7F) -- 前3bit 0b001 0b001 0b001 0b010 0b011 0b011 0b011 0b011 0b011 0b011 0b011 在哪个寄存器中 后5Bit 0 0001 1 0001 1 1001 0 0001 0 0001 1 1010 1 1011 1 1100 1 1101 1 1110 1 1111 在寄存器中的第几位 在哪个寄存器 第几位 Bit0 RCC_CR Bit17 Bit25 RCC_BDCR Bit1 Bit1 Bit26 Bit27 RCC_CSR Bit28 Bit29 Bit30 Bit31 -- -- */ /* Get the RCC register index */

tmp = RCC_FLAG >> 5;//判断在哪个寄存器

if (tmp == 1) /* The flag to check is in CR register */ { statusreg = RCC->CR; }

else if (tmp == 2) /* The flag to check is in BDCR register */ { statusreg = RCC->BDCR; }

else //(tmp==3) /* The flag to check is in CSR register */ { statusreg = RCC->CSR; }

/* Get the flag position */

tmp = RCC_FLAG & FLAG_Mask;// #define FLAG_Mask ((u8)0x1F)//判断在寄存器中的第几位

if ((statusreg & ((u32)1 << tmp)) != (u32)RESET) { bitstatus = SET; } else

{ bitstatus = RESET; }

/* Return the flag status */ return bitstatus; }

15.2.4 函数RCC_AdjustHSICalibrationValue

Table 342. 函数RCC_AdjustHSICalibrationValue 函数名 RCC_AdjustHSICalibrationValue void RCC_AdjustHSICalibrationValue(u8 函数原形 HSICalibrationValue) 功能描述 调整内部高速晶振(HSI)校准值 输入参数 HSICalibrationValue:校准补偿值(值在0-0x1F之间) 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例: /* Set HSI calibration value to c0x1F (maximum) */ RCC_AdjustHSICalibrationValue(0x1F); 函数原型如下:

void RCC_AdjustHSICalibrationValue(u8 HSICalibrationValue) {

u32 tmpreg = 0;

/* Check the parameters */

assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));

tmpreg = RCC->CR;

/* Clear HSITRIM[4:0] bits */

tmpreg &= CR_HSITRIM_Mask;// #define CR_HSITRIM_Mask ((u32)0xFFFFFF07) //bit7-3

本文来源:https://www.bwwdw.com/article/0v0v.html

Top