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;
Previous article:stm8l15x TIM3 PWM configuration
Next article:STM8 hardware PWM settings
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- #Free application for evaluation#DFRobot AS7341 visible spectrum sensor
- One picture lets you understand TE's various industrial motor sensors
- [Project Source Code] How to forcefully delete the device driver when a device driver conflict causes a blue screen
- A beginner is asking for help. The source code of two different LPC1768 chips, the QEIPOS register is not used
- I2C Controlled 6A Three-Level Switch-Mode Single Cell Battery Charger
- Sipeed LicheeRV 86 Panel Review】6- Review Summary
- FPGA Experiment (III) VGA Color Bar Signal Display Based on HDL Language
- The key to the warehouse was found, and the music phone from 15 years ago was unboxed!
- Application of OPT3004 in battery-powered cameras
- Does anyone on the forum have a SiliconLabs BRD4001A board?