cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with PWM

e239955
Associate II
Posted on October 30, 2015 at 08:37

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);
}

1 REPLY 1
Posted on October 30, 2015 at 12:48

RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

It's not on APB2 You set it up for 21 MHz, seems a bit rapid, you'd need a scope to see that. 4 Hz might be more perceivable. Remember boot Prescaler and Period are 16-bit so you'll need to factor the numbers. Don't see any code to phase/vary the duty.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..