STM8S clock configuration source code program

Publisher:WhisperingSongLatest update time:2018-06-02 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Model: stm8s903k3

Compiler: STVD4.3.5+cosmic 8k 5.4.1

Others: Use operation registers, do not use libraries

-----------------

The clock is the soul of the microcontroller, and everything is related to the clock. Compared with the inhuman fuse bit setting of AVR for clock/oscillation source, stm only needs to operate the register. The most important thing is that even if the setting is wrong, it will not lock up!

----------------

Schematic diagram of stm internal clock.



As can be seen from the figure, the system clock can be provided by the external HSE (high speed external) or the internal high speed clock HSI (high speed internal) or the internal low speed clock LSI (low speed internal). Here we take the internal high speed clock as an example. The internal high speed clock HSI is divided by HSIDIV to get the Fmaster clock. Please note that, as the name implies, this clock is the main clock. All other clocks in the chip are divided by this clock (distributed once), and it also controls the gates of these peripheral clocks (open or close).

Fmaster is the trunk, the internal/external clock is the root, and the CPU or peripheral clock is the branch. There are many roots and branches, but there is only one trunk.

 

Therefore, the idea of ​​configuring the clock is.

First, determine the value of Fmaster. The default is division by 8, which we can change through the HSIDIV bit.

Second, determine the CPU frequency by setting CPUDIV.

The third option is to enable the peripheral clocks. If there are power requirements, turning off unused clocks can reduce power consumption. All are enabled by default.

Fourth, other settings, clock switching, SWIM, clock output, etc. The following source code is not set by default.


Below is the source code

/************************************************************************/  

/* Function: Clock configuration initialization 

 * Parameters: None 

 * Return: None 

 * Modified: 2014-11-14 

 * Reason: Global clock initialization, main frequency, gate bit */   

/************************************************************************/  

void clk_init(void)  

{  

  

    /*For the internal high-speed clock HSI[16MHz], its logical relationship is HSI---[HSIDIV division]---[clock selection gate]--- 

    * Master clock Fmaster---{[CPUDIV division]---Fcpu clock; other peripheral clocks} 

    */  

    CLK_ICKR = 0x01; //Internal clock control register. Reset value, enable HSI clock, fast wake-up disable  

    CLK_ECKR = 0x00;    //External clock control register。HSE禁止。  

    CLK_SWR = 0xE1; //Clock master switch register. Select HSI as the master clock.  

    CLK_SWCR = 0x00; //Clock switch control register. Clock switching, the Chinese data sheet translation seems to be wrong  

      

        /*Clock divider register. Divides the frequency of Fmaster and Fcpu. 

    * The default is 8 and 1, now it is 2 and 1, so Fmaster=8M, Fcpu=8M */  

    CLK_CKDIVR = 0x08;  

    CLK_PCKENR1 = 0xFF; //Peripheral clock gating register 1. Fmaster is connected to the peripheral clock  

    CLK_PCKENR2 = 0x0; //same as above  

    CLK_CSSR = 0x0; //Clock security system register. Clock security detection  

    CLK_CCOR = 0x0; //Configurable clock control register. Clock output is turned off  

}  

-------------------

Update, stm has an LSI calibration function, please refer to the chip manual for details. And this calibration function, the library function has been written. But there are two points to note when using it.

First, the LSI calibration function of the library function uses AWU and TIM3, so in the above configuration, CLK_PCKENR2 should be configured to turn on the AWU peripheral, otherwise it will be an infinite loop.

Second, if TIM3 needs to perform other functions, it must be configured after LSI calibration, that is, the sequence is main clock initialization ---- LSI calibration ---- TIM3 configuration, otherwise TIM3 will not be started

3. The above LSI calibration procedure comes from the latest official firmware library. In fact, I personally think that if the frequency requirement is not strict, you don’t need to configure it, such as the buzzer…


Keywords:STM8S Reference address:STM8S clock configuration source code program

Previous article:STM8L HSE clock configuration
Next article:stm8 lights up the first LED

Recommended ReadingLatest update time:2024-11-16 11:38

STM8S reads and writes internal EEPROM
1 Initialization function void InitEEPROM(void)   {          FLASH_DeInit();     FLASH_Unlock(FLASH_MEMTYPE_DATA);//EEPROM     FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_TPROG);        }   2. Implementation of the function to read a byte /****************************************************************
[Microcontroller]
Description and procedures of STM8S, MPU6050 driver
This MPU6050 is driven by IIC, but the specific circuit diagram does not use the hardware IIC interface of STM8S, but uses the analog IIC. It should be pointed out that when configuring the STM8S IO, SDA, for the IO connected to SDA, the IO on the STM8S processor does not have a real open-drain output function. Th
[Microcontroller]
STM8S_009_EXTI external interrupt
Ⅰ. Write in front The stability of STM8S is not as good as that of STM32, especially when the power consumption is low. When using EXTI external interrupt to wake up, it is easy to be woken up by interference signals. Therefore, in this case, more anti-interference circuits need to be considered in the hardware circ
[Microcontroller]
STVD Debugging Problems in STM8S
The environment is the main control chip: STM8SF103F3, the compiler is COSMIC8, and the IDE environment is STVD. The code is as follows: u8 ret;  ret = rf_get_addr_len(); ret = rf_get_trint_level(); ret = rf_get_drssi_level(); ret = rf_get_dest_provider(); ret = rf_get_addr_len();  When using STLINK for debu
[Microcontroller]
STM8S (105K4) usage notes - use TIM1 to output PWM to make a breathing light
STM8S105K4 related The known output channels that can be used as TIM1 PWM are PC1, PC2, PC3, and PC4. The optional TIM1 clock is known as fmaster. If the STM8S chip used is not 105K4, please refer to the corresponding documentation of the chip, confirm the output channel of TIM1 PWM, and set the corresponding option
[Microcontroller]
STM8S (105K4) usage notes - use TIM1 to output PWM to make a breathing light
STM8S main clock switching (using HSE clock source)
The clock switching method used is manual switching. The clock switching is performed in the interrupt, and then the switched main clock is output to the CLK_CCO port. Clock switching flow chart (manual switching flow chart): Clock Tree: Register version: unsigned char i=255;  PD_DDR_DDR0=1; //Configure PD0 to o
[Microcontroller]
STM8S main clock switching (using HSE clock source)
IAR FOR STM8S error An error occurred while retrieving GDI features: gdi-error [40201] solution
This morning, I used IAR to debug and compile a project, and found that IAR actually had the following error message An error occurred while retrieving GDI features: gdi-error : Can't access configuration database I checked online and found that some people also had this problem. Generally speaking, the problem ca
[Microcontroller]
STM8S_006_AWU automatic wake-up
Ⅰ. Write in front In some low-power devices, the device needs to enter low power consumption and need to wake up the MCU at a certain interval. Therefore, there is a need for a timed automatic wake-up function called "AWU automatic wake-up".   To implement the above functions in STM32, the common operation is to use R
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号