First of all, this is my first time writing a blog. I hope to use this platform to record my growth experience in learning.
I have just come into contact with stm8 and am just getting started. If there are any mistakes or things that need to be corrected, I hope you can point them out. Thank you very much!
This time I will mainly talk about the configuration of stm8s103 output pwm wave. First, the picture of the successfully output pwm wave and the relevant register values is shown.
First is the clock configuration:
CLK_ICKR &=0X01; //Turn on the internal high-speed clock
CLK_CKDIVR = 0x00; //Set the clock to the internal 16M high-speed clock
Consult the manual for relevant information (the manual is the most important tool and reference):
Clock start operation:
Set 16M high speed clock:
Then we look at the CLK_CKDIVR register:
So as long as all settings are set to zero, it means 1 division, and you can get a 16M high-speed clock;
Next, configure TIM2:
CLK_PCKENR1 = 0x20; //tim2 peripheral clock enable
TIM2_CR1 = 0X00; //TIM2 counter disabled
The purpose of writing the first register here is to enable the tim2 peripheral clock (actually it is not necessary, it is enabled by default). The explanation of this register is as follows:
Writing the second register disables the counter from counting at the beginning. The register is as follows:
TIM2_CCMR3 |= 0X70; //Set timer 2 3-channel PWM mode 2 output mode
TIM2_CCER2 |= 0x03; //Low level is valid, OC1 signal is output to the corresponding output pin
Here is mainly the configuration of the timer mode and enable
CCMR3 is the mode register.
This illustrates the pwm mode:
CCER2 is the enable register;
Next is the setting of pwm core, frequency and duty cycle:
/* Set the Autoreload value */
TIM2_ARRH = 0x00;
TIM2_ARRL = 0x8a;
/*TIM2 Frequency=16M/138=106k*/
TIM2_CCR3H = 0x00; //Set PWM output duty cycle to 40%
TIM2_CCR3L = 0X37;
Here is how to calculate it:
From the clock configuration, we know that the clock is 16M, and the value of automatic reload is 0x008a=138;
So f=16M/138=106k;
Duty cycle calculation: 0x0037=55; Duty cycle=55/138=40%;
The final bit of configuration:
TIM2_PSCR = 0x00; //No frequency division
TIM2_IER |= 0X00; //Disable interrupt enable
TIM2_CCMR3 |= 0X08; // Enable preloading
TIM2_CR1 = 0X01; //TIM2 counter enable
There is still a frequency divider inside TIM2 (TIM3), so it needs to be set
This is the basic explanation. Since it was my first time, I encountered many problems during the process. I also searched for various information on the Internet. Finally, I completed it. Finally, I hope that we can learn and improve together!
Previous article:RFID Programming Based on STM8
Next article:STM8S key PWM dimming lamp history
Recommended ReadingLatest update time:2024-11-16 17:49
- Popular Resources
- Popular amplifiers
- STM8 C language programming (1) - basic program and startup code analysis
- Description of the BLDC back-EMF sampling method based on the STM8 official library
- STM32 MCU project example: Smart watch design based on TouchGFX (8) Associating the underlying driver with the UI
- MC9S12XS MCU Principle and Embedded System Development
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- To become a high-level electronic engineer, in addition to programming and PCB drawing, please recommend other circuit design books. Thank you
- Circuit design for charging and discharging dual NiMH batteries
- FPGA and DSP communication issues
- Disassembling a multi-turn potentiometer
- Last day: Apply for free evaluation, Bear Pi folding development board
- Those who are familiar with solar photovoltaic laminators, please come in!
- How to change the SinA332.0 development board from VGA output to LCD output
- Why is large-screen splicing becoming more and more market-oriented?
- MSP430 MCU matrix keyboard driver LED
- 【Smart Sports Watch】2. Build Development Environment 1