[STM8] - External clock + internal clock switching

Publisher:光子梦境Latest update time:2018-06-02 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM8S103K3: External clock + internal clock switching.

  1. First, take a look at the clock structure diagram of STM8S103K3, which can help you understand it well. 
    Write the picture description here 
    There are several clocks here, which are the clocks marked in the figure, and you need to clarify them:

  • fHSE: External high-speed crystal oscillator clock, which is generated by an external crystal oscillator and its size is determined by the size of the external crystal oscillator. The external crystal oscillator range of STM8S is: 1-24M, see "HSE OSC 1-24M" in the figure.

  • fHSI: internal RC high-speed clock, which is generated by the internal RC oscillator circuit, and its value is 16M. However, it can be divided by the frequency divider behind it, and four frequency division coefficients are available (1, 2, 4, 8). Note: The accuracy is slightly worse than that of the external crystal oscillator.

  • fMASTER: Master clock, which is provided by HSE or HSI. Its main function is to provide clock for peripheral devices (such as I2C, SPI, ADC, etc.) and provide clock source for CPU.

  • fCPU: CPU clock, which is obtained by dividing fMASTER. Its function is to provide clock to the CPU. One mechanical cycle is one fCPU clock cycle.

  • The following is the source code of the clock for your reference. 
    There are four programs written here, namely:

    • Using high-speed internal clock (register version)

    • Using high-speed internal clock (library function version)

    • Using an external clock (register version)

    • Using an external clock (library version)

    The source code is as follows, you should be able to read the comments.

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

    * Function Name  : InitCpuClock.

    * Description    : Initial CPU clock, .

    * Input          : None.

    * Output         : None.

    * Return         : None.

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

    static void InitCpuClock(void)

    {


    #if 1

        // Register version - uses high speed internal clock 


        //Use HSI @8MHZ, div = 2; 8=16/2

        CLK->ECKR &= ~CLK_ECKR_HSEEN; // Disable external clock


        CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_HSIDIV); // Clear internal clock prescaler

        CLK->CKDIVR |= CLK_PRESCALER_HSIDIV2; // Set the internal clock prescaler to 2; For details, please refer to the datasheet (reference manual)


        CLK->ICKR |= CLK_ICKR_HSIEN; // Enable internal high-speed clock


        while(!(CLK->ICKR&CLK_ICKR_HSIRDY)); // Wait for the internal high-speed clock to stabilize. After stabilization, the internal clock has started running. Clock = 16/2 M

    #endif


    #if 0

        // Library version - use internal high speed clock


        //Use HSI @8MHZ, div = 2; 8=16/2

        CLK_HSECmd(DISABLE); // Disable external clock


        CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2); // Set the internal clock prescaler to 2; For details, please refer to the data manual (reference manual)


        CLK_HSICmd(ENABLE); // Enable internal high-speed clock


        while(!(CLK->ICKR&CLK_ICKR_HSIRDY)); // Wait for the internal high-speed clock to stabilize. After stabilization, the internal clock has started running. Clock = 16/2 M

    #endif


    #if 0

    // Register version - using external clock


        CLK->CKDIVR |= CLK_PRESCALER_CPUDIV1; // CPU clock divided by 1, CPU clock = external clock (that is, external crystal frequency)


        CLK->ECKR |= CLK_ECKR_HSEEN; // Allow external high speed oscillator to work

        while(!(CLK->ECKR & CLK_ECKR_HSERDY)); // Wait for the external high-speed oscillator to be ready


        CLK->SWCR |= CLK_SWCR_SWEN; // Enable switching

        CLK->SWR = CLK_SOURCE_HSE; // Select the high-speed oscillator outside the chip as the main clock

        while(!(CLK->SWCR&CLK_SWCR_SWIF)); // Wait for the switch to succeed

        CLK->SWCR &= ~(CLK_SWCR_SWEN|CLK_SWCR_SWIF); // Clear the switch flag

    #endif



    #if 0

    // Library version - using external clock


        CLK->CKDIVR |= CLK_PRESCALER_CPUDIV1; // CPU clock divided by 1, CPU clock = external clock (that is, external crystal frequency)

        CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, // Clock automatic switching mode, note: the parameter is automatic switching, otherwise only using this function will not switch successfully

                              CLK_SOURCE_HSE, // Clock to switch (here is the external clock)

                              DISABLE, // Whether to enable the switching completion interrupt (disabled here)

                              CLK_CURRENTCLOCKSTATE_ENABLE); // Whether to enable the current clock (here choose to turn off the current clock HSI)

        CLK->SWCR &= ~(CLK_SWCR_SWEN|CLK_SWCR_SWIF); // Clear the switch flag


    #endif


    }


    Keywords:STM8 Reference address:[STM8] - External clock + internal clock switching

    Previous article:stm8 clock source selection configuration
    Next article:STM8 clock source switching

    Recommended ReadingLatest update time:2024-11-15 17:27

    STM8S development environment builds IAR For STM8
    /* ********************************************************************************************************************************** * Name: STM8S development environment build IAR For STM8 * Author : MingMing * Release : 2013/12/29 * Update: 2013/12/29 * E-mail : clint.wang@foxmail.com ******************************
    [Microcontroller]
    STM8S development environment builds IAR For STM8
    STM8 and assembly language (5)
    In the last experimental program, we completed the driving of the LED indicator light, using the GPIO output method. This time, we will use the GPIO input method to input the key. The following code reads the key value. If the key is pressed, the LED is turned on, otherwise the LED is turned off. Use ST's developmen
    [Microcontroller]
    LED intelligent lighting control system based on IOT
    Aiming at the wired and mechanical control mode of traditional lighting, this paper proposes an intelligent lighting control system based on the Internet of Things (IOT) technology. It embeds the intelligent control module into the LED lighting terminal and comprehensively uses embedded technology, wireless sensor net
    [Microcontroller]
    LED intelligent lighting control system based on IOT
    Talk about the basic idea of ​​low-power MCU programming with STM8
    1 Introduction After I started working, I came across the term "low power consumption" and started searching for various low power MCUs. The Internet is a very free place, and you can always see various debates about which MCU has lower power consumption. In these debates, I looked up the characteristics of almost all
    [Microcontroller]
    Talk about the basic idea of ​​low-power MCU programming with STM8
    stm8 eeprom
    void EEPROOM_Configuration(void) {  FLASH_DeInit();  /* Define flash programming Time*/  FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);  /* Unlock Data memory */  FLASH_Unlock(FLASH_MEMTYPE_DATA);  } Write one bit of data FLASH_ProgramByte(0x4000,0xaa); //Write 0xaa data to address 0x4000 Read one bit of data r
    [Microcontroller]
    Design of Bluetooth Serial Communication System Implemented by STM8 MCU
    I recently found a single-chip microcomputer, STM8, while browsing Taobao. Compared with the 8-bit AVR I have been using before, STM8 feels more powerful. The chip features are as follows: Core: Harvard architecture with 3-stage pipeline, extended instruction set Program memory: 8K bytes Flash; RAM: 1K bytes Data memo
    [Microcontroller]
    Design of Bluetooth Serial Communication System Implemented by STM8 MCU
    STM8 Analog/Digital Converter (ADC) conversion modes
    STM8 analog/digital converter (ADC) conversion mode The ADC supports five conversion modes: single mode, continuous mode, continuous mode with buffer, single scan mode, and continuous scan mode. Single mode In the ADC single conversion mode of STM8, the ADC completes only one conversion on the channel selected by CH
    [Microcontroller]
    STM8 beCAN working mode
    STM8 beCAN working mode beCAN has 3 main working modes: initialization mode, normal mode and sleep mode. After hardware reset, beCAN works in sleep mode to reduce power consumption. Software can request beCAN to enter initialization or sleep mode by setting INRQ or SLEEP in the CAN_MCR register to '1'. Once entering i
    [Microcontroller]
    STM8 beCAN working mode
    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号