STM32F429使用TIM4和TIM14 的PWM波驱动步进电机

最近做了一个使用STM32F429的TIM4和TIM14产生的PWM波来驱动两个步进电机的项目。
看到有不少新入行的同学们在找TIM产生PWM方波的例程代码,就在这里放一份,供大家参考。
经过亲测,唯一需要注意的是TIM4和TIM14产生占空比的方式稍有不同:
TIM14使用
TIM_SetCompare1(TIM14,625); 但是这个办法对TIM4行不通。

TIM4使用
TIM_OCInitStructure.TIM_Pulse = dutyCycle;

废话不多说了,Talk is easy,show me the code. 直接上代码了。这段代码是我自己亲测可用的。


//
TIM14 PWM初始化 //PWM输出初始化 //arr:自动重装值 psc:时钟预分频数。 void TIM14_PWM_Init(u32 arr,u32 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //是能 PORTF时钟 GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GF9复用为TIM14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9 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(GPIOF,&GPIO_InitStructure); //初始化PF9 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(TIM14,&TIM_TimeBaseStructure);//初始化定时器14 //初始化TIM14 Channel1 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出是能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性低 TIM_OC1Init(TIM14, &TIM_OCInitStructure); //初始化外设 TIM1 4OC1 TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //使能预装载寄存器 TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPE使能 TIM_Cmd(TIM14, ENABLE); //关闭使能TIM14 } void TIM4_PWM_Init(u32 arr,u32 psc, u32 dutyCycle) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM4时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //是能 PORTD时钟 GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_TIM4); //GD14复用为TIM4 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //GPIO D14 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(GPIOD,&GPIO_InitStructure); //初始化PD14 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(TIM4,&TIM_TimeBaseStructure);//初始化定时器14 //初始化TIM4 Channel3 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出是能 TIM_OCInitStructure.TIM_Pulse = dutyCycle; //配置占空比 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性低 TIM_OC3Init(TIM4, &TIM_OCInitStructure); //初始化外设 TIM4 OC3 通道3 TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable); //使能预装载寄存器 TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能 TIM_Cmd(TIM4, ENABLE); //使能TIM14 } //PF9 -->TIM14 Channel_1 void StepMotor_Init(void) { TIM14_PWM_Init(1250-1,84-1); //定时器时钟为84M,分频系数为84,所以计数频率为84M/84=1Mhz,重装载值1250,所以PWM频率为 1M/1250=800hz. TIM_SetCompare1(TIM14,625); //占空比50%. PWM每一个周期为1s/800hz=1.25ms=1250us.设置低电平时间为625us. //升降电机拨码为1600步每圈。一圈6cm。 现在每秒800步,所以速度为3cm/s GPIO_InitTypeDef GPIO_InitStructure; /*时钟使能*/ //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); /* 引脚设置 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8; GPIO_Init(GPIOF, &GPIO_InitStructure); PFout(7)=1; //打开使能 PFout(8)=1; //确定方向 0为向上 1为向下 //PD14 PD15 TIM4 /// 必须按照厂商给定的配置来设置,控制器对应的横向电机的噪音才会很小。 /// 厂商给定配置控制器拨码为 800步每圈。PWM给定频率为每秒2000个脉冲 /// 定时器时钟为84M,分频系数为84,所以计数频率为84M/84=1Mhz,重装载值500,所以PWM频率为 1M/5000=2000hz. /// 占空比50%. PWM每一个周期为1s/2000hz=0.5ms=500us.设置低电平时间为250us. // TIM4 对应水平的滑杆:控制器的拨码,800步前进一圈,一圈为1.2cm。每秒钟中有2000步,所以速度为3cm/s TIM4_PWM_Init(500-1,84-1,250); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOD, &GPIO_InitStructure); //PDout(14)=1; PDout(15)=1; //1 为朝左 0为朝右 }

 

posted @ 2018-04-24 14:26  愚人1984  阅读(7250)  评论(0编辑  收藏  举报