cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 TIM PWM on CH2N only

tg.developer
Associate II
Posted on July 15, 2015 at 12:29

Hello,

I have configured only PB0 (TIM1_CH2N) for PWM output, but I can't get any signal from the pin. code looks like this:

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE);

  gpio_struct.GPIO_Mode = GPIO_Mode_AF_PP;

  gpio_struct.GPIO_Pin = GPIO_Pin_0;

  gpio_struct.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB,&gpio_struct);

  

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);

  /* timebase setup */

  tmr_period = (SystemCoreClock / 25000);

  TIM_TimeBaseStructure.TIM_Period = tmr_period - 1;  

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  TIM_OCInitStructure.TIM_OutputState  = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = tmr_period/2;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  

  /* channel 2*/

  TIM_OC2Init(TIM1, &TIM_OCInitStructure); 

  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

  /* enable main output */

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

  /* enable timer */

  TIM_Cmd(TIM1, ENABLE);

Thanks

#pwm #timer #stm32f103
5 REPLIES 5
Posted on July 15, 2015 at 17:14

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tg.developer
Associate II
Posted on July 15, 2015 at 17:39

I already figured your proposal, but even now it does not working.

tg.developer
Associate II
Posted on July 15, 2015 at 17:52

I have found the problem in TIM1 remap, what SHOULD be:

GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);

thanks clive1

tg.developer
Associate II
Posted on July 16, 2015 at 08:16

I tried put PWM in idle state, so the the:

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;

will pull PWM signal to HIGH state.

I tried to preset MOE control bit, but it only disables PWM and I can't get

pre-programmed HIGH signal on output.

Any advice?

Posted on July 16, 2015 at 14:14

You you try min/max values in CCR2, ie 0% 100%

TIM1->CCR2 = 0; // 0%

TIM1->CCR2 = TIM1->ARR + 1; // 100%

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..