ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

STM8S自学笔记-004 时钟

2021-08-05 23:33:10  阅读:262  来源: 互联网

标签:STM8S CLK HSI CPU 004 HSE clock 时钟


STM8S自学笔记-004 时钟与延时

STM8S的时钟源

单片机世界的多种时钟源

单片机的时钟源有很多种,根据其来源可将它们大致分为两类:内部时钟源 和 外部时钟源。而后,根据频率的不同,又可作如下划分:

内部时钟源

  • 高速的内置时钟源 HSI
  • 中速的内置时钟源 MSI(想到了某PC主板品牌?)
  • 低速的内置时钟源 LSI
  • 锁相环倍频器 PLL(和电脑CPU里的倍频系数很像)

外部时钟源

  • 高速的外置时钟源 HSE
  • 低速的外置时钟源 LSE

STM8S的时钟源

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

上电复位后的STM8S

STM8S上电后,MCU将自动运行在HSI / 8 = 2(MHz)的时钟下。而后,你就可以随心所欲地切换时钟源,以及随心所欲地设置运行速度。

那么问题也就来了:

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

时钟设置代码

我自己的项目里,从没用过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 */

标签:STM8S,CLK,HSI,CPU,004,HSE,clock,时钟
来源: https://blog.csdn.net/weixin_43572492/article/details/115015197

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有