高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-5-23
|
楼主 |
发表于 2017-8-17 10:56:34
|
显示全部楼层
static void MOTOR_PWMOUTPUT_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// 开启TIMx_CLK,x[2,3,4,5,12,13,14]
RCC_APB2PeriphClockCmd(Motor_TIM_CLK, ENABLE);
/* 累计 TIM_Period个后产生一个更新或者中断*/
//当定时器从0计数到8999,即为9000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Period = 50-1;
// 通用控制定时器时钟源TIMxCLK = HCLK/2=90MHz
// 设定定时器频率为=TIMxCLK/(TIM_Prescaler+1)=100KHz
TIM_TimeBaseStructure.TIM_Prescaler = 90-1;
// 采样时钟分频
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// 计数方式
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// 初始化定时器TIMx, x[2,3,4,5,12,13,14]
TIM_TimeBaseInit(Motor_TIM, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50-1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(Motor_TIM, &TIM_OCInitStructure); //使能通道1
TIM_OC2Init(Motor_TIM, &TIM_OCInitStructure); //使能通道2
/*使能通道1重载*/
TIM_OC1PreloadConfig(Motor_TIM, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(Motor_TIM, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(Motor_TIM, ENABLE); //使能TIM重载寄存器ARR
// 使能定时器
TIM_Cmd(Motor_TIM, ENABLE);
}
|
|