cancel
Showing results for 
Search instead for 
Did you mean: 

Change PWM pulse

fenice-6
Associate II
Posted on September 14, 2015 at 20:00

Hi,

I'm using a servo with a pwm connection and this is a part of code:

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_1);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_BaseStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_BaseStruct.TIM_Prescaler = 48-1;
TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseStruct.TIM_Period = 20000-1; 
TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_BaseStruct);
TIM_Cmd(TIM3, ENABLE); 
TIM_OCInitTypeDef TIM_OCStruct;
TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
/* 50% duty cycle */
TIM_OCStruct.TIM_Pulse = 10000; 
TIM_OC1Init(TIM3, &TIM_OCStruct);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

How can I change dinamically TIM_Pulse? (I'm using ST32F0xx)
1 REPLY 1
Posted on September 14, 2015 at 21:05

TIM3->CCR1 = width in micro seconds, or TIM_SetCompare1(TIM3, x);

So perhaps a value between 600 to 2400, or 1500 top-centre, for a servo

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..