AnsweredAssumed Answered

STM8S003 125khz PWM not work

Question asked by hungtrinh36 on May 29, 2016
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:
     //Config clock for timer2:
     //timer freq = (clock CPU/8) -> 1bit = 1uS -> 8*1uS=8uS (125khz).
     /* PWM1 Mode configuration: Channel1 */ 
- System clock init:
static void System_init(void)
     CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);//CPU Prescaler = 1/
     CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV2);//Prescaler = 8, 16Mhz
     CLK_CCOConfig(CLK_OUTPUT_CPUDIV64);//Output clock on CCO pin.
                                                            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!

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.