STM32从零开始(五)详解RCC库函数

本文详细介绍了STM32的RCC库函数使用,包括重置时钟配置、配置外部高速振荡器HSE、等待HSE启动、启用或禁用内部高速振荡器HSI、配置PLL时钟源和乘法因子、启用或禁用PLL以及各种时钟的配置。同时,文章讲解了位带操作、中断处理和RTC时钟的相关知识。
摘要由CSDN通过智能技术生成

目录

1.将RCC时钟配置重置为默认重置

其实不用单独调用,因为系统会自动运行systemInit这个函数,将时钟在启动的时候置为72mhz。具体每句话其实就是把rcc寄存器不同的位设置一下,设置的结果看手册第六章最后一节,rcc寄存器那里,一个一个对应过去即可


/**
  * @brief  Resets the RCC clock configuration to the default reset state.
  * @param  None
  * @retval None
  */
void RCC_DeInit(void)
{
   
  /* Set HSION bit */
  RCC->CR |= (uint32_t)0x00000001;

  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
  RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
  RCC->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 */

}

——————————————————————————

2.***配置外部高速振荡器(HSE)。

用来控制HSE的打开和关闭。HSE就是这
在这里插入图片描述我们要知道为什么叫HSE,就是high speed external,高速,外部的意思。就很好记。


/**
  * @brief  Configures the External High Speed oscillator (HSE).
  * @note   HSE can not be stopped if it is used directly or through the PLL as system clock.
  * @param  RCC_HSE: specifies the new state of the HSE.
  *   This parameter can be one of the following values:
  *     @arg RCC_HSE_OFF: HSE oscillator OFF
  *     @arg RCC_HSE_ON: HSE oscillator ON
  *     @arg RCC_HSE_Bypass: HSE oscillator bypassed with external clock
  * @retval None
  */
void RCC_HSEConfig(uint32_t 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;
 	/*
  	#define CR_HSEON_Reset            ((uint32_t)0xFFFEFFFF)
	#define CR_HSEON_Set              ((uint32_t)0x00010000)
	置位用或,复位用与
	*/
  /* Reset HSEBYP bit */
  RCC->CR &= CR_HSEBYP_Reset;
  /* 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;
      //外部就连了个晶振,不能自己震荡,得有hse电路配合
    case RCC_HSE_Bypass:
      /* Set HSEBYP and HSEON bits */
      RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;
      break;
      //这个是外部连接了时钟,直接连进来就直接用的时钟信号,不是晶振
    default:
      break;
  }
}

————————————————————————————————

3.等待HSE启动

HSE打开得等一段时间
注意返回值类型是ErrorStatus类型的,也就是0或者1。enum类型的枚举默认是int型的。
typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
在这里插入图片描述读取的是rcc_cr寄存器的这一位

/**
  * @brief  Waits for HSE start-up.
  * @param  None
  * @retval An ErrorStatus enumuration value:
  * - SUCCESS: HSE oscillator is stable and ready to use
  * - ERROR: HSE oscillator not yet ready
  */
ErrorStatus RCC_WaitForHSEStartUp(void)
{
   
  __IO uint32_t StartUpCounter = 0;
  ErrorStatus status = ERROR;
  FlagStatus HSEStatus = RESET;
  
  /* Wait till HSE is ready and if Time out is reached exit */
  do
  {
   
    HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
    StartUpCounter++;  
  } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));
  //如果等待时间过长或者标志位为1的时候就跳出循环继续往下
  if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
  {
   
    status = SUCCESS;
  }
  else
  {
   
    status = ERROR;
  }  
  return (status);
}

————————————————————————————

4.调整内部高速振荡器(HSI)

在这里插入图片描述比较鸡肋,往里写就行了。实际中配合传感器使用<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值