PWM output configuration of STM8S105 series microcontroller

Publisher:bonbonoLatest update time:2019-11-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timer resources of STM8S105xx:


(1) Two 16-bit general-purpose timers (TIM2, TIM3) with 2+3 CAPCOM channels (IC, OC or PWM);


(2) Advanced control timer (TIM1): 16 bits, 4 CAPCOM channels (capture/compare channels), 3 complementary outputs, dead zone insertion and flexible synchronization;


(3) 8-bit basic timer (TIM4) with 8-bit prescaler;


(4) Automatic wake-up timer;


(5) 2 watchdog timers: window watchdog and independent watchdog.



There are differences in the initialization configuration of the PWM output function using the advanced control timer and the ordinary general timer. The following is a simple analysis:


TIM1 - 16-bit advanced control timer


16-bit up, down and bidirectional auto-reload counter with 16-bit prescaler


TIM2, TIM3——16-bit general-purpose timer


16-bit up-counting and auto-reload counter


15-bit prescaler, the division factor can be adjusted to a power of 2 value between 1 and 32768


TIM4 - 8-bit basic timer


8-bit auto-load adjustable prescaler, ratio selectable as power of 2 between 1 and 128


Configure the PWM output function of TIM1_CH1:



void Time1_Init()


{


      CLK->CKDIVR=0x00; 


      /*

     * Here, the counting frequency of TIM2 is set to 16M/1600=10000Hz

     */


      TIM1_TimeBaseInit(1599,TIM1_COUNTERMODE_UP,2000,0); //Division by 1600, counting up, TIM1_ARR=1000, repeat counter is 0


    /*OC1 initialization: PWM1 mode


                               Output Enable


                               Complementary output disabled


                               CCR1=1000, duty cycle is 50%


                               Output valid polarity is high


                               Complementary output effective polarity is high (no effect)


                               Output high when idle


                               Complementary output high when idle (no function)


     */


      TIM1_OC1Init(TIM1_OCMODE_PWM1,


      TIM1_OUTPUTSTATE_ENABLE,


      TIM1_OUTPUTNSTATE_DISABLE,


      1000,


      TIM1_OCPOLARITY_HIGH,


      TIM1_OCNPOLARITY_HIGH,


      TIM1_OCIDLESTATE_SET,


      TIM1_OCNIDLESTATE_SET);


     


      TIM1_OC1PreloadConfig(ENABLE);  


      TIM1_CtrlPWMOutputs(ENABLE); //PWM output enable


      TIM1_Cmd(ENABLE); //Timer starts counting





Configure the PWM output function of TIM2_CH1:

void TIMER2_PWMInit(unsigned short timebase,unsigned short t)

{

      TIM2_DeInit();  

      /*

       * TIM2 Frequency = TIM2 counter clock/(ARR + 1) 

       * Here, the counting frequency of TIM2 is set to 16M/32/(timebase+1)=1K

       */

      TIM2_TimeBaseInit(TIM2_PRESCALER_32, timebase); 

    

      /* 

       * PWM1 Mode configuration: Channel1

       * TIM2 Channel1 duty cycle = [TIM2_CCR1/(TIM2_ARR + 1)] * 100 = 50%

       * TIM2 Channel2 duty cycle = [TIM2_CCR2/(TIM2_ARR + 1)] * 100 = 50%

       */ 

    

      /* Test channel 1 */

      TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, t, TIM2_OCPOLARITY_HIGH);

      TIM2_OC1PreloadConfig(ENABLE);

     

      TIM2_Cmd(ENABLE);

}

Reference address:PWM output configuration of STM8S105 series microcontroller

Previous article:STM8S103F3P6 built-in LED driver assembly
Next article:stm8 debugging hardware I2C experience

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号