cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S003 125khz PWM not work

hungtp
Associate
Posted on May 29, 2016 at 11:50

Hi All,

I have new application used STM8S003, The MCU using timer1 or timer2 to generator 125khz PWM, duty cycle is 50% with 8Mhz HSI clock source.

- This is my code to initial time2:

static void Timer2_Init(void)

{

//Reset value:

TIM2_DeInit(); 

//Config clock for timer2:

//timer freq = (clock CPU/8) -> 1bit = 1uS -> 8*1uS=8uS (125khz).

TIM2_TimeBaseInit(TIM2_PRESCALER_8,8);   

/* PWM1 Mode configuration: Channel1 */ 

TIM2_OC1Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE,500, TIM2_OCPOLARITY_HIGH);

        TIM2_OC1PreloadConfig(ENABLE);

TIM2_ARRPreloadConfig(ENABLE);

  TIM2_Cmd(ENABLE); 

}

- System clock init:

static void System_init(void)

{

CLK_DeInit();

CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);//CPU Prescaler = 1/

CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV2);//Prescaler = 8, 16Mhz

//CLK_CCOCmd(ENABLE);

CLK_CCOConfig(CLK_OUTPUT_CPUDIV64);//Output clock on CCO pin.

CLK_ClockSwitchConfig(

CLK_SWITCHMODE_AUTO,//Enable the automatic clock switching mode 

CLK_SOURCE_HSI   ,//Clock Source HSI.

DISABLE , //Disable interrupt

CLK_CURRENTCLOCKSTATE_DISABLE//Current clock enable 

);

CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER1,ENABLE);

CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER2,ENABLE);

CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER4,ENABLE);

//HSI Presscaler (1,2,4,8)

//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//Setting 16Mhz.

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);//Setting 8Mhz.

//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV4);//Setting 4Mhz.

//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV128);//Setting 125khz.

//While until HSI is ready!

while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)!=SET);

}

This code is not working when generator 125Khz, but I did change output to 3khz it is work well, I don't know reason of MCU. I have try with timer1 result is same.

Please help me to review my code and help me to fix that.

Thanks

#pwm125k-with-stm8s
0 REPLIES 0