STM8 internal clock switching

Publisher:幸福家庭Latest update time:2021-02-28 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: There is a clock switching function inside the STM8, which can change the clock frequency when needed. The board is STM8_Discovery, and the LED is PD0.

The procedure is as follows:


//Function: STM8 internal clock frequency division switch


#include "iostm8s105c6.h"


// Function: Delay function


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


void DelayMS(unsigned int ms)


{


unsigned char i;


while(ms != 0)


{


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


{


}


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


{


}


ms--;


}


}


//GPIO port initialization


void GPIO_init(void)


{


PD_DDR = 0x01; //Configure the direction register PD0 output of the PD port


PD_CR1 = 0x01; //Set PD0 to push-pull output


PD_CR2 = 0x00;


}


// Clock initialization


void CLK_init(void)


{


CLK_SWR = 0xE1; //Select the 16MHZ RC oscillator inside the chip as the main clock


}


main()


{


int i;


GPIO_init();


CLK_init();


while(1) //Enter an infinite loop


{


//Set the CPU clock divider below so that CPU clock = main clock / 4


//Through the LED, we can see that the speed of the program has indeed dropped significantly


CLK_CKDIVR = 0x1A; // Main clock = 16MHZ/8 = 2MHz


//CPU clock = main clock / 4 = 500KHZ


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


{


PD_ODR = 0x01;


DelayMS(100);


PD_ODR = 0x00;


DelayMS(100);


}


// Set the CPU clock divider below so that the CPU clock = the main clock


CLK_CKDIVR = 0x0A; // Main clock = 16MHZ/2 = 8MHz


//CPU clock = main clock/4 = 2MHZ


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


{


PD_ODR = 0x01;


DelayMS(100);


PD_ODR = 0x00;


DelayMS(100);


}


}


}


Keywords:STM8 Reference address:STM8 internal clock switching

Previous article:Clock system of STM8 microcontroller
Next article:STM8 realizes perpetual calendar (highlight time adjustment)

Recommended ReadingLatest update time:2024-11-16 19:39

TIM timer operation based on STM8---STM8-Chapter 3
1. Overview   STM8S provides three types of TIM timers: advanced control type (TIM1), general type (TIM2/TIM3/TIM5) and basic timer (TIM4/TIM6). Although they have different functions, they are all based on a common architecture. This common architecture makes it very easy and convenient to design applications using
[Microcontroller]
TIM timer operation based on STM8---STM8-Chapter 3
STM8 MCKIT1.0 BLDC sensorless control acquisition implementation analysis
The STM8 motor library written by ST can drive BLDC in a sensorless way. The STM8 chip has only one AD converter, and it is an 8-bit machine. In addition to collecting and comparing electric potential, it is also necessary to collect, calculate and process analog signals such as bus voltage, bus current, heat sink tem
[Microcontroller]
STM8 timer TIM1 timing
The TIM1 timer of ST's STM8 microcontroller is a 16-bit advanced control timer that can be used to achieve basic timing and PWM wave generation. Here we mainly analyze the basic timing function. Let's first look at the initialization function of the TIM1 timer in the library function provided by ST void TIM1_TimeB
[Microcontroller]
About iar for stm8: iar Fatal Error
Due to work needs, I installed iar for stm8. The company's god sent a program. However, after installation and completion, the compilation failed. The reason for failure is as the title. I looked up some posts on the Internet and found some key inspirations, but for those who have just come into contact with stm8 soft
[Microcontroller]
About iar for stm8: iar Fatal Error
Solution to STM8 MCU PWM no waveform output
  When debugging the PWM function of the STM8 microcontroller, you often encounter a situation where after the PWM initialization settings are completed, the program is downloaded to the microcontroller, but there is no waveform output. Sometimes the code is downloaded from other debuggers, but there is no waveform ou
[Microcontroller]
Solution to STM8 MCU PWM no waveform output
STM8 transplants library functions into register methods
When using the STM32 microcontroller, I like to use library functions. Since there are too many registers of STM32, it is too troublesome to set up if the registers are used directly. In addition, the STM32 microcontroller is fast and has a large capacity, so using registers will not improve efficiency much. It is n
[Microcontroller]
STM8 transplants library functions into register methods
STM8 Study Notes--ADC
The ADC converter of STM8 has a resolution of 10 bits. The special thing about STM8 is that if the user can accept a lower resolution, the speed of reading the conversion result will be faster; the user can read only 8 bits out of 10 bits by selecting the corresponding register alignment. Configure the register ADC_CR2
[Microcontroller]
【STM8】Serial port data is always stuck in read interrupt
▪ Phenomenon STM8 UART1 has enabled the read interrupt, and the initialization code is as follows: // Configure the serial port parameters as follows: baud rate 115200, 8 data bits, 1 stop bit, no parity check, disable synchronous transmission, allow receiving and sending UART1_Init((u32)115200, UART1_WORDLENGTH_8D,
[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号