DWORD32
DWORD32

Reputation: 11

STM32F103 PWM output not working

I'm trying to set up one of the LEDs on the STM3210E-EVAL board as a PWM output so that I can vary the brightness.

I am targeting the red LED, which is on port F, pin 8. I have set up timer 13 which should be tied to that pin for PWM output, but I feel like like I am missing a step somewhere. Here is the current function to initialize the pin, setup the timer, and set up the PWM:

void led_init(void)
{
    TIM_OC_InitTypeDef sConfigOC;
    TIM_HandleTypeDef htim13;

    /* Configure GPIO pins : PF8 */
    __HAL_AFIO_REMAP_TIM13_ENABLE();
    __GPIOF_CLK_ENABLE();
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

    htim13.Instance = TIM13;
    htim13.Init.Prescaler = (uint32_t)(72000000 / 2000000) - 1;
    htim13.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim13.Init.Period = 700;
    htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    HAL_TIM_Base_Init(&htim13);

    HAL_TIM_PWM_Init(&htim13);

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 350;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    sConfigOC.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
    sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
    sConfigOC.OCIdleState  = TIM_OCIDLESTATE_RESET;

    HAL_TIM_PWM_ConfigChannel(&htim13, &sConfigOC, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1);
}

Upvotes: 0

Views: 5110

Answers (2)

LoriB
LoriB

Reputation: 391

It seems you aren't enabling the timer's clock:

__HAL_RCC_TIM13_CLK_ENABLE()

Did you start your project from an example or STM32cubeMX? Usually some init code like this part goes to the stm32f1_hal_msp.c file! It's kind of elegant to also put your PWM pin (PF8) init there!

Upvotes: 1

I think you should specify which alternate function you use on the GPIO. In that case it is PWM. There must be a function like GPIO_PinAFConfig.

Upvotes: 0

Related Questions