STM8S103F3--PWM configuration

Publisher:雅意盎然Latest update time:2020-01-28 Source: eefocusKeywords:STM8S103F3 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The chip used in this article is stm8s103f3.


The following mainly describes the process of configuring TIM2 channel 1 for PWM output.


1. Register configuration

clip_image004

4-bit prescaler, the counter count frequency Fck_cnt = Fck_psc/2^(PSC[3:0]) = 16M/2^0 = 16M


Then the counting period is 1/16us, which means the counter will count once every 1/16us.


TIM2_PSCR=0x00; //Division value=0 16M

clip_image006

Auto-reload register,


When the auto-reload register = 0, the counter is in a blocked state, that is, it is not counting. Because when CNTR = ARR, CNTR will be cleared, so the auto-reload register should be greater than 0 during configuration. In addition, the value of the auto-reload register is the period of the PWM waveform. For example, if ARR = 0X0100, the period of PWM is 0x0100*1/16=16us, and the period of PWM is 16us.


  TIM2_ARRH=0x01; //Automatically reload the value 0x0100


  TIM2_ARRL=0x00;

clip_image008

Capture/compare enable register. The configuration of this register can select the corresponding Tim2 channel.


bit5, bit4 are configured for ch2, bit1, bit0 are configured for ch1


If OC1 is currently an output channel, then


bit1: OC1 low level is valid


bit0: OC1 signal is output to the current pin


  TIM2_CCER1=bit0|bit1; //low level,OC1

clip_image010

clip_image012

clip_image014

Capture/Compare Mode Register,


bit1,bit0


CC1S[1:0] is capture/compare selection


        00: CC1 channel is configured as output


bit2 reserved


bit3 output compare preload enable


    0: Disable


    1: Enable


bit6, bit5, bit4 output comparison mode


    110: PWM1 mode


Difference between PWM1 and PWM2 modes


PWM1: CNT

PWM2: CNT>CCR, CH1 is activated


TIM2_CCMR1=bit3|bit5|bit6; //MODE

clip_image016

Capture/compare register, this register determines the duty cycle of PWM. CCR/ARR = PWM duty cycle, for example, if it is set to 0x0060, then the duty cycle is 0x0060/0x0100=6/16


  TIM2_CCR1H=0x00;


  TIM2_CCR1L=0x60;

clip_image018

Interrupt enable register. If an interrupt is required, it can be set here.


bit1: CC1E capture/compare 1 interrupt enable


    0: CC1 interrupt is disabled


    1: CC1 interrupt enable


  TIM2_IER=0x00; //Update interrupt enable

clip_image020

Control register, bit0 controls the opening and closing of the counter.


bit0, counter enable


    0: Disable


    1: Enable


  TIM2_CR1=bit0; //enable counter


The output level of the PD4 pin is tested by a logic analyzer, and the waveform is shown below.

clip_image022

The source code is as follows,


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


 1 #define bit0 0x01 

 2 #define bit1 0x02 

 3 #define bit2 0x04 

 4 #define bit3 0x08 

 5 #define bit4 0x10 

 6 #define bit5 0x20 

 7 #define bit6 0x40 

 8 #define bit7 0x80

 9 

10 void Timer2_Init(void) 

11 { 

12 CLK_ICKR|=0x01; //Turn on internal HSI 

13 while(!(CLK_ICKR&0x02));//HSI is ready 

14 CLK_SWR=0xe1; //HSI is the main clock source 

15 CLK_CKDIVR=0x00; //HSI, 8 division=16M 

16 TIM2_PSCR=0x00; //Division value=0 16M 

17 TIM2_ARRH=0x01; //Automatically reload the value 0x0100 

18 TIM2_ARRL=0x00; 

19 TIM2_CCER1=bit0|bit1; //low level,OC1

20  

21 TIM2_CCMR1=bit3|bit5|bit6; //MODE 

22 TIM2_CCR1H=0x00; 

23 TIM2_CCR1L=0x60; 

24 TIM2_IER=0x00; //Update interrupt enable 

25 

26 TIM2_CR1=bit0; //enable counter 

27 }

28 int main( void ) 

29 { 

30 Timer2_Init(); 

31 while(1){}; 

32 }

Keywords:STM8S103F3 Reference address:STM8S103F3--PWM configuration

Previous article:The process of debugging PWM of STM8S103
Next article:STM8S103F3P6 lock problem and unlock

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号