stm8 TIM2 PWM configuration

Publisher:飘然出尘Latest update time:2020-01-29 Source: eefocusKeywords:stm8  TIM2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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. 

Oscilloscope
Write the picture description here

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: 

Write the picture description here 
Write the picture description here 

Set 16M high speed clock: 

Write the picture description here 

Then we look at the CLK_CKDIVR register: 

Write the picture description here 

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: 

Write the picture description here 

Writing the second register disables the counter from counting at the beginning. The register is as follows: 

Write the picture description here

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. 

CCMR Register Description 

This illustrates the pwm mode: 

Write the picture description here 

CCER2 is the enable register; 

CCER Register Description

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 

Write the picture description here

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!

Keywords:stm8  TIM2 Reference address:stm8 TIM2 PWM configuration

Previous article:RFID Programming Based on STM8
Next article:STM8S key PWM dimming lamp history

Recommended ReadingLatest update time:2024-11-16 17:49

How to set read protection and remove read protection using STVP programming program in STM8 series chips
First, open STVP, open the program to be burned (Open), if you want to read protect the chip, just set the protection option to ON. As shown in the figure below: Click Write All, and you will be prompted to set protection. Click "Yes" to write protection. After the above writing is completed, both reading and writin
[Microcontroller]
How to set read protection and remove read protection using STVP programming program in STM8 series chips
STM8 one-bit common anode digital tube 0 to F digital display
Sixiu Electronics Little Prince Development Board has a common anode digital tube with digital display from 0 to F. #define LED PC_ODR //1-digit digital tube segment code connection port group u8 tableB ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //Common anode digital tube
[Microcontroller]
STM8 erase method
When using STVP to program stm8, the erase option is always grayed out. Erasing method: Step 1: In OPTION BYTE, set the ROP option to Read Out Protection ON, and then burn (select Program, Current tab in turn); Step 2: Continue in OPTION BYTE, set the ROP option to Read Out Protection OFF, and then burn (select
[Microcontroller]
STM8 erase method
STM8 simulation debugging quick start
● Installation of ST Visual Develop   Go to the ST official website:  http://www.st.com/stonline/products/support/micro/files/sttoolset.exe  to download and install. ● Setting up ST Visual Develop software and creating and opening related debugging files   - using Cosmic C language, software simulation   If you want
[Microcontroller]
STM8 simulation debugging quick start
STM8 burning and encryption/decryption process
Open the burning software STVP Drag the program xxx.s19 to be burned to the burning software Click on the window "OPTION BYTE" Click the "ROP" tab and select "Read Out Protection ON" Click the Burn button A prompt box pops up, click "Yes" Complete the burning and encryption process Click the Read Chip bu
[Microcontroller]
STM8 burning and encryption/decryption process
STM8 IO port bit operation
Many novices don’t know how to perform bitwise operations on the I/O ports of STM8 without using the STM library functions. Let me share with you my experience when I first started using STM8! p style="color: rgb(51, 51, 51); font-family: Tahoma, SimSun, Verdana, Arial; font-size: 12px; line-height: normal;" span st
[Microcontroller]
STM8 clock modification
System functions Modify the clock of STM8 and display it with LED to observe whether the flashing speed of LED changes.    hardware design LED control circuit schematic diagram software design /************************************************************************ Weixue Electronics WaveShare http://www.waveShar
[Microcontroller]
STM8 clock modification
Experiment 8: Servo
  The servo is shown in the figure above. The left picture is a 9g servo, and the right picture is a 55g servo. Unlike the DC reduction motor, it works by turning to the corresponding angle according to the pulse width, and the angle range is 180 degrees. The servo angle corresponding to the pulse width of the PWM wa
[Microcontroller]
Experiment 8: Servo
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号