通电后只转一次,之后就不转了。舵机没有问题
#include "stm32f10x.h"
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //TIM3?1?2?? ,??PWM
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //PA?6,7???TIM3?1?2??,?????????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; //TIM4?1?2?3?4??
GPIO_Init(GPIOB,&GPIO_InitStructure); //B??6,7,8,9??TIM4?1?2?3?4??,?????????
TIM_TimeBaseStructure.TIM_Period =9999; //???????
TIM_TimeBaseStructure.TIM_Prescaler =143; //???? ,???50HZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //????
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //?????????
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM3,?TIM4??????,???? ,PWM???50HZ
//?????
TIM_OCStructInit(& TIM_OCInitStructure); //????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //??????pwm??1
TIM_OCInitStructure.TIM_Pulse =0; //???,????????
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //???????TIM3???1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //?????
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);//TIM4?4??????????
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE); //??PWM??
}
void SetJointAngle(u8 ID, float angle)
{
switch(ID)
{
case 0: //-90°~90°
angle=angle+90.0;
angle=(u16)(50.0*angle/9.0+249.0);
TIM_SetCompare1(TIM3,angle);
break;
//0°~180°
case 1:
angle=(u16)(4.175*angle+409.25);
TIM_SetCompare2(TIM3,angle);
break;
case 2: //-150°~0°
angle=-angle;
angle=(u16)(4.175*angle+480.0);
TIM_SetCompare1(TIM4,angle);
break;
case 3:
angle=-180-angle;
angle=-angle;
angle=(u16)(4.175*angle+315.0);
TIM_SetCompare2(TIM4,angle);
break;
//-90°~90°
case 4:
angle=90.0+angle;
angle=(u16)(249.0+50.0*angle/9.0);
TIM_SetCompare3(TIM4,angle);
break;
default: break;
}
}
int main()
{
TIM_Configuration();
while(1)
{
SetJointAngle(1, 30.0);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>