新手上路
- 积分
- 45
- 金钱
- 45
- 注册时间
- 2019-7-27
- 在线时间
- 15 小时
|
3金钱
请大家帮忙看看,用定时器TIM3_CH3和CH4输出pwm,哪里错了
void TIM3_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //使能PORTB时钟
GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_TIM3); //GPIOB0¸复用为定时器3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_TIM3); //GPIOB1¸复用为定时器3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIOB1,B0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //退挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB,&GPIO_InitStructure); //使能PB0,PB1
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分屏
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动装载模式
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//初始化定时器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
TIM_OCInitStructure.TIM_Pulse=500;
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //根据TIM3在CCR1预装载寄存器
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //根据TIM3在CCR1预装载寄存器
TIM_ARRPreloadConfig(TIM3,ENABLE);//ARPE使能
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}
|
|