2016-06-24 08:22 PM
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!2016-07-12 09:31 AM
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-2016-12-23 09:21 PM
Thank you so much! i successfull.
2017-04-27 05:13 PM
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 kHzuint16_t period = ((cr1+1)*2)-1;TIM1_DeInit();
/* Time Base configuration */
/*TIM1_Period = 4095TIM1_Prescaler = 0TIM1_CounterMode = TIM1_COUNTERMODE_UPTIM1_RepetitionCounter = 0*/TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, period, 0);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
/*TIM1_OCMode = TIM1_OCMODE_PWM2TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLETIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLETIM1_Pulse = CCR1_ValTIM1_OCPolarity = TIM1_OCPOLARITY_LOWTIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGHTIM1_OCIdleState = TIM1_OCIDLESTATE_SETTIM1_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.
2017-05-10 08:25 AM
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.
2017-07-10 09:42 PM
mmmm...and it is chomping into my time right now.