初中生
最后登录1970-1-1
在线时间 小时
注册时间2019-4-11
|
我想用stm32f4控制机械手的张开和闭合,已经写了一部分,但是连接到PA8管脚上动不起来,求大佬指点
#include "pwm.h"
/*******************************************************************************
*******************************************************************************/
void TIM1_CH1_PWM_Init(u16 pre,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//
GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM1);//
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//­
GPIO_Init(GPIOF,&GPIO_InitStructure); //
TIM_TimeBaseInitStructure.TIM_Period=pre; //
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //Ê
TIM_ARRPreloadConfig(TIM1,ENABLE);//
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE); //
}
void servo_init(void)
{
TIM1_CH1_PWM_Init(SERVO_TIM_PRE,SERVO_TIM_PSC);
TIM_SetCompare1(TIM1,150);
}
void servo_angle(uint16_t angle)
{
uint16_t pulse;
if(angle<=0)
angle=0;
if(angle>=180)
angle=180;
pulse=(uint16_t)(50+angle*100/90.0);
TIM_SetCompare1(TIM1,pulse);
}
void servo_debug(void)
{
uint8_t i;
for(i=0;i<10;++i)
{
delay_ms(500);
servo_angle(45);
delay_ms(500);
servo_angle(90);
delay_ms(500);
servo_angle(135);
delay_ms(500);
servo_angle(90);
}
}
|
|