AnsweredAssumed Answered

Problem with PWM

Question asked by crow.john on Oct 30, 2015
Latest reply on Oct 30, 2015 by Clive One
Hey there.
For few days i've been trying to successfully init PWM, but i didnt managed to do it. It looks like i saw all of the examples on the internet.
I want to init simple PWM channel with timer on LED.
STM32F103 runs clock at the 72MHz.
The LED duty cycle should go from 0 to 100% in 5s.
Can someone tell me where and what im doing wrong?

void PWM_init (void) {
    uint16_t Period;
    //************Init type defs ************
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
 
    //***** LED CONFIG*****
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
    GPIO_StructInit(&GPIO_InitStructure);
    //GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
     
    GPIO_InitStructure.GPIO_Pin = STATUS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(STATUS_PORT, &GPIO_InitStructure);
     
    Period = (SystemCoreClock / 21000000);
    //******* TIMER init ********
    RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
    TIM_DeInit(TIM3);
    TIM_TimeBaseInitStructure.TIM_Prescaler = 0 ;
    TIM_TimeBaseInitStructure.TIM_Period = Period - 1;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
    TIM_ARRPreloadConfig(TIM3, ENABLE);
 
     
 
    TIM_OCStructInit(&TIM_OCInitStructure);
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_Pulse = Period / 2 ;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    TIM_CtrlPWMOutputs(TIM3, ENABLE);
    TIM_OC1Init( TIM3, &TIM_OCInitStructure);
    TIM_Cmd(TIM3, ENABLE);
}

Outcomes