cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S TIM1 PWM2 not working

levantoan2029
Associate II
Posted on June 25, 2016 at 05:22

Hi all. please help me. I program my code in IAR embeded, I built no error, no warning, but it not working. this is my code in main.c :

#include ''main.h''

void main(void)

{

  CLK_HSICmd(ENABLE);

  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);

  GPIO_Config();

  TIM1_Config();

  

  while (1)

  {

//TIM1_SetCompare1(349);

  }

  

}

void TIM1_Config()

{

  TIM1_DeInit();

  /* cau hinh timer 1 co dau ra PWM la 10kHz

  - bo chia tan 2

  - duty =30%

   Ftimer=16/2=8Mhz

   TimerPeriod= Ftimer/Fpwm  -1=799  

 

  */

  TIM1_TimeBaseInit(1, TIM1_COUNTERMODE_UP, 799, 0);

  TIM1_SelectOCxM(TIM1_CHANNEL_1,TIM1_OCMODE_PWM2);

  TIM1_OC1PolarityConfig(TIM1_OCPOLARITY_LOW);

  TIM1_CCxCmd(TIM1_CHANNEL_1,ENABLE);

  TIM1_SetCompare1(239);

  TIM1_OC1PreloadConfig(ENABLE);

  TIM1_ARRPreloadConfig(ENABLE);

  

    /* TIM1 counter enable */

  TIM1_Cmd(ENABLE);

  /* TIM1 Main Output Enable */

  TIM1_CtrlPWMOutputs(ENABLE);

  

}

void GPIO_Config(void)

{

  GPIO_DeInit(GPIOB);

  GPIO_Init(GPIOB,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);

  GPIO_DeInit(GPIOC);

  GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_OUT_PP_LOW_FAST);

}

and my code in main.h 

#ifndef __MAIN_H

#define __MAIN_H

/* Includes ------------------------------------------------------------------*/

#include ''stm8s.h''

#include ''user_delay.h''

void TIM1_Config();

void GPIO_Config(void);

#endif /* __MAIN_H */

 

thanks for helping! 
5 REPLIES 5
Walid FTITI_O
Senior II
Posted on July 12, 2016 at 18:31

Hi toanlee,

Refer to the ''TIM1_7PWM_Output'' example in

/content/my_st_com/en/products/embedded-software/mcus-embedded-software/stm8-embedded-software/stsw-stm8html/save-bookmark

, which output PWM2 signals like you need. Compare with your code to identify what you have missed.

Example is at this path: STM8S_StdPeriph_Lib\Project\STM8S_StdPeriph_Examples\TIM1\TIM1_7PWM_Output

-Hannibal-

levantoan2029
Associate II
Posted on December 24, 2016 at 06:21

Thank you so much! i successfull.  

Posted on April 28, 2017 at 00:13

Hello,

I have implemented the above sample on my system and am NOT getting any output on Timer1 Channel1 on an STM8S103F3 which is Pin 16 on the TSOP package and the designation is PC6/ SPI_MOSI [TIM1_ CH1].

I've tried modifying the polarity and enable/disable of the output channels - nothing seems to change any behavior.

Does anyone know why this might be happening?

Here is the TIM1_Config function with a few small modifications to deal with frequency.

static void TIM11_Config(void)

{

uint16_t cr1 = 78; // this gives approximately 100.8 kHz

uint16_t period = ((cr1+1)*2)-1;

TIM1_DeInit();

/* Time Base configuration */

/*

TIM1_Period = 4095

TIM1_Prescaler = 0

TIM1_CounterMode = TIM1_COUNTERMODE_UP

TIM1_RepetitionCounter = 0

*/

TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, period, 0);

/* Channel 1, 2,3 and 4 Configuration in PWM mode */

/*

TIM1_OCMode = TIM1_OCMODE_PWM2

TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE

TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE

TIM1_Pulse = CCR1_Val

TIM1_OCPolarity = TIM1_OCPOLARITY_LOW

TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH

TIM1_OCIdleState = TIM1_OCIDLESTATE_SET

TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET

*/

TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_DISABLE, TIM1_OUTPUTNSTATE_ENABLE,cr1, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET,TIM1_OCNIDLESTATE_RESET);

/*TIM1_Pulse = CCR2_Val*/

TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, cr1, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET);

/*TIM1_Pulse = CCR3_Val*/

TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE,cr1, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET);

/*TIM1_Pulse = CCR4_Val*/

TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, cr1, TIM1_OCPOLARITY_LOW, TIM1_OCIDLESTATE_SET);

/* TIM1 counter enable */

TIM1_Cmd(ENABLE);

/* TIM1 Main Output Enable */

TIM1_CtrlPWMOutputs(ENABLE);

}

That's all there is to it - pretty straight forward.

Gary Anson
Associate II
Posted on May 10, 2017 at 17:25

I've discovered that the processor required one of the OPTION bits to be set to change the alternate function of the pin. This problem has probably bit many people over the years.

Tim Bartlett
Associate
Posted on July 11, 2017 at 06:42

mmmm...and it is chomping into my time right now.