STM8S自学笔记-004 时钟配置:HSI、HSE与LSI

STM8S自学笔记-004 时钟:HSI、HSE与LSI


【修订记录】
1、20210805,第一版
2、20210806,添加目录,追加 修改HSE_Value文字段落
3、20230206,表述简化

STM8S的时钟源

芯片的主时钟有4种时钟源可选:

  1. HSE:1~16MHz高速的外部晶体
  2. HSE:最高16MHz的外部时钟信号
  3. HSI:16MHz内部高速RC振荡器
  4. LSI:128KHz的低速内部RC振荡器

上电复位后

上电复位(POR)后,MCU自动运行于HSI下,分频系数是8。习惯上,我们在进入main函数后,优先配置时钟系统,以满足项目需求;除非HSI分频得到的2MHz已足够。

那么,问题也就来了:

  1. 时钟源切换
  2. 分频设置

时钟设置代码

1、HSE→SYSCLK分频→SYSCLK:当HSE为系统时钟源时,分频系数只有1个,即SYSCLK分频系数;
2、HSI→HSI分频→SYSCLK分频→SYSCLK:当HSI为系统时钟源时,还多出1个分频系数可设定,也就是HSI分频系数
3、关于LSI:切换到LSI的函数读者可自己仿写,软件流程和切换到HSI的方式相同。LSI的有点是功耗最低。

/**
  ******************************************************************************
  * @file    clk_hal.c 
  * @author  
  * @version V1.0.0
  * @date    DD-MM-YYYY
  * @brief   
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "clk_hal.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Variables -----------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Functions -----------------------------------------------------------------*/
/**
  * @brief  Switch system clock to HSE
  * @param  CLK_Prescaler : CPU clock divider to apply
  * @retval None
  */
void CLK_HAL_SwitchToHSE(CLK_Prescaler_TypeDef CLK_Prescaler)
{
    CLK_HSECmd(ENABLE);                                 /* Enable HSE */
                                                        /* Configures the Switch from one clock to another */
    while (CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,                       /* automatic clock switching mode */
                                 CLK_SOURCE_HSE,                            /* Clock Source HSE */
                                 DISABLE,                                   /* Disable IT */
                                 CLK_CURRENTCLOCKSTATE_DISABLE) != SUCCESS);/* Disable current clock */
    while (CLK_GetFlagStatus(CLK_FLAG_HSERDY) != SET);  /* Wait for the-new-clock's stability */
    CLK_ClockSwitchCmd(ENABLE);                         /* Starts manually the clock switch execution. */
    CLK_ClockSecuritySystemEnable();                    /* Enables the Clock Security System. */
    CLK_SYSCLKConfig(CLK_Prescaler);                    /* Write CPU clock divider. */
}

/**
  * @brief  Switch system clock to HSI
  * @param  HSIPrescaler : HSI clock divider to apply.
  * @param  CLK_Prescaler : CPU clock divider to apply.
  * @retval None
  */
void CLK_HAL_SwitchToHSI(CLK_Prescaler_TypeDef HSIPrescaler, CLK_Prescaler_TypeDef CLK_Prescaler)
{
    CLK_HSICmd(ENABLE);                                 /* Enable HSI */
                                                        /* Configures the Switch from one clock to another */
    while (CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,                       /* automatic clock switching mode */
                                 CLK_SOURCE_HSI,                            /* Clock Source HSI */
                                 DISABLE,                                   /* Disable IT */
                                 CLK_CURRENTCLOCKSTATE_DISABLE) != SUCCESS);/* Disable current clock */
    
    while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) != SET);  /* Wait for the-new-clock's stability */
    CLK_ClockSwitchCmd(ENABLE);                         /* Starts manually the clock switch execution. */
    CLK_HSIPrescalerConfig(HSIPrescaler);               /* Write HSI clock divider. */
    CLK_SYSCLKConfig(CLK_Prescaler);                    /* Write CPU clock divider. */
}

/******************************************************************************/
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值