初级会员
- 积分
- 192
- 金钱
- 192
- 注册时间
- 2019-12-4
- 在线时间
- 29 小时
|
10金钱
void Servo_PWM_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //使能定时器1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA的时钟
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; //PA11
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseInitStruct.TIM_Prescaler = psc; //设定预分频器
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0; //设置时钟分割
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct); //初始化定时器
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //使能定时器中断
NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_IRQn; //使能外部中断通道
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级1
NVIC_Init(&NVIC_InitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //选择PWM1模式
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStruct.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //设置输出极性
TIM_OC4Init(TIM1,&TIM_OCInitStruct); //初始化输出比较参数
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable); //CH4使能预装载寄存器
TIM_CtrlPWMOutputs(TIM1,ENABLE); //高级定时器输出需要设置这句
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM1在ARR上的预装载寄存器
// TIM1->CCR4=1500;
TIM_Cmd(TIM1,ENABLE); //使能定时器1
TIM1->BDTR |= 1<<15; //TIM1要这么一句才可以输出PWM
//TIM_CtrlPWMOutputs(TIM1,ENABLE); // MOE 主输出使能,高级定时器必须开启这个
}
|
|