Main function Accept: Using the MCU STM8S105C6 PWM Channel 2 PC2 to do the breathing light has been verified OK, ah//Ah, this PWM setup just started with a little trouble, because it is their own groping, take some time, or conquer.
The child functions used are all called stm8s in the library function stm8s_tim.c.
Macro definition://Divider macro//Counter clock frequency (fck_cnt) equals fck_psc/(pscr[15:0]+1).
#define TIM1_PRESCALER_1 ((U16) 0x00) #define TIM1_PRESCALER_2 ((U16) 0x01) #define TIM1_PRESCALER_3 ((U16) 0x02) #define TIM1_PRESCALER_4 ((U16) 0x03) #define TIM1_PRESCALER_5 ((U16) 0x04)//PWM frequency setting #define MDIV2//16m 2 #if defined (MDIV2) #define Tim1_div_set tim1_div2 #define PWMFRE_8KHZ 1000//main frequency 16m/2=8m 1/8k=1/8m*1000 cycle 0.125us #def INE pwmfre_4khz #define PWMFRE_2KHZ 4000 #define PWMFRE_1KHZ 8000 #define PWMFRE_05KHZ 16000 #define Pwmfre_025kh Z 32000 #define Pwmfre_020khz 40000//200hz cycle 5000us/0.125 =40000 #define PWMFRE_38KHZ 210//for IR 38KHZ carrier #define Pwmfre_ The OPEN 70//1/3 PWM duty ratio #else//16m set #define TIM1_DIV_SET tim1_div1 #define PWMFRE_8KHZ//1/8k=1/16m*2000 #defin E pwmfre_4khz 4000 #define PWMFRE_2KHZ 8000 #define PWMFRE_1KHZ 16000 #defiNE pwmfre_05khz 32000 #define pwmfre_025khz 64000 #define PWMFRE_38KHZ 421//for IR 38KHZ carrier #define Pwmfre_open 140//1/3
PWM duty #endif//Initialization call: Tim1_prescalerconfig (tim1_prescaler_2,tim1_pscreloadmode_immediate); 1 divide i.e. do not assign Allow event update tim1_oc2init (tim1_ocmode_pwm1,tim1_outputstate_enable,tim1_outputnstate_enable, 0,TIM1_OCP
Olarity_low,tim1_ocnpolarity_high, Tim1_ocidlestate_set,tim1_ocnidlestate_set); PWM1 mode Tim1_countermodeconfig (TIM1_COUNTERMODE_UP);//Up-counting mode tim1_arrpreloadconfig (enable);//re-preloaded to enable tim1_ Oc2preloadconfig (enable);//Comparator 2 pre-state enable Tim1_setcounter (0);//Initialize Count value tim1_setautoreload (pwmfre_020khz);//0.125us Cycle 40 *0.125=5us 200khz cycle is 5us 5/0.125 tim1_setcompare2 (0);//Duty ratio tim1_ctrlpwmoutputs (enable);//PWM control enable tim1_cmd (enable);
/Enable TIM1 Count Tim1_ccxncmd (tim1_channel_2,enable);//The second channel output enable//main program call (10ms execution 1 times)://Change duty-free if (F_add) {
Pwmval + = 100;
if (Pwmval >=) {f_add = 0;
}} else {pwmval-= 100;
if (Pwmval <=) {f_add = 1;
}} tim1_setcompare2 (Pwmval);