帮忙看下 还没弄到互补 2个PWM就让我倒下了
大神们 TIM_CtrlPWMOutputs(TIM1,ENABLE);这个我查了半天说什么也没懂 例程里面都用的通用定时器 第一次 弄 头皮发麻 希望解答下
#include "tb6612fng.h"
void Motor_Init(void)
{
GPIO_InitTypeDef GPIO_Motor_Init;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);//B口时钟使能
GPIO_Motor_Init.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Motor_Init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Motor_Init.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;//PB12,13,14,15
GPIO_Init(GPIOB , &GPIO_Motor_Init);
GPIO_SetBits(GPIOB , GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
}
void TIM1_PWM_Init(u16 psc,u16 arr)//定时器1 通道1-PA18,通道4-PA11
{
GPIO_InitTypeDef GPIO_TIME1_PWM;
TIM_TimeBaseInitTypeDef TIM_Time1_Init;
TIM_OCInitTypeDef TIM1_OC_Init;
Motor_Init();//初始化电机
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1 , ENABLE);
//PA时钟使能 复用时钟使能 定时器1时钟使能
GPIO_TIME1_PWM.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_TIME1_PWM.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;//PA8 PA11
GPIO_TIME1_PWM.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_TIME1_PWM);
TIM_Time1_Init.TIM_Prescaler = psc;//预分频系数
TIM_Time1_Init.TIM_Period = arr;//自动装载值
TIM_Time1_Init.TIM_CounterMode = TIM_CounterMode_Up;//计数模式 上
TIM_Time1_Init.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1 , &TIM_Time1_Init);//定时器1初始化
TIM1_OC_Init.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式 1
TIM1_OC_Init.TIM_OutputState = TIM_OutputState_Enable;//PWM输出通道使能
TIM1_OC_Init.TIM_OCPolarity = TIM_OCPolarity_High;//比较输出极性 高
TIM_OC1Init(TIM1 , &TIM1_OC_Init);//OC1输出比较参数初始化
TIM_OC4Init(TIM1 , &TIM1_OC_Init);//OC4输出比较参数初始化
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);//OC1
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);//OC4使能预装载寄存器
TIM_ARRPreloadConfig(TIM1,ENABLE);//自动重装载预装载
TIM_Cmd(TIM1,ENABLE);//使能TIM1
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
/**
* @brief Enables or disables the TIM peripheral Main Outputs.
* @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the TIMx peripheral.
* @param NewState: new state of the TIM peripheral Main Outputs.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM_LIST2_PERIPH(TIMx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the TIM Main Output */
TIMx->BDTR |= TIM_BDTR_MOE;
}
else
{
/* Disable the TIM Main Output */
TIMx->BDTR &= (uint16_t)(~((uint16_t)TIM_BDTR_MOE));
}
}
关键看上面的英文解释。
一周热门 更多>