初级会员
- 积分
- 64
- 金钱
- 64
- 注册时间
- 2019-11-14
- 在线时间
- 15 小时
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//PWM
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11;//PUL 频率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//定义管脚的模式
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_Pin_8, GPIO_AF_2);//配置PA8 复用成TIM1-CH1
GPIO_PinAFConfig(GPIOA, GPIO_Pin_11, GPIO_AF_2);//配置PA11复用成TIM1-CH4
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 20;//装载值
TIM_TimeBaseStructure.TIM_Prescaler = 10;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复计数寄存器(TIMx_RCR),TIM1和TIM8才有
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //信号输出到对应的输出引脚
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable; //互补信号输出到对应的输出引脚
TIM_OCInitStructure.TIM_Pulse = 10; //脉冲宽度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //互补输出高电平有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出高电平有效
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态为1
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //互补输出空闲状态为0
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //OC1通道初始化
TIM_OC4Init(TIM1,&TIM_OCInitStructure); //OC4通道初始化
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);//允许自动重装载寄存器(ARR)
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能PWM输出 |
|