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);
}
Previous article:STM8S103F3P6 built-in LED driver assembly
Next article:stm8 debugging hardware I2C experience
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Microphone preamplifier circuit
- STM32 uses CycloneTCP protocol, HTTP 301/302 response problem
- Fudan Microelectronics MCU
- dsp bootloader c5000
- Crystal oscillator problem
- Design of PLC communication between CAN bus and computer
- What are the introductory knowledge points for Web front-end?
- [TI's Big Live Broadcast] Let's get started! For 30 yuan, let's play with the new MSP430 that helps eliminate smog, vertical and horizontal sensing and measurement
- HardFault location method and steps for smart microcontroller
- [ESP32-Audio-Kit Audio Development Board]-1. Installation of the development environment