2016-10-06 06:27 AM
Hİ GUYS, I trying to use sg90 servo motors.As following code,Systemclock is 168Mhz. I divided clock of timer4 by 4 and then calculate pwm as 50 hz. But logic analyzer show 99hz. So my servo dont work. Can any one help me about that?
/**
* * * * * * * * * * * * * SERVO PROJECT * * * * * * * * * * * * * * * * *
*@brief use servo motor SG90 with pwm to turn 90 and 180 degree *
*@author ATABERK TOPÇU *
* *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include ''stm32f4xx.h'' // Device header
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
SystemInit();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
/** @brief MAKE GPIO CONFIGURATION FOR SETTING OUTPUTS PINS */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
GPIO_Init(GPIOB,&GPIO_InitStructure);
/** @brief MAKE TIMER CONFIGURATION FOR USE PWM */
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 19999;
TIM_TimeBaseInitStructure.TIM_Prescaler = 42-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_Cmd(TIM4,ENABLE);
/** @brief TIMER OUTPUT COMPARE FOR FREQUENCY AND PERIPOD OF PWM*/
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 1999;
TIM_OC1Init(TIM4,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
while(1);
}
2016-10-06 07:03 AM
Hi Ataberk,
I recommend you to have a look to the TIM example under the it may be helpful.STM32F4xx_DSP_StdPeriph_Lib_V1.7.1\Project\STM32F4xx_StdPeriph_Examples\TIM\TIM_7PWMOutputThis example shows how to configure the TIM1 peripheral to generate 7 PWM signals with 4 different duty cycles (50%, 37.5%, 25% and 12.5%).TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock (SystemCoreClock is set to 168 MHz for STM32F4xx devices).-Syrine-2016-10-06 08:14 AM
Hi Ataberk,
You should pay attention to the system clock configuration and the Timer clock configuration. As first step , you should well configure the system clock by putting the right prescaler of each bus (AHB, APB1 and ABP2) . In you case , Timer4 is on ABP1 which clock should be : APB1_clock = PCLK1= HCLK /4 = 42 MHzThe Timer clock input is by default TIM3CLK = PCLK1 = 42MHzThen to get the 50 Hz Timer output , you should focus on both period and prescaler formulas:Periode= ARR = (TIM3 counter clock / TIM3 output clock) - 1Prescaler = PSC = (TIM3CLK / TIM3 counter clock) - 1So let's take a possible configuration example :To get TIM3 counter clock = 30000 HzWe need ARR= (TIM3 counter clock / 30000) - 1 For PSC= 0 -> TIM3 counter clock = TIM3CLK = 42 MHz -> ARR= (42000000 / 30000) -1 = 1400 -1In your code you can replace TIM3 counter clock with SystemCoreClock So we have in code:Period = (SystemCoreClock / 30000) - 1;Prescaler= 0;ClockDivision=0;You may refer to the Timer examples on standard peripheral library or STM32Cube library.There are helpful application note line describes many features on Timers and shows different configuration. And of course, the reference manual still always first source of information.-Hannibal-2016-10-06 08:28 AM
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4; // This doesn't do what you seem to think it does.
Only Prescaler and Period define what the output frequency are.I can't get to 31.99 Hz from the math, so it suggests the part isn't actually running at 168 MHz. Suggest you revisit the PLL settings, and output the actual clock via MCO (PA8)2016-10-06 11:33 PM
Well what do clock divison timer do? Does it divide clock which come to timer?
2016-10-06 11:35 PM
Well what do clock divison timer do? Does it divide clock which come to timer?