cancel
Showing results for 
Search instead for 
Did you mean: 

PWM signal generation for a servo motor

kasun_duminda92
Associate III
Posted on January 01, 2013 at 14:08

I want to generate a PWM signal to control a servo motor. The servo has a dead band width of 10?s. I have tried doing this using the '7_PWMOutputs' example.

Code so far:

_______________________________________________________________________

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

uint16_t TimerPeriod = 0;

uint16_t Channel1Pulse = 0,

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

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

TimerPeriod = (SystemCoreClock / 17570) - 1;

/* With variable 'x'*/

Channel1Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = TimerPeriod;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);

TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);

TIM_Cmd(TIM1, ENABLE);

TIM_CtrlPWMOutputs(TIM1, ENABLE);

while(1){

}

_______________________________________________________________________

How and where should I change or what should I add more to output a PWM signal with a 25% duty cycle and a 10?s dead band width.

Thanks in advance!

#pwm
7 REPLIES 7
Posted on January 01, 2013 at 19:30

The PWM Frequency is controlled by the Prescale and Period values, divided into the timer source clock, the width is controlled by Pulse in units in the range of the Period value, and quanta of timer source divided by Prescale.

Prescale and Period are in the form N-1, where N is the divisor.

Here is a 50 Hz Servo example for the VL Discovery, I have something similar for the F4

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/Servo%20control%20using%20STM32VLDiscovery&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000491D59B8574F8049B5DFA3E8B21CBA51&currentviews=1106]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32VLDiscovery%2FServo%20control%20using%20STM32VLDiscovery&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000491D59B8574F8049B5DFA3E8B21CBA51¤tviews=1106
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rihab150192
Associate II
Posted on May 16, 2014 at 18:18

rihab150192
Associate II
Posted on May 16, 2014 at 22:40

Thank you for the code but it didn't work i use microservo TowerPro 9g and it has only one input 

Posted on May 17, 2014 at 00:45

Thank you for the code but it didn't work i use microservo TowerPro 9g and it has only one input

Then just use one output.... If you think there is a problem with the signal inspect it with a scope and report.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lee2
Associate II
Posted on April 13, 2015 at 21:05

lee2
Associate II
Posted on April 13, 2015 at 21:08