stm8s clock source switching

Publisher:芳华逝水Latest update time:2016-09-22 Source: eefocusKeywords:stm8s Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The STM8 microcontroller has a rich clock source. The chip has both a 16MHZ high-speed RC oscillator and a 128KHZ low-speed RC oscillator. It can also connect a high-speed crystal oscillator externally. During system operation, it can be switched freely as needed. After the microcontroller is reset, the internal high-speed RC oscillator is used first, and the frequency division factor is 8, so the clock frequency of the CPU power-on operation is 2MHZ.

Switching the clock source mainly involves the following registers: main clock switching register CLK_SWR and switching control register CLK_SWCR.

The reset value of the master clock switch register is 0xe1, which means switching to the internal high-speed RC oscillator. When 0xb4 is written to the register, it means switching to the external high-speed crystal oscillator.

In the actual switching process, the SWEN (bit 1) in the switching control register should be set to 1 first, then the value of CLK_SWCR should be set, and finally, it should be determined whether the SWIF flag in the switching control register is switched successfully.

The following experimental program first switches the main clock source to an external crystal oscillator with an oscillation frequency of 8MHZ, and then quickly flashes the LED indicator. Next, the main clock source is switched to the internal oscillator with an oscillation frequency of 2MHZ, and then the LED indicator flashes slowly. By observing the flashing frequency of the LED indicator, it can be seen that the same loop code changes the flashing frequency and duration due to the change of the main clock source.

Still using ST's development tools, generate a C language program framework, and then modify main.c. The modified code is as follows.

 

// Program description: Change the CPU running speed by switching the CPU's main clock source

 

#include "STM8S207C_S.h"

 

// Function: Delay function

// Input parameter: ms -- the number of milliseconds to delay, assuming the CPU frequency is 2MHZ

// Output parameters: None

// Return value: None

// Note: None

void DelayMS(unsigned int ms)

{

  unsigned char i;

  while(ms != 0)

  {

    for(i=0;i<250;i++)

    {

    }

    for(i=0;i<75;i++)

    {

    }

    ms--; 

  }    

}

 

main()

{

  int i;

 

  // Set PD3 to push-pull output to drive the LED

  PD_DDR = 0x08;

  PD_CR1 = 0x08;             

  PD_CR2 = 0x00;

 

  // Start the external high speed crystal oscillator

  CLK_ECKR = 0x01; // Allow external high speed oscillator to work

  while((CLK_ECKR & 0x02) == 0x00); // Wait for the external high-speed oscillator to be ready

              

  // Note that after reset, the CPU clock source comes from the internal RC oscillator

 

  for(;;) // Enter an infinite loop                             

  {

 

    // Next, switch the CPU clock source to an external high-speed crystal oscillator. The frequency on the development board is 8MHZ.

    // Through the LED, we can see that the speed of program running has indeed been significantly improved

    CLK_SWCR = CLK_SWCR | 0x02; // SWEN <- 1

    CLK_SWR = 0xB4; // Select the high-speed oscillator outside the chip as the main clock

    while((CLK_SWCR & 0x08) == 0); // Wait for the switch to succeed

         

    CLK_SWCR = CLK_SWCR & 0xFD; // Clear the switch flag

 

    for(i=0;i<10;i++) //LED flashes 10 times at high speed

    {

      PD_ODR = 0x08;

      DelayMS(100);

      PD_ODR = 0x00;

      DelayMS(100);

    }

  

    // Next, switch the CPU clock source to the internal RC oscillator. Since the reset value of CLK_CKDIVR is 0x18

    // Therefore, the 16MHZ RC oscillator must be divided by 8 before it can be used as the main clock, so the frequency is 2MHZ

    // Through the LED, it can be seen that the speed of program running has indeed dropped significantly

    CLK_SWCR = CLK_SWCR | 0x02; // SWEN <- 1

    CLK_SWR = 0xE1; // Select HSI as the main clock source

    while((CLK_SWCR & 0x08) == 0); // Wait for the switch to succeed

 

    CLK_SWCR = CLK_SWCR & 0xFD; // Clear the switch flag

 

    for(i=0;i<10;i++) //LED flashes 10 times at low speed

    {

      PD_ODR = 0x08;

      DelayMS(100);

      PD_ODR = 0x00;

      DelayMS(100);

    }

  }

}

Keywords:stm8s Reference address:stm8s clock source switching

Previous article:STM8 UART
Next article:Use of STC12C5A16S2 dual serial ports

Recommended ReadingLatest update time:2024-11-16 13:41

stm8s development (IV) Use of CLOCK: clock control
STM8S has a powerful clock system, which provides a high-speed 16M RC oscillator and a low-speed 128K RC oscillator. The clock controller is powerful, flexible and easy to use. Its purpose is to enable users to obtain the best performance while ensuring the lowest power consumption. Users can manage each clock sourc
[Microcontroller]
stm8s development (IV) Use of CLOCK: clock control
How to see the RAM ROM size when compiling STM8S using stvd
I just installed the STVD compiler, but it doesn't show how much RAM and ROM are used when compiling? There are two ways to solve this problem: one is to look at the .map file and the other is to add a patch. The specific steps are as follows. You can download the corresponding file in My Resources. http://download.
[Microcontroller]
[MCU Notes] STM8S series MCU FLASH operation
    1. Modify the comments of stm8s_conf.h to allow #include "stm8s_flash.h" to compile     2. Modify the injection of stm8s.h to enable the Flash function to run from RAM #if !defined (RAM_EXECUTION)     #define RAM_EXECUTION (1) // When writing to a block, you must enable injection here #endif /* RAM_EXECUTION *
[Microcontroller]
STM8S GPIO self-study notes
STM8S105C6T6 48 pins 32KB Flash Package: LQFP temp: -40~85 PA1~PA6,PB0~PB7,PC1~PC7,PD0~PD7,PE0~PE3,PE5~PE7,PG0,PG1, There are a total of 38 general-purpose I/O ports. GPIO Registers Port x Output Data Register (Px_ODR); Port x input data register (Px_IDR); Port x output data direction (Px_DDR); 0: input mode 1: outp
[Microcontroller]
stm8s_STVD generates interrupt_vector.c problem
When using STVD software to develop stm8sMCU, open the STVD software and the system automatically generates an interrupt vector file vector.c. The following code can be seen in it Refer to the official document PM0044 program manual of stm8s mcu, we can know that mcu has 32 4-byte interrupt vector entries, where the
[Microcontroller]
stm8s_STVD generates interrupt_vector.c problem
STM8S TIM2 output PWM initialization function
Refer to the online examples to summarize the initialization function #define TIM2_CLK 16000000  void  timer2_Init(char channal, ulong hz,int pwm) {     TIM2_ARRH = (TIM2_CLK/hz)/256;     TIM2_ARRL = (TIM2_CLK/hz)%256;     switch (channal)     {     case 1:{          TIM2_CCMR1 |= 0x70;          TIM2_CCER1 |= 0x0
[Microcontroller]
STM8S EEPROM operation
STM8S has built-in EEPROM, which is a good resource for products that require large memory. The following is what I have personally figured out and verified to be OK. You can use it with confidence if you need it.  #define EEPROMADDR0X000 ((u32)(FLASH_DATA_START_PHYSICAL_ADDRESS))  #define EEPROMADDR0X001 ((u32)(FLA
[Microcontroller]
Data storage method based on EEPROM inside STM8S microprocessor
EEPROM (Electrically Erasable Programmable Read-Only Memory) refers to an electronically erasable read-only memory, which is a non-volatile memory. After the power is off, the stored data is still retained. To erase or rewrite the content, you only need to operate directly in the form of electronic signals. EEPROM i
[Microcontroller]
Data storage method based on EEPROM inside STM8S microprocessor
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号