cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f30x: How to connect PB8 and PB9 to Timer16CH1 and Timer17CH1

RBaum.1
Associate

Hello all,

i have a Problem on a STM32F30x as described in the question.

I can´t achieve to connect TIM16CH1 and TIM17CH1 to GPIOB 8 and 9. The goal is to get a PWM signal on these pins based on a compare match.

Both Timers are running. If i read the corresponding TIM16->CNT rest. TIM17->CNT register i can see both are counting up and restarting after a overflow occurred. But i´m not getting a signal on the pin PB8 and PB9.

Here is my code:

/**
* System clock is initalized 
*/
 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
 
GPIO_InitTypeDef GPIOB_pwm_init;
GPIOB_pwm_init.GPIO_Mode = GPIO_Mode_AF;
GPIOB_pwm_init.GPIO_OType = GPIO_OType_PP;
GPIOB_pwm_init.GPIO_Pin = (GPIO_Pin_8 | GPIO_Pin_9);
GPIOB_pwm_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIOB_pwm_init.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOB, &GPIOB_pwm_init);
 
 TIM_DeInit(TIM16);
 TIM16->CR1 |= (0x01 << 7);
 TIM16->CCMR1 |= (0x04 << 4 ) | (0x01 << 3);
 TIM16->CCER |= (0x01);
 TIM16->PSC = 71; 
 TIM16->ARR = 4000;
 TIM16->CCR1 = 2000;
 TIM_Cmd(TIM16, ENABLE); 
 
 TIM_DeInit(TIM17);
 TIM17->CR1 |= (0x01 << 7);
 TIM17->CCMR1 |= (0x04 << 4 ) | (0x01 << 3);
 TIM17->CCER |= (0x01);
 TIM17->PSC = 71; 
 TIM17->ARR = 4000;
 TIM17->CCR1 = 2000;
 TIM_Cmd(TIM17, ENABLE);    

I have spend hours reading through the reference manual but no hints.

Help is welcome!

3 REPLIES 3
berendi
Principal

TIM16 and TIM17 have break functionality, where there is an additional Master Output Enable bit that must be set to get any output on the pins.

TIM16->BDTR |= TIM_BDTR_MOE;
TIM17->BDTR |= TIM_BDTR_MOE;

Also note that both PSC and ARR are programmed with N-1 for N states, ie 0 .. N-1

You perhaps want 0 .. 3999, with the center at 2000, ie 0..1999, 2000..3999

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

Thank you @berendi for the explanation and @clive1 for your hint.

I was able to solve the problem end the oscilloscope is showing a nice pwm signal now.

Thanks 8) :)