STM8S自学笔记-004 时钟:HSI、HSE与LSI
2021-09-13 来源:eefocus
STM8S的时钟源
单片机世界的多种时钟源
单片机的时钟源有很多种,根据其来源可将它们大致分为两类:内部时钟源 和 外部时钟源。而后,根据频率的不同,又可作如下划分:
内部时钟源
高速的内置时钟源 HSI
中速的内置时钟源 MSI(想到了某PC主板品牌?)
低速的内置时钟源 LSI
锁相环倍频器 PLL(和电脑CPU里的倍频系数很像)
外部时钟源
高速的外置时钟源 HSE
低速的外置时钟源 LSE
STM8S的时钟源
HSE:1~16MHz高速的外部晶体振荡器
HSE:最高16MHz的外部时钟信号
HSI:16MHz内部高速RC振荡器
LSI:128KHz的低速内部RC振荡器
上电复位后的STM8S
STM8S上电后,MCU将自动运行在HSI / 8 = 2(MHz)的时钟下。而后,你就可以随心所欲地切换时钟源,以及随心所欲地设置运行速度。
那么问题也就来了:
时钟源切换
频率设置
时钟设置代码
我自己的项目里,从没用过STM8S的LSI,所以,在代码内容中,没有LSI的切换,但我觉得可以仿照HSI的切换来编写。
新建【Drv_CLK.c】
/**
******************************************************************************
* @file Drv_CLK.c
* @author Elsa
* @version V1.0.0
* @date 5-August-2021
* @brief
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include 'Drv_CLK.h'
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Switch system clock to the Internal High Speed oscillator (HSI).
* @param HSIPrescaler : Specifies the HSI clock divider to apply.
* This parameter can be any of the @ref CLK_Prescaler_TypeDef enumeration.
* @param ClockPrescaler : Specifies the HSI or CPU clock divider to apply.
* @retval None
*/
void CLOCK_HSI(CLK_Prescaler_TypeDef HSI_PRESCALER, CLK_Prescaler_TypeDef CPU_PRESCALER)
{
/* Enables the Internal High Speed oscillator (HSI). */
CLK_HSICmd(ENABLE);
/* Configures the Switch from one clock to HSI */
while (CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE) != SUCCESS);
/* Checks whether the CLK_FLAG_HSIRDY flag is set. */
while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET);
/* Starts manually the clock switch execution. */
CLK_ClockSwitchCmd(ENABLE);
/* Configures the HSI clock dividers. */
CLK_HSIPrescalerConfig(HSI_PRESCALER);
/* Configures the HSI and CPU clock dividers. */
CLK_SYSCLKConfig(CPU_PRESCALER);
}
/**
* @brief Switch system clock to the the External High Speed oscillator (HSE).
* @param ClockPrescaler : Specifies the HSI or CPU clock divider to apply.
* @retval None
*/
void CLOCK_HSE(CLK_Prescaler_TypeDef CPU_PRESCALER)
{
/* Enable the External High Speed oscillator (HSE). */
CLK_HSECmd(ENABLE);
/* Configures the Switch from one clock to HSE */
while (CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE) != SUCCESS);
/* Checks whether the CLK_FLAG_HSERDY flag is set. */
while (CLK_GetFlagStatus(CLK_FLAG_HSERDY) == RESET);
/* Starts manually the clock switch execution. */
CLK_ClockSwitchCmd(ENABLE);
/* Enables the Clock Security System. */
CLK_ClockSecuritySystemEnable();
/* Configures the HSI and CPU clock dividers. */
CLK_SYSCLKConfig(CPU_PRESCALER);
}
新建【Drv_CLK.h】
/**
******************************************************************************
* @file Drv_CLK.h
* @author Elsa
* @version V1.0.0
* @date 5-August-2021
* @brief This file contains the headers of the Drv_CLK.c
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __DRV_CLK_H
#define __DRV_CLK_H
/* Includes ------------------------------------------------------------------*/
#include 'main.h'//This is my version of include.h
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void CLOCK_HSI(CLK_Prescaler_TypeDef HSI_PRESCALER, CLK_Prescaler_TypeDef CPU_PRESCALER);
void CLOCK_HSE(CLK_Prescaler_TypeDef CPU_PRESCALER);
#endif
如果用HSE作为时钟源,记得修改库函数【stm8s.h】中HSE_VALUE的值。
下列代码大约从该头文件的第99行开始:
/**
* @brief In the following line adjust the value of External High Speed oscillator (HSE)
used in your application
Tip: To avoid modifying this file each time you need to use different HSE, you
can define the HSE value in your toolchain compiler preprocessor.
*/
#if !defined HSE_Value
#if defined (STM8S208) || defined (STM8S207) || defined (STM8S007) || defined (STM8AF52Ax) ||
defined (STM8AF62Ax) || defined (STM8AF622x)
#define HSE_VALUE ((uint32_t)24000000) /* Value of the External oscillator in Hz*/
#else
#define HSE_VALUE ((uint32_t)12000000) /* Value of the External oscillator in Hz*/ //See here
//#define HSE_VALUE ((uint32_t)16000000) /* Value of the External oscillator in Hz*/ //See here
#endif /* STM8S208 || STM8S207 || STM8S007 || STM8AF62Ax || STM8AF52Ax || STM8AF622x */
#endif /* HSE_Value */
- STM8S 使用IAR在线调试配置
- IAR FOR STM8S 错误 An error occurred while retrieving GDI features: gdi-error [40201]解决方法
- STM8S定时器操作
- IAR调试STM8S遇到函数返回值错误的问题
- STM8S 定时器第一次使用时间不准确的问题
- 解决stm8s配置timer2中断时间不准
- STM8S被设定读保护的芯片如何解除读保护,重新下载程序
- STM8S单片机红外接收解码程序 带1602显示
- stm8 stm8s stm8af 485接口 modbus协议代码
- STM8S学习笔记之三(STM8 SysClk)