Mình đang làm điều khiển RC Servo như sao nó không chạy. Mọi người giúp mình với. Không biết phần tính timer mình tính đúng chưa. Mình dùng STM32F4 Discovery #include stm32f4xx.h void DELAY_Configuration(); void RCC_Configuration(void); void GPIO_Configuration(void); void TIMER_Configuration(void); int main(void) { RCC_Configuration(); GPIO_Configuration(); TIMER_Configuration(); DELAY_Configuration(); while(1) { TIM_SetCompare1( TIM1, 1000); delay_ms(500); TIM_SetCompare1( TIM1, 1500); delay_ms(500); TIM_SetCompare1( TIM1, 2000); delay_ms(500); } } void DELAY_Configuration(void){ SysTick_Config(SystemCoreClock/1000000); } void RCC_Configuration(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); RCC_AHB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); } void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1); } void TIMER_Configuration(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseStructure.TIM_Prescaler = 16800-1; // 50Hz; TIM_TimeBaseStructure.TIM_Period = 200; // 20000us 20ms TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM1, ENABLE); /* TIM1 enable counter */ TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line){ while (1){ } } #endif
Posted on: Fri, 19 Sep 2014 16:40:48 +0000
Trending Topics
Recently Viewed Topics
© 2015