2014-12-15 08:19 AM
Hello everybody,
I've been working with my STM32 and to be honest I'm going to throw it out through my windows soon. I set a PWM (TIM1, CH1) and it actually was working preety well. Than I want to change a pulse using a SetCompare and CCR. It does not work. Then I choose just to initialize again PWM Channel just with some other pulse, and I was really suprised that LED still gleam with the same power. I can not change pulse witdh. It seem like It bloked itself or something like that. I do not have a clue. Please let me know if You have any ideas, below I put a code maybe I mess something up in this area or maybe it's CooCox.void InitLED3()
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE,ENABLE); // enables clock to portE
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LED3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // _IN _AF:Alternate _AN:Analog
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // _2MHz _10MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // _PP:Push/Pull _OD:OpenDrain
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // _UP _DOWN
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, LED3, GPIO_AF_2);
}
void InitializeTimer_PWM()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 25;
TIM_TimeBaseStructure.TIM_Period = 511;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
}
void InitializePWMChannel()
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 16; // preset pulse width 0..pwm_period
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Pulse polarity
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// These settings must be applied on the timer 1.
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1 , ENABLE);
And just one more thing, if I delete aInitializePWMChannel andInitializeTimer_PWM it's still shining.
Thank You in advance, hope You help.
2014-12-15 09:54 AM
GPIO_PinAFConfig() uses a pin index (GPIO_PinSourceX), not a bit mask (GPIO_Pin_X) like GPIO_Init()
2014-12-15 11:00 AM
Thank You for your response, it works. Can not believe how stupid mistake I was doing. And thanks for doing that as well. Have a nice day ; )