- UID
- 1029342
- 性别
- 男
|
/******************************TIM2的通道1使用单脉冲模式**TIM3使用门控模式用来输出PWM******************************///TIM2per:重装值//Compare1:比较捕获1的预装载值void Motor_Init(u16 TIM2per, u16 TIM3per, u16 TIM3Compare1){ TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能定时器2的时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能定时器3的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能复用IO时钟 http://write.blog.csdn.net/postedit GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_6;//PA0 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); //TIM2工作在单脉冲下 TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//没有时钟分割 TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式 TIM_TimeBaseInitStruct.TIM_Prescaler = 7200;//预分频值,每100us计数一次 TIM_TimeBaseInitStruct.TIM_Period = TIM2per;//重装值 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止 TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器 TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref); TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平 TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能 TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高 TIM_OCInitStruct.TIM_Pulse = 1;//比较捕获1的预装载值 TIM_OC1Init(TIM2,&TIM_OCInitStruct); TIM_Cmd(TIM2,DISABLE);//先不使能能TIM2 //TIM3工作在从模式的门控模式下的PWM输出模式 TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//没有时钟分割 TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式 TIM_TimeBaseInitStruct.TIM_Prescaler = 720;//预分频值,10us计数一次 TIM_TimeBaseInitStruct.TIM_Period = TIM3per;//重装值 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);//TIM3为门控模式 TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);//使能TIM3的主从模式 TIM_SelectInputTrigger(TIM3,TIM_TS_ITR1);//内部触发,从TIM2触发 TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平 TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能 TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高 TIM_OCInitStruct.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值 TIM_OC1Init(TIM3,&TIM_OCInitStruct); TIM_Cmd(TIM3,ENABLE);//使能TIM3} |
|