一下是对TIM3 的初始化,是否需要定义定时器的通道号,应该怎么设置?
我想让TIM3的CH3(PB0)产生PWM波,帮我看看下面的程序:
void TIM3_Configuration(uint Period,uint Pulse) //使用PB0作为电机的PWM输出,TIM3_CH3属于APB1
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能由APB1时钟控制的外设中的TIM3端口
/* TIM3 Peripheral Configuration ----------------------------------------*/
TIM_DeInit(TIM3); //复位定时器3所有寄存器
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 1; //预分频数为1,不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式为向上计数模式
TIM_TimeBaseStructure.TIM_Period = Period - 1; //设置计数器溢出后的重载初值 ?
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //配置时钟分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循环计数次数值
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //用以上参数初始化定时器时间基础模块
/* Channel 3 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pulse; //设置捕获比较器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //设置TIM_OCPolarity极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;j
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 5;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM3, &TIM_BDTRInitStructure);
//清中断
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
//TIM1中断源设置,开启相应通道的捕捉比较中断
TIM_ITConfig(TIM3, TIM_FLAG_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE); //使能定时器3
TIM_CtrlPWMOutputs(TIM3, ENABLE); //使能定时器3的PWM输出
}
此帖出自
小平头技术问答
一周热门 更多>