中级会员
- 积分
- 238
- 金钱
- 238
- 注册时间
- 2016-9-16
- 在线时间
- 51 小时
|
3金钱
void TIM3_PWM4_Init3(u16 state)//一号舵机
{
static u16 i=200;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿íêè«ÖØó3éä TIM3_CH4->B1
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH4μÄWMÂö3å2¨DÎ GPIOB.0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH3
{GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; } //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO
//3õê¼»ˉTIM3
TIM_TimeBaseStructure.TIM_Period = 1999; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler = 719; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM3 Channel 3 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 18;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM3 OC4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIM3
if(state == 1) //Æe
{
for(;i>140;i--)
{
TIM_SetCompare4(TIM3,i);
delay_ms(20);
}
}
else
{
for(;i<200;i++)
{
TIM_SetCompare4(TIM3,i);
delay_ms(20);
}
} //Âä
}
void TIM3_PWM3_Init2(u16 state)//二号舵机
{
static u16 i=160;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer22¿íêè«ÖØó3éä TIM3_CH3->B0
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH4μÄWMÂö3å2¨DÎ GPIOB.0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM_CH3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO
//3õê¼»ˉTIM3
TIM_TimeBaseStructure.TIM_Period = 1999; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler = 719; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM3 Channel3 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_Pulse = 180;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM3 OC3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIM3
if(state == 1) //Æe
{
for(;i>130;i--)
{
TIM_SetCompare3(TIM3,i);
delay_ms(30);
}
}
else
{
for(;i<190;i++)
{
TIM_SetCompare3(TIM3,i);
delay_ms(30);
}
}
}
int main(void)
{
RCC_Configuration();
delay_init();
uart_init(115200);
TIM3_PWM3_Init2(1);//二号舵机
delay_ms(1000);
TIM3_PWM4_Init3(1);//一号舵机
while(1);
}
|
最佳答案
查看完整内容[请看2#楼]
1,把所有舵机拔了,用示波器打波形,如果正确,到第2步
2,舵机需要的电流比较大,如果大电流反向影响到控制引脚,也可能会乱,这时,将所有地线全部接回电源供电端
|