1 description
- Using stm8s internal clock (HSI);
- PWM mode 2;
- Duty ratio is 50%, frequency is 2Hz (convenient test LED lamp);
- PD2 The LED lights, PD2 port output PWM wave;
- System clock Initialization is important: CLK_CKDIVR |= 0x08;
2 Code
/*tim2_ch3 PWMPD2 output*/#include <STM8S003F3P.h>voidClk_init (void){Clk_ickr|=0x01;//Enable internal high-speed clock HSI CLK_CKDIVR|=0x08;//16m Internal RC via 2, System clock is 8M while(! (clk_ickr&0x02));//hsi ready .Clk_swr=0xe1;//HSI Primary Clock source}voidINIT_TIM2 (void) {TIM2_CCMR3 |=0X70;//Set timer 23-channel (PD2) output compare three modesTIM2_CCMR3 |=0x04;//Output comparison 3 pre-load enableTim2_ccer2 |=0x03;//Channel 3 Enable, low active, configured as output //Initialize the clock divider to 1, i.e. the counter clock frequency is Fmaster=8m/64=0.125mhzTIM2_PSCR =0x06;//Initialize the automatic load register to determine the frequency of the PWM square wave, fpwm=0.125m/62500=2hzTim2_arrh =62500/ the; TIM2_ARRL =62500% the;//Initialize comparison register to determine the duty ratio of the PWM square wave: 5000/10000 = 50%TIM2_CCR3H =31250/ the; TIM2_CCR3L =31250% the;//Start count; Update Interrupt deactivationTIM2_CR1 |=0x81;//tim2_ier |= 0x00; }voidInit_gpio (void){/ * Set to push-pull output, PD2 LED light * / pd_ddr|=0x04;//Set PD2 port to output modePD_CR1 |=0x04;//Set PD2 port to push-pull output modePd_cr2 &=0XFD;pa_ddr|=0X08;//Set PA3 port to output modePA_CR1 |=0X08;//Set PA3 port to push-pull output modePa_cr2 |=0xf7;}voidMainvoid) {clk_init (); Init_gpio (); INIT_TIM2 (); while(1);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
stm8s---timer generates PWM (TIM2)