AnsweredAssumed Answered

stm32F051 discovery Timer1 PWM

Question asked by Pumuckl on Dec 17, 2015
Latest reply on Dec 18, 2015 by Pumuckl
Hi,
I am trying to get a PWM Signal out of my discovery board but I must be missing something. I started with a writing the registers directly, then I tried it with the peripheral library. I looked at various examples but I cannot find my problem. The timer is running, I can toggle the pin... Please somebody help me, I am going crazy! Here is the code.
Best regards,
Christian

#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_tim.h"
#include "system_stm32f0xx.h"
  
void main(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    uint16_t period;
    uint16_t pulse;
  
    SystemInit();
  
    period = 0xFF;
  
    pulse = period >> 1;
  
    /* GPIOA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
    /* Initialize PA8, Alternative Function, 50Mhz, Output, Push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_0);
  
    /* TIM1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  
    /* Timer Base configuration */
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = period;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  
    /* Channel 1 output configuration */
    TIM_OCStructInit(&TIM_OCInitStructure);
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = pulse;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  
    /* Enable Preload register on CCR1. */
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  
    /* TIM1 counter enable */
    TIM_Cmd(TIM1, ENABLE);
  
    /* TIM1 Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
  
    /* forever... */
    while (1)
    {
        __asm("nop");
    }
}

Outcomes