STM8S自学笔记-004 时钟与延时
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
修改库函数【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 */