Use the PWM function of the STM8S003 timer to output PWM waves

Publisher:hylh2008Latest update time:2020-01-30 Source: eefocusKeywords:STM8S003 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Use the CH1, CH2, and CH3 channel PWM output of timer TIM2


1. When using the stm8s series of microcontrollers, most of the header files used are universal stm8s. The first step is to modify the header file:


/* Uncomment the line below according to the target STM8S or STM8A device used in your

   application. */


 /* #define STM8S208 */ /*!< STM8S High density devices with CAN */

 /* #define STM8S207 */ /*!< STM8S High density devices without CAN */

 /* #define STM8AF52Ax */ /*!< STM8A High density devices with CAN */

 /* #define STM8AF62Ax */ /*!< STM8A High density devices without CAN */

 /* #define STM8S105 */ /*!< STM8S Medium density devices */

 /* #define STM8AF626x */ /*!< STM8A Medium density devices */

  #define STM8S103 /*!< STM8S Low density devices */

 /* #define STM8S903 */ /*!< STM8S Low density devices */


2. Uncomment stm8s103. The register addresses of 103 and 003 are the same, so we use 103 instead of 003.


3. When the pin uses the PWM function, there is no need to configure the GPIO state, just configure the register directly. For example, the library function configuration of TIM2 is as follows:


    TIM2_TimeBaseInit(TIM2_PRESCALER_1, 7999); /* 1 division, count value 8000, frequency = 16M/8000 = 2kHZ */

   

    /* PWM1 Mode configuration: Channel1 */

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

    TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,2000, TIM2_OCPOLARITY_HIGH);

    TIM2_OC1PreloadConfig(ENABLE);



    /* PWM2 Mode configuration: Channel2 */

    TIM2_OC2Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE,2000, TIM2_OCPOLARITY_HIGH);

    TIM2_OC2PreloadConfig(ENABLE);


     /* PWM3 Mode configuration: Channel3*/


    TIM2_OC3Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE,2000, TIM2_OCPOLARITY_HIGH);

    TIM2_OC3PreloadConfig(ENABLE);

    

    TIM2_Cmd(ENABLE);


4. After the configuration is completed, debug and output a 2kHz PWM wave with a duty cycle of 25%, as shown in the following figure:

5. Because the three PWM channels of TIM2 do not use the Alternative function, there is no need to configure Options Bytes.


2. Use the PWM function of CH1, CH2, and CH3 channels of timer TIM1


1. CH1 and CH2 of TIM1 are pin multiplexing functions that need to be configured. Before debugging the program, you need to configure Option Bytes. AFR0 is set to Alternate active.

2. TIM1 three-channel output PWM register example:


  CLK->CKDIVR= 0x00; //Internal 16M clock is not divided

  TIM2->PSCR = 0x00; //Timer 2 pre-scaling factor is 1

  TIM2->CR1 = 0x01; //Internal counter enable;

  TIM2->ARRH = 0x00; //0140, 320 division, 16M/320=50K, measured 49.75K

  TIM2->ARRL = 0x8a; //Automatically load register low bit; the measured value of 013e is exactly 50.0K

  /*PWM channel 1 settings*/

  TIM2->CCER1 |= 0x01; //Turn on OC1 signal output pin

  TIM2->CCMR1 = 0x60; //PWM1 mode

  TIM2->CCR1H = 0x00; //Duty cycle high

  TIM2->CCR1L = 0x37; //Duty cycle low bit

  TIM2->CCMR1 |= 0x08; //Output compare 1 preload enable

  /*PWM channel 2 settings*/

  TIM2->CCER1 |= 0x10; //Turn on OC2 signal output pin

  TIM2->CCMR2 = 0x60; //PWM1 mode

  TIM2->CCR2H = 0x00; //Duty cycle high

  TIM2->CCR2L = 0x37; //Duty cycle low bit

  TIM2->CCMR2 |= 0x08; //Output compare 2 preload enable

  /*PWM channel 3 settings*/

  TIM2->CCER2 |= 0x01; //Turn on OC3 signal output pin

  TIM2->CCMR3 = 0x60; //PWM1 mode

  TIM2->CCR3H = 0x00; //Duty cycle high

  TIM2->CCR3L = 0x37; //Duty cycle low bit

  TIM2->CCMR3 |= 0x08; //Output compare 3 preload enable

  TIM2->CR1 |= 0x01;


Keywords:STM8S003 Reference address:Use the PWM function of the STM8S003 timer to output PWM waves

Previous article:stm8l15x TIM3 PWM configuration
Next article:STM8 hardware PWM settings

Latest Microcontroller Articles
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号