Question
Not true pwm signal as in code which ı write in stm32f4vg
Posted on October 06, 2016 at 15:27
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);
}