0

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);
}
2
  • Please define what you mean by 'not working'? Commented Aug 27, 2015 at 21:36
  • The output is 0V. In the code above, the duty cycle should be set to 50%, but there is nothing lighting up on the board. If I set the pin as a standard GPIO output, the LED turns on with no issues. Now I want to setup PWM so that I can dim the LED. Commented Aug 27, 2015 at 21:44

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.