STM32:配置定时器为PWM输出模式以及编码器接口模式

一:STM32的高级定时器TIM8配置为PWM输出模式

最近在工程项目中,需要用stm32的定时器 (以高级定时器TIM8为例 ) 产生两路PWM输出,作为电机的pwm,方案如下:

/*************初始化电机控制所需IO口**************/
void Motor_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;			
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);//使能A口、B口时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);//使能C口时钟
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;		//PB0、1 端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化PB
	//GPIO_ResetBits(GPIOB,GPIO_Pin_All);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;		//PC4、5端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
	GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化PC
	
}
/*************
说明:
只需两路PWM输出,分别是: TIM8->CH3和TIM8->CH4 两路通道
TIM8->CH3:对应引脚输出为PC 8
TIM8->CH4:对应引脚输出为PC 9
**************/
void Motor_PWM_Init(u16 arr,u16 psc)
{
	Motor_Init();  //初始化电机控制所需IO
	GPIO_InitTypeDef  GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	TIM_DeInit(TIM8);  //初始化TIM8寄存器
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);	//使能定时器8时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	//使能GPIO外设时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);	//使能AFIO复用功能模块时钟
	
//设置引脚为复用功能,输出TIM8 -->\CH1		   \CH2		  	的PWM脉冲波形
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;		//PC 6、7 端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 		 //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
	GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化PC
	 
	TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载周期值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值 不分频
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据指定的参数初始化TIM8
		 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式	
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
	TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OC 低电平有效 	
	TIM_OC3Init(TIM8, &TIM_OCInitStructure);  //根据指定的参数初始化外设TIM8
	TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH1 预装载使能
	
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式	
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
	TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //OC 低电平有效	
	TIM_OC4Init(TIM8, &TIM_OCInitStructure);  //根据指定的参数初始化外设TIM8
	TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH2 预装载使能
	
	TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIM8在ARR上的预装载寄存器	
	TIM_CtrlPWMOutputs(TIM8,ENABLE);	//MOE 主输出使能,高级定时器必须开启这个 	
	TIM_Cmd(TIM8, ENABLE);  //使能TIM8
	
}

二:STM32的普通定时器TIM4配置为编码器接口模式

使用的是500线编码器(四条引线,分别为:A相、B相,电源线和GND),方案如下:

/**********把TIM4初始化为编码器接口模式****************/
void Encoder_Init_TIM4(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_DeInit(TIM4);  //初始化TIM4寄存器
	
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能A、B、AFIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);	

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;	//PB6/7 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  //浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化PB6/7
	
TIM_TimeBaseStructure.TIM_Period = 65535; //设置自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIM2
	
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
	
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1|TIM_Channel_2; //CC1S=01 	选择输入端 IC1映射到TI1上
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;	//边沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 //配置输入分频,不分频 
TIM_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM_ICInitStructure);
	
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM4->CNT = 0;
TIM_Cmd(TIM4, ENABLE);   //启动TIM2定时器
}

 

你可能感兴趣的:(My,growth)