[mw_shl_code=c,true]#include "bsp_ServoMotor.h"
void ServoMotor_Pulse_Init(void)
{
/* 结构体初始化 */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器结构体
TIM_OCInitTypeDef TIM_OCInitStructure;//定时器复用结构体
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2| RCC_APB1Periph_TIM3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* 定时器通道引脚复用 */
GPIO_PinAFConfig(TIM2_GPIO_PORT,TIM2_PINSOURCE,TIM2_AF);
/* 定时器通道引脚复用 */
GPIO_PinAFConfig(TIM3_GPIO_PORT,TIM3_PINSOURCE,TIM3_AF);
/* 定时器通道引脚配置 */
GPIO_InitStructure.GPIO_Pin = TIM2_PIN| TIM3_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(TIM3_GPIO_PORT, &GPIO_InitStructure);
GPIO_Init(TIM2_GPIO_PORT, &GPIO_InitStructure);
/* TIM2工作在单脉冲下 */
TIM_TimeBaseStructure.TIM_Period = 1000-1; //100US
TIM_TimeBaseStructure.TIM_Prescaler = 9000-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;// 采样时钟分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;// 计数方式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);// 初始化定时器TIM2
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
TIM_OCInitStructure.TIM_Pulse = 20;//比较捕获1的预装载值
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2,DISABLE);//先不使能能TIM2
/* tim3工作在从模式的门控模式下的PWM输出模式 */
TIM_TimeBaseStructure.TIM_Period = TIM3per;
TIM_TimeBaseStructure.TIM_Prescaler = 900-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//TIM2初始化
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);//TIM3为门控模式
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);//使能TIM3的主从模式
TIM_SelectInputTrigger(TIM3,TIM_TS_ITR1);//内部触发,从TIM2触发
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
TIM_OCInitStructure.TIM_Pulse = TIM3per/2;//比较捕获1的预装载值
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_Cmd(TIM3,ENABLE);//使能TIM3
}
void ServoMotor_Control(u16 cycle, u16 pulse_num)
{
u16 arr3 = 0;
u32 Time = 0;
if(pulse_num)
{
Time = cycle * pulse_num / 100;//预分频为9000,100us计数一次
arr3 = cycle / 10; //预分频为900,10us计数一次
TIM_SetAutoreload(TIM2, Time + 19);//低电平周期加高电平周期
TIM_SetAutoreload(TIM3, arr3 - 1);
TIM_SetCompare1(TIM3, arr3 / 2); //设置PWM占空比为50%
TIM_Cmd(TIM2, ENABLE);//使能TIM2
}
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>