说明
PB14 PB15是STM32的TIM1的反向引脚 L11 H8 F2 35 53 75 PB14 I/O FT PB14 SPI2_MISO/TIM1_CH2N USART3_RTS(8)/ L12 G8 F1 36 54 76 PB15 I/O FT PB15 SPI2_MOSI/I2S2_SD TIM1_CH3N(8)/ 可以输出PWM波 极性与TIM1_CH相反。 设置一个pwm波形,设置他的占空比和频率即可。
代码
/**
- @file motor.c
- @author STMicroelectronics
- @version V1.0
- @date 2016.2.2
- @brief 聚焦电机控制
- @attention
1000hz 20%占空比
*/
#include "IRFocusMotor.h" #include "gpio_array.h" /**
- @brief 配置TIM2复用输出PWM时用到的I/O
- @param 无
- @retval 无
*/ #define IRPWMPERIOD 1000 //此刻频率1k #define IRPWMDUTY 200 //此刻占空比80% static void TIM1_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* 设置TIM2 CLK 为 72MHZ */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); // GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);//配置 PB5 为复用输出。
/*GPIOA Configuration: TIM2 channel 1 and 2 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**
- @brief 配置TIM3输出的PWM信号的模式,如周期、极性、占空比
- @param 无
- @retval 无
/ /
- TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> TIMxCNT 重新计数
TIMx_CCR(电平发生变化) 复制代码
- 信号周期=(TIMx_ARR +1 ) * 时钟周期
- 占空比=TIMx_CCR/(TIMx_ARR +1)
*/ static void TIM1_Mode_Config(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = IRPWMPERIOD; //当定时器从0计数到999,即为1000次,为一个定时周期 TIM_TimeBaseStructure.TIM_Prescaler = 719; //设置预分频:不预分频,即为72MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = IRPWMDUTY; //设置跳变值,当计数器计数到这个值时,电平发生跳变 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道1 TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = IRPWMDUTY; //设置通道2的电平跳变值,输出另外一个占空比的PWM TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //当定时器计数值小于CCR1_Val时为高电平 TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2 TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM3重载寄存器ARR TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM3 enable counter */ TIM_Cmd(TIM1, ENABLE); //使能定时器3 }
/**
- @brief TIM3 输出PWM信号初始化,只要调用这个函数
TIM3的四个通道就会有PWM信号输出 复制代码
- @param 无
- @retval 无
*/ void TIM1_PWM_Init(void) { TIM1_GPIO_Config(); TIM1_Mode_Config(); }
void IRFocus_Ctrl(unsigned char state) {
if(state) { TIM_CCxCmd(TIM1, TIM_Channel_3, DISABLE); TIM_CCxCmd(TIM1, TIM_Channel_2, DISABLE);
TIM_SetCompare3(TIM1,IRPWMDUTY); TIM_CCxCmd(TIM1, TIM_Channel_3, ENABLE); } else {
TIM_CCxCmd(TIM1, TIM_Channel_3, DISABLE); TIM_CCxCmd(TIM1, TIM_Channel_2, DISABLE);
TIM_SetCompare2(TIM1,IRPWMDUTY); TIM_CCxCmd(TIM1, TIM_Channel_2, ENABLE);
}
}